[英]jQuery DataTable custom button regex column search
我正在使用 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;
}
PS -> 如果我只搜索 1 個字符串,例如dt.column(8).search(400, true, false).draw();
它有效但不是正則表達式? 我該怎么做才能讓它在服務器端工作? 還是正則表達式僅適用於客戶端渲染?
附上截圖供您參考。
當使用dt.column(8).search('^(400|404)$', true, false).draw();
當使用dt.column(8).search(400, true, false).draw();
你可以試試:(也許你在數字之前或之后有空格)
dt.column(8).search('^\\s*(400|404)\\s*$', true, false).draw();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.