Custom column filter not returning any rows
Custom column filter not returning any rows
I have initialized a table and using the a particular column elements to update another column in the row from using an Ajax request. Since the ajax responses are late the datatable initializes the table and the column filters are not picking up the updated values. I added some custom select filters. This works till here.
But whenever I try to filter data on the table using the column filters, it gives and empty table back. Please help how I can get this working.
var sometable = $('#sometable').DataTable({
"initComplete": function () {
updateURT(); //fuction which gets the table update using ajax. Also call this in drawCallback
this.api().columns([0,1,3,7,8]).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();
},
'click': function(e) {
// stop click event bubbling
e.stopPropagation();
}
});
var tc=0;
var pp=0;
column.data().unique().sort().each( function ( d, j ) {
if(column.index() == 8){
select.append('<option value="OPTION1">OPTION1</option>'+
'<option value="OPTION2">OPTION2</option>'+
'<option value="OPTION3">OPTION3</option>'+
'<option value="OPTION4">OPTION4</option>'
)
}
console.log(d);
//console.log(+d+);
select.append( '<option value='+d+'>'+d+'</option>' )
} );
Edited by Colin - Syntax highlighting. Details on how to highlight code using markdown can be found in this guide.
Answers
I am new to this. Any help is really appreciated.
Probably the best thing to do is to build the
select
elements in yourupdateURT
function in theajax
method'ssuccess
function. In this example, the table is being initialised there, for you just move the code ininitComplete
there instead,Colin
Thanks Colin.
But doesn't that lead to initialization happening on every ajax call?
This is the updateURT function.
Edited by Kevin: Syntax highlighting. Details on how to highlight code using markdown can be found in this guide
Looks like you are updating the Datatable with a jQuery method:
Datatables won't know about these changes. You can use
cell().data()
to update both the table and Datatable's data cache. Or you can usecell().invalidate()
orrow().invalidate()
to have Datatalbes update its cache.Kevin
Thanks Kevin.
This is very promising. I will try this.
Could you please suggest how do I select the particular row and the cell to update the value using cell() as I am using the file_name to select/identify the row and the cell to be updated.
Take a look at the
cell-selector
docs for all the options. Looks like you can use something like this:Kevin
When I try to select the row using this : https://datatables.net/forums/discussion/66164/selecting-a-row-based-on-a-cell-value
I am getting an TypeError (TypeError: uploadtable.row(...).select is not a function)
Here is how I updated my fuction -
Table initialization:
I suspect this line:
should be
If that doesn't help, we're happy to take a look, but as per the forum rules, please link to a test case - a test case that replicates the issue will ensure you'll get a quick and accurate response. 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
Sure Colin. I will try to create a test case for this.
Just wanted to let you that this also resulted in the same error. However when I get the row like I was previously doing, it update the cells well. But I can see another error in the console and finally the filter don't work again either which was the actual issue.