Individual column filtering
Individual column filtering
horus1613
Posts: 19Questions: 6Answers: 0
Hi.
I want to do a select filter columns: 4,25,31.
My code:
initComplete: function () {
var ik = 0;
while (ik <= 31) {
if ((ik == 4) || (ik == 25) || (ik == 31)) {
var column = this.api().column(ik);
console.log(column.search())
var select = $('<select><option value=""></option></select>')
.appendTo( $(column.footer()).empty() )
.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 ) {
if( column.search() == d){
select.append( '<option selected value="'+d+'">'+d+'</option>' );
}
else{
select.append( '<option value="'+d+'">'+d+'</option>' );
}
} );
}
ik++;
}
},
But it doesn't work if two or more columns are selected at the same time...
I'm depressed...
Help me, please.
PS: and i don't know how to do so that these "selects" don't get into excel.
This question has an accepted answers - jump to answer
This discussion has been closed.
Answers
Hi @horus1613 ,
I don't understand what you mean by "it doesn't work if two or more columns are selected at the same time". Those searches will be ANDed together, so both conditions will have to match.
Cheers,
Colin
@colin
if I select 2 "select", an empty table is always issued
@horus1613 , is that because no rows match those two search terms? As I said before, they're ANDed together.
It's probably worthwhile if you create a 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
@colin
https://jsfiddle.net/horus1613/43mvnw8y/18/
Here's a modified version of that fiddle which works. The way you were using the
columns
variable meant you were always searching on the final column.C
@colin
thanks, it works!