Column search exact match with Server-side processing
Column search exact match with Server-side processing
I had a DataTable with JSON source without serverside processing and the following code to provide column filters:
...
, initComplete: function () {
this.api().columns(2).every( function () {
var column = this;
var select = $('<select class="form-control" style="min-width: 150px;"></select>')
.appendTo( $('#drivergroup') )
.on( 'change', function () {
var val = $.fn.dataTable.util.escapeRegex(
$(this).val()
);
column
.search( val ? '^'+val+'$' : '', true, false )
.draw();
} );
column.data().unique().sort().each( function ( d, j ) {
select.append( '<option value="'+d+'">'+d+'</option>' )
} );
} );
...
Recently realized that the json source i was using became pretty large, slowing my load-time, so i moved to server-side processing where every page is loaded separately, but doing so made my column filters unusable. I figured that removing the regex in search() solves my problem:
...
column
.search(val)
.draw();
...
this, though, removed the ability to search only 100% matching results in the column, so for example if i select option '70' it also shows 7010, 7020, etc..
I am not sure why Regex search doesn't work with serverSide, because i'm not a code pro and haven't got any luck figuring it out, do you guys know any workaround?
Thanks in advance!
Answers
If you are using serverSide, then all the filtering should be part of your sql. If you want exact matches or like matches depends upon how you write your query.
select * From mydatabase where myfield = " passed value "
vs
select * From mydatabase where myfield like "%passed value%"
MySQL also has regex matching, but I use it rarely, and never for datatables, but that's another approach.
Well that's a little unsettling since i have lots of filters, but i guess it is the only way. Thank you!