Skip to content
Advertisement

Allow user to type regular expression into column search bar, datatables

I would like to be able to type a regular expression, for example ^.{2}06.{4,}$ into my individual column search bars. For reference I was able to do this when I was using the client side version of DataTables, but since switching over to server side to handle my larger table I haven’t been able to replicate this.

Here is a link to the example I have built off of https://www.datatables.net/examples/api/multi_filter.html

And here is my current code for the initialization of the table

var table = $('#results').dataTable({
        processing: true,
        serverSide: true,
        ajax: 'getTable.php',
        orderCellsTop: true,
        fixedHeader: true,
        "search" : { //this is what I have changed from the DataTables example//*
            "regex": true                                                     //*
        },                                                                    //*
        createdRow: function ( row ) {                                        //*
            $(row).addClass("hover");                                         //*
        },                                                                    //*
        //***********************************************************************
        initComplete: function () {
            this.api()
                .columns()
                .every(function () {
                    var that = this;
                    
                    $('input', this.footer()).on('keyup change clear', function () {
                        if (that.search() != this.value){
                            that.search( this.value ).draw();
                            
                            //I have also tried setting the optional regex value of the 
                            //search function to true with no success
                            //that.search( this.value , true ).draw();
                        }
                    });
                });
        }

    });

If anyone could help me with this it would be greatly appreciated

Advertisement

Answer

Look at the Sent Parameters section of the Server-Side Processing page in the official manual.

You will see two things of relevance:

search[regex] – true if the global filter should be treated as a regular expression for advanced searching, false otherwise.

And:

columns[i][search][regex] – Flag to indicate if the search term for this column should be treated as regular expression (true) or not (false).

So, your server-side route handler needs to parse the sent parameters in the response, to extract these booleans for each of your column filters. These values will determine if the column search term should be treated as a regex or not.

You also need to use the DataTables searchCols option to indicate which of your columns’ search terms should be treated as regexes – you cannot only use the search: { "regex": true } option because that is only for the global search field.

An example:

searchCols: [
  { regex: false },
  { regex: true },
  { regex: true },
  { regex: true },
  { regex: true },
  { regex: true }
]

The above example assumes a table with 6 columns, and all of their search terms – except for the first column – are to be treated as regexes.

The data can be found in the request body at columns[i][search][regex], as noted above.


There is a warning in the Data Tables documentation which is worth repeating:

As with global search, normally server-side processing scripts will not perform regular expression searching for performance reasons on large data sets, but it is technically possible and at the discretion of your script.


Final Note:

Client-side (DataTables) search logic is ignored when you use serverSide: true. That includes the DataTables search() API call included in your question:

that.search( this.value ).draw();

That is one of the fundamental points about using server-side processing for your DataTable: All logic for paging, sorting and filtering is handled on the server – and none of it is provided by DataTables. You have to provide all that logic yourself.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement