【发布时间】:2021-05-19 08:02:06
【问题描述】:
我正在使用 jQuery DataTable 来呈现服务器端数据。我可以搜索某个字符串并根据它呈现 DataTable,但我想匹配多个字符串,以下代码不起作用。
如何在服务器端渲染dt.column(8).search('^(400|404)$', true, false).draw();进行以下工作
"buttons": [
'copy',
'csv',
{
text: 'Show All',
action: function(e, dt, node, config) {
dt.column(8).search('').draw();
}
},
{
text: 'Show Only Errors',
action: function(e, dt, node, config) {
dt.column(8).search('^(400|404)$', true, false).draw();
}
}
],
服务器端代码
public static IQueryable < T > ToIndividualColumnSearch < T > (this IQueryable < T > table, DTParameters Param) {
if (Param?.Columns != null && Param.Columns.Length > 0 && table.FirstOrDefault() != null) {
Type EntityType = table.FirstOrDefault().GetType();
var Properties = EntityType.GetProperties();
//listing necessary column where individual columns search has applied. Filtered with search text as well it data types
Param.Columns.Where(w => !string.IsNullOrEmpty(w.Search?.Value)).ToList().ForEach(x => {
foreach(var match in Properties.Where(p => p.Name == x.Data)) {
switch (match.PropertyType) {
case var i when(i == typeof(System.Int32)) | (i == typeof(System.Int32 ? )):
if (int.TryParse(x.Search.Value, out int intValue)) {
table = table.Where(x.Data + " = @0", intValue);
}
break;
case var b when(b == typeof(System.Boolean)) | (b == typeof(System.Boolean ? )):
bool searchValue = false;
if (bool.TryParse(x.Search.Value, out searchValue)) {
table = table.Where(x.Data + " = @0", searchValue);
} else
if (int.TryParse(x.Search.Value, out int bool2Int)) {
searchValue = bool2Int == 1;
table = table.Where(x.Data + " = @0", searchValue);
} else {
switch (x.Search.Value) {
case var yes when string.Compare(x.Search.Value, "yes", true) == 0:
table = table.Where(x.Data + " = @0", true);
break;
case var no when string.Compare(x.Search.Value, "no", true) == 0:
table = table.Where(x.Data + " = @0", false);
break;
}
}
break;
case var d when(d == typeof(System.DateTime)) | (d == typeof(System.DateTime ? )):
foreach(string dateTimeFormat in new string[] {
"yyyy-MM-dd",
"dd-MM-yyyy"
}) {
string[] bounds = System.Text.RegularExpressions.Regex.Split(x.Search.Value, "( - )");
if (bounds.Length == 3) {
if (DateTime.TryParseExact(bounds[0], dateTimeFormat, CultureInfo.InvariantCulture, DateTimeStyles.None, out DateTime lowerBound) &&
(DateTime.TryParseExact(bounds[2], dateTimeFormat, CultureInfo.InvariantCulture, DateTimeStyles.None, out DateTime upperBound))) {
upperBound = upperBound.AddDays(1);
table = table.Where(string.Format("{0} >= DateTime({1},{2},{3}) AND {0} < DateTime({4},{5},{6})", x.Data, lowerBound.Year, lowerBound.Month, lowerBound.Day, upperBound.Year, upperBound.Month, upperBound.Day));
break;
}
}
if (DateTime.TryParseExact(x.Search.Value, dateTimeFormat, CultureInfo.InvariantCulture, DateTimeStyles.None, out DateTime CreatedOn)) {
DateTime upperBound = CreatedOn.AddDays(1);
table = table.Where(string.Format("{0} >= DateTime({1},{2},{3}) AND {0} < DateTime({4},{5},{6})", x.Data, CreatedOn.Year, CreatedOn.Month, CreatedOn.Day, upperBound.Year, upperBound.Month, upperBound.Day));
break;
}
}
break;
case var s when(s == typeof(System.String)):
table = table.Where(x.Data + ".Contains(@0)", x.Search.Value);
break;
}
}
});
}
return table;
}
P.S -> 如果我只搜索像dt.column(8).search(400, true, false).draw(); 这样的1 个字符串,它可以工作,但不是正则表达式?我该怎么做才能让它在服务器端工作?还是正则表达式只适用于客户端渲染?
附上截图供您参考。
当使用dt.column(8).search('^(400|404)$', true, false).draw();时
当使用dt.column(8).search(400, true, false).draw();时
【问题讨论】:
-
如果你这样做
dt.column(8).search('(400|404)', true, false).draw();会有什么结果
标签: javascript jquery asp.net regex datatables