Reputation: 33
I'm using jquery DataTables, and I have a table with cells, each <td>
contains a <span>
and a hidden <select>
, now all I want is to filter on just the text inside the <span>
not the whole content of the <td>
which also contains the hidden <select>
element.
I'm using basic DataTables configuration:
$(document).ready( function () {
$('#table_id').DataTable();
} );
I've been trying for a couple of days now on this site, Datatables site , googling, but couldn't find an answer, so please help Thanks in advance
The code is generated on server, but the resulting table is something like this:
Please notice that: <select>
element is hidden with css
<tr>
<td>
<span>Text</span>
<select>
<option>option1</option>
<option>option2</option>
....
</select>
</td>
<td>
<span>Text</span>
<select>
<option>option1</option>
<option>option2</option>
....
</select>
</td>
</tr>
...
Upvotes: 3
Views: 3194
Reputation: 58900
You can use the code below to only search <span>
inside cells in specific columns. Please note that I've used "targets": [0, 1]
to target first and second column only based on your HTML code, adjust according to your needs.
$('#table_id').DataTable({
"columnDefs": [{
"targets": [0, 1],
"render": function ( data, type, full, meta ) {
if(type === 'filter'){
return $('#table_id').DataTable().cell(meta.row, meta.col).nodes().to$().find('span').text();
} else {
return data;
}
}
}]
});
Alternatively, you can use data-search
attribute on <td>
element to specify value used for filtering, then no additional initialization code is required. See below for an example:
<tr>
<td data-search="Text">
<span>Text</span>
<select>
<option>option1</option>
<option>option2</option>
....
</select>
</td>
<td data-search="Text">
<span>Text</span>
<select>
<option>option1</option>
<option>option2</option>
....
</select>
</td>
</tr>
See manual or example for more information on data-
attributes.
Upvotes: 4