this.footer() returns NULL?
this.footer() returns NULL?
I have a table that uses dataTables in AJAX mode, with queries being performed server-side for every operation (search, sort, etc). It works fine.
I am now trying to set up individual column searching via text inputs on specific columns. Based on the example given in the official docs (https://www.datatables.net/examples/api/multi_filter.html), I arrived at this code:
table.columns('.search-sort').every(function () {
var that = this;
console.log(this.footer()); // returns null
$('input', this.footer()).on('keyup change', function () {
if (that.search() !== this.value) {
that.search(this.value).draw();
}
});
});
(The input fields are already in the HTML code; obviously the datatable initialization is in another part of the code (it's rather complex).)
There are four .search-sort cells in the footer of the table.
My problem: the above code results in the eventlistener (keyup, change) being attached to all fields in the page, and not only the fields in the .search-sort cells. (Furthermore, each eventListener is attached four times to each field --as many as the number of search fields.)
As commented in the code above, this.footer() outputs "null" when displayed via console.log. Since this.footer() is supposed to provide a context for the jQuery selector, the fact that it returns null explains why the eventlistener are attached everywhere instead of only the current column's footer input.
So my question would be: why doesn't "this.footer()" seem to work? Any lead?
Thanks in advance.
(Of course there is a <tfoot> element in the html and it mirrors the number of columns in the <thead>)
This question has an accepted answers - jump to answer
Answers
I put together this example:
http://live.datatables.net/vuruhosa/1/edit
I put the
.search-sort
class on two header columns. This is for thetable.columns('.search-sort').every(function ()
part to work. I also put it on the same footers and adjusted the example's selector for the code that adds the text inputs, for example:$('#example tfoot .search-sort').each( function () {
This seems to work.
Kevin
Thanks for taking the time to test that. One important difference is that my version works on ajax (server-side search), but I don't know how much this changes things. This makes it difficult to set a public live example (my code is behind a login form).
Below is a more complete version of the code that I use (including HTML). I cleaned it up a little for the sake of readability. (Most notably, I removed the "rowCallback" and "drawCallback" functions, but first I checked that removing those functions has no impact on my issue).
As you can see, the problem really boils down to this.footer() returning NULL when it is used; if I use a different jQuery selector, everything works as it is supposed to work (see comments in code).
So my question really is what could cause this.footer() to not work as intended... ?
HTML:
JAVASCRIPT:
Move your search code into
initComplete
. The issue is that since you are using Ajax, the table hasn't finished initialising (i.e. using ajax makes the DataTable also async).Allan
Ah, yes, that was exactly it! Problem solved, thanks a lot!