trouble with regex OR searching

trouble with regex OR searching

alexc1054alexc1054 Posts: 3Questions: 1Answers: 0

Hello, I am trying to enable regex searching to allow for filtering for two values on one column, i.e. "val1|val2". This feature seems well documented and I have looked at and tried to follow the given examples.

I followed this example to add search bars to each column, and slightly modified it to move them to the header instead of the footer (and this was done successfully, works just fine):
https://datatables.net/examples/api/multi_filter.html

To search using '|' (OR), I have read the following:
https://datatables.net/reference/api/search()
https://datatables.net/examples/api/regex.html

It would seem all I need to do is edit the .search() line...

.search( this.value, true, false )

but when I try this I get "No matching records found" upon using "val1|val2"
I have also tried adding the following to the datatable initilization:

"search": { "regex": true },

I can filter a single value of any column just fine, I just get nothing when trying two values with an '|' (OR)
here is all of my code:

$(document).ready(function(){

        // Setup - add a text input to each footer cell
        $('#datatable tfoot th').each( function () {
            var title = $(this).text();
            $(this).html( '<input type="text" placeholder="Search '+title+'" />' );
        } );
        $('#datatable tfoot tr').appendTo('#datatable thead');

        // DataTable + Initialization
        var datatable = $('#datatable').DataTable({
            "bServerSide":true,
            "ordering":true,
            "paging":true,
            dom: 'Bltip',
            scrollY: "40vh",
            scrollX: "20vh",
            "sAjaxSource":"{% url 'tabledata' %}",
            "search": { "regex": true },
            buttons: [
                'copyHtml5',
                'excelHtml5',
                'csvHtml5',
            ]
        });

        // Apply the search
        datatable.columns().every( function () {
            var filteredCol = this;

            $( 'input', this.header() ).on( 'keydown', function (ev) {
                if ( ev.keyCode == 13 ) {
                    filteredCol
                        .search( this.value, true, false )
                        .draw();
                }
            } );
        } );

    });

Running the debugger... I see this under table state...
Global Filter -

{
    "bCaseInsensitive": true,
    "sSearch": "",
    "bRegex": true,
    "bSmart": true,
    "_hungarianMap": {
        "caseInsensitive": "bCaseInsensitive",
        "search": "sSearch",
        "regex": "bRegex",
        "smart": "bSmart"
    },
    "regex": true
}

Column Filter (for every column) -

[{
    "bCaseInsensitive": true,
    "sSearch": "",
    "bRegex": false,
    "bSmart": true
}, {

This question has an accepted answers - jump to answer

Answers

  • kthorngrenkthorngren Posts: 20,147Questions: 26Answers: 4,736
    edited June 2018 Answer ✓

    Since you are using server side processing is your server script setup to support regex searching?

    Kevin

  • alexc1054alexc1054 Posts: 3Questions: 1Answers: 0

    It appears not, that's probably the issue. Looking into it.
    Thanks for the direction Kevin!

  • alexc1054alexc1054 Posts: 3Questions: 1Answers: 0

    Yeah, problem was on the server side processing logic. Thanks again.

This discussion has been closed.