Quantcast
Channel: Editor — DataTables forums
Viewing all articles
Browse latest Browse all 3740

Exact match regex, serverSide:true, and Editor

$
0
0

I'm working on an autocomplete filter for a column where I'd like the table to filter on an exact match when the user "selects" a specific entry from the autocomplete.

Consider this page, the scoreboard: http://cfbha.org/w/Special:SeasonHome?view=scoreboard

If I type in "Georgia" into the school filter, I get Georgia, Georgia Southern, Georgia State, and Georgia Tech in the autocomplete and West Georgia shows up in the table as well, which is fine.

If I want only the Georgia games themselves, then I select "Georgia" from the autocomplete by either clicking on it or arrow-keying to it and pressing enter.

"Selecting" an actual option from the autocomplete adds "^" to the front and "$" on the back, uses the DataTables search with regex = true, and reduces the table just to the Georgia games themselves.

Perfect!

However, on another page I have a table with 645,000 rows so I use serverSide: true: http://cfbha.org/w/Special:DatabaseHome?view=tn_g

Typing "Georgia" here gives Georgia, Georgia Southern, Georgia Southwestern, Georgia State, Georgia State College, and Georgia Tech in the autocomplete while all those plus West Georgia, West Georgia JC, and even tiny Middle Georgia State show up in the table. All good so far.

But if I select "Georgia" from the autocomplete here then the table returns no rows, partially because I've added the "^" and "$" but mostly because server side processing doesn't support regex.

I've scoured the forums and continually see that I have to account for this on the server side script.

I'm using Editor to retrieve the records, so it seems like a ->where() condition would be applied when an option from the autocomplete is "selected".

I also know which fields to do the exact match on because the search terms have had "^" and "$" appended to them on the client side.

Using $_POST['columns'], I've made a function to build the ->where() condition based on which columns have been passed a search value with the "^" and the "$":

    ->where( function ( $q ) {
        $columns = $_POST['columns'];
        $filters = 0;
        foreach ($columns as $column) {
            $columnName = $column['name'];
            $searchString = $column['search']['value'];
            if (substr($searchString, 0, 1) == '^' && substr($searchString, -1) == '$') {
                ++$filters;
                $actualSearchString = substr($searchString, 1, -1);
                if ($filters == 1) {
                    $q->where( $columnName, $actualSearchString);
                } else {
                    $q->and_where( $columnName, $actualSearchString);
                }
            }
        };
    })

However I get no results returned.

It appears the query is being built properly. I can do a var_dump($q) and it looks identical to simply using ->where('team_name', 'Georgia').

However I suspect somewhere Editor is still referring back to that initial passed string of "^Georgia$" and trying to filter on that.

Am I correct, and if so how can I catch and update that $_POST['columns'] before it gets passed?

Or if I'm not correct where should I be looking?


Viewing all articles
Browse latest Browse all 3740