How to limit the search to "equals" and not "like" at the time of initialization?

How to limit the search to "equals" and not "like" at the time of initialization?

kunwarkunwar Posts: 2Questions: 1Answers: 0

I am using DataTables 1.10.25 with server-side processing, I am trying to limit a column to display items matching a specific value for that column. I tried it with search builder but later found out that it's not supported yet. So I moved on to using searchCols. It got me close to what I wanted to do but the problem is that it matches the keyword with "Like" and so it brings back all similar results as well. I wanted it to be matching the keyword exactly. The column is an integer so if I search for 2 it would bring back results for 2, 22, and 12 as well. I tried using regular expression and it didn't work at.

Here's how I am using it:

    searchCols: [
        null,
        null,
        { sSearch: '2'},
        null,
    ],

Answers

  • colincolin Posts: 15,240Questions: 1Answers: 2,599

    That's right, the supplied server-side scripts don't support regexes for performance reasons. However, there are a few threads on this forum. such as this one, that offer solutions,

    colin

  • kunwarkunwar Posts: 2Questions: 1Answers: 0

    Here's the work around that I created:

    I created a copy of filter method in the SSP class as customFilter and changed a certain fragment to this code below:

                if ( $requestColumn['searchable'] == 'true' && $str != '' ) {
                    if(!empty($column['db'])){
                        if(str_starts_with($str, '=')){
                            $str = ltrim($str, '=');
                            $binding = self::bind( $bindings, $str, PDO::PARAM_STR );
                            $columnSearch[] = "`".$column['db']."` = ".$binding;
                        }else{
                            $binding = self::bind( $bindings, '%'.$str.'%', PDO::PARAM_STR );
                            $columnSearch[] = "`".$column['db']."` LIKE ".$binding;
                        }
                    }
                }
    

    And then passed the number as { sSearch: '=12'}. This got the job done but now I want to change the Jquery code to recognize some like { sSearch: '12' , matchType: "equals"} so that it gets passed to $requestColumn variable on the server side. Can anyone point me to the part that I need to modify? Also, would it be a good idea to commit my addition to the repo?

  • allanallan Posts: 63,498Questions: 1Answers: 10,470 Site admin

    You would use ajax.data to modify the data structure DataTables sends to the server.

    Including it with the script - it is something that we'll consider. There are a few other options such as startsWith that we've been thinking about as well, and this would fall under that umbrella.

    Allan

Sign In or Register to comment.