javascript - Customizing the jquery.datatables plugin to filter with a regex on two columns -
i using jquery datatables plugin found here. there 2 columns named "new gl" , "old gl". datatables plugin has built in feature called search filters data in table based on keyword enter textbox.
when type "40.88.1010.0" or "40.88.600000.05" textbox want record appear. 
the same rule applies of records. want search continue filter of columns using 1 single textbox columns "new gl" , "old gl"...i want user able use both "-" , "." characters.
my initial thought if datatables had regex option work because "." in regex character. want rule apply 2 columns.
is possible using datatables plugin?
here current datatable initialization code:
$(document).ready(function () { var table = $('#datatable').datatable({ "idisplaylength": 50 }); });
solution
you need implement custom search function shown below.
the drawback of method content in other columns searched in same manner, i.e. searched using either . or - characters.
$(document).ready(function () { var table = $('#datatable').datatable({ "pagelength": 50 }); $('.datatables_filter input', table.table().container()) .off('.dt') .on('keyup.dt cut.dt paste.dt input.dt search.dt', function (e) { // if length 3 or more characters, or user pressed enter, search if(this.value.length > 0){ // escape expression can perform regex match var val = $.fn.datatable.util.escaperegex(this.value); // add regular expression match both . , - characters val = val.replace(/(\\\-|\\\.)/g, "[\\\.\\\-]"); // call api search function table.search(val, true, false).draw(); } else { // ensure clear search if backspace far enough table.search("").draw(); } }); }); demo
see this jsfiddle code , demonstration.

Comments
Post a Comment