Regex not working with DataTables

Regex not working with DataTables

avasmatavasmat Posts: 6Questions: 2Answers: 0

Hello,

I'm hoping there may be someone that can help me figure out how to use regex with DataTables.

So, I have a column that I'm looking to search. This column holds multiple different values, including 'Services' and 'Industry'. I'm looking to specifically search/filter for 'Services' and 'Industry'.

These are the attempted regex configurations:

regex_services = "/^(?=.(Services|Industry)).$/gm";
regex_services = "/\b(?:Services|Industry)\b/gi";

This is the search code I'm using

table_services.columns(1).search(regex_services, true, false, false).draw();

The issue is that there's no data returned in the table.

Any help is much appreciated! Thank you.

PS: the configuration below works, but only to search/filter for 'Services' and not 'Industry' as well.

table_services.columns(1).search('Services').draw();

Answers

  • kthorngrenkthorngren Posts: 20,276Questions: 26Answers: 4,765

    See if this example helps:
    http://live.datatables.net/giwefeye/1/edit

    Kevin

  • avasmatavasmat Posts: 6Questions: 2Answers: 0

    Thank you Kevin. Your example works as I wish my code would. I've tried replicating it, but my table still comes up empty (no data shows up). Very strange. I've even updated the DT code to the latest version.

    Wonder if perhaps my DT configuration may not be playing nice?

    This is the full JS code I'm using:

        var table_services = $('#services-all').DataTable({
        processing: true,
        serverSide: true,
        deferRender: true,
        pageLength: 25,
        lengthMenu: [25, 50, 75, 100 ],
        scrollY: "400px",
        scrollCollapse: true,
        pagingType: 'simple',
        orderClasses: false,
            responsive: {
            details: {
                    type: 'none',
                    target: ''
                }
            },
            ajax: {
                url: 'x.php',
                type: 'GET',
            },
            order: [ 4, 'desc' ],
            columnDefs: [
                {
                    target: 0,
                    width: "70%",
                    className: "all"
                },
                {
                    target: 1,
                    className: "never"
                },
                {
                    target: 2,
                    width: "10%",
                    className: "min-tablet"
                },
                {
                    target: 3,
                    width: "10%",
                    className: "min-tablet"
                },
                {
                    target: 4,
                    width: "10%",
                    className: "all"
                },
                {
                    target: 5,
                    visible: false,
                    searchable: false,
                    className: "never"
                }
            ]
        });
       table_services
        .columns(1)
        .search('Services|Industry', true, false, false)
        .draw();
    
  • kthorngrenkthorngren Posts: 20,276Questions: 26Answers: 4,765

    You have serverSide: true. Your server script is responsible for performing the search. You will need to adjust your server script to support regex expressions. Are you using a Datatables supplied server side processing script?

    Or do you really need server side processing? How many rows of data do you have?

    Kevin

  • avasmatavasmat Posts: 6Questions: 2Answers: 0

    Gotcha. Yes, I'm using the Datatables supplied server side processing script (the only custom changes were to the array below). Server side processing is required; I have thousands of rows currently, which will reach tens of thousands, maybe more in the future.

    Do you happen to have any examples of how to implement regex server side?

    $columns = array(
    
        array(
            'db'        => 'title',
            'dt'        => 0,
            'formatter' => function( $d, $row ) {
                return '<a target="_blank" href="'.$row[7].'">'.$d.'</a>';
            }
        ),
        array( 'db' => 'unit', 'dt' => 1 ),
        array( 'db' => 'category', 'dt' => 2 ),
        array( 'db' => 'country', 'dt' => 3 ),
        array(
            'db'        => 'date',
            'dt'        => 4,
            'formatter' => function( $d, $row ) {
                return time_elapsed_string(date( 'Y-m-d g:i A', strtotime($d)));
            }
        ),
    
        array( 'db' => 'date', 'dt' => 5 ),
        array( 'db' => 'source', 'dt' => 6 ),
        array( 'db' => 'url', 'dt' => 7 ),
       array( 'db' => 'type', 'dt' => 8 )
    );
    
  • kthorngrenkthorngren Posts: 20,276Questions: 26Answers: 4,765

    Sorry I don't have any examples. Others may post some but these threads might give you some ideas:
    https://datatables.net/forums/discussion/47717/exact-match-regex-serverside-true-and-editor
    https://datatables.net/forums/discussion/61483/hints-for-a-better-search-experience-with-server-side-processing

    You might be able to find other threads as well.

    Kevin

  • avasmatavasmat Posts: 6Questions: 2Answers: 0

    Than you Kevin. Will look around! And check out the threads as well.

Sign In or Register to comment.