Dynamic Selects - Adjusting Search Parameter to filter for ALL fields (or specific ones).
Dynamic Selects - Adjusting Search Parameter to filter for ALL fields (or specific ones).
aehrenwo
Posts: 3Questions: 2Answers: 0
I am trying to figure out how to amend this code so that I can have the dropdowns filter against "all fields" and not just the one that they were originally bound to based on the data on that column.
For example, I have a column called Sector (7 in the code) and I would like if someone selects that particular dropdown to have it search if ANY fields have that string.
Here is the code I used to create the original selects on the columns.
initComplete: function () {
this.api().columns([4, 6, 7, 8, 9]).every( function () {
var column = this;
var select = $('<select><option value=""></option></select>')
.appendTo( $(column.header()) )
.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>' );
} );
} );
}
This discussion has been closed.
Answers
Hi @aehrenwo ,
If you want to search against all fields, then you just need to change that line 10, where it's doing a column search with
column().search()
, you would just do a table search instead,search()
, so you would changeto be
Cheers,
Colin
This seems to make ALL of the dropdowns stop working. I want it to try to find a match in any of the fields? Any suggestions?
Hi @aehrenwo ,
We're happy to take a look, but it would help, as per the forum rules, if you could link to a running test case showing the issue so we can offer some help. Information on how to create a test case (if you aren't able to link to the page you are working on) is available here.
Cheers,
Colin