jQuery DataTable custom button regex column search

I am using jQuery DataTable for rendering serverside data. I am able to search a certain string and render the DataTable based on it but I would like to match multiple strings and the following code does not work.

How do I make the following work on the server side rendering 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();
        }
    }
],

Server-side code

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 -> If I only search for 1 string like dt.column(8).search(400, true, false).draw(); it works but not the regex? What can I do to make it work on the server-side? Or the regex only works on client-side rendering?

Attaching the screenshot for your reference too.

When using dt.column(8).search('^(400|404)$', true, false).draw();

enter image description here

When using dt.column(8).search(400, true, false).draw();

enter image description here

6 thoughts on “jQuery DataTable custom button regex column search”

Leave a Comment