Initial filter set
Initial filter set
Hi all,
I'm working on a initialization of the table filter. Filter values are to be passed in URL. The function looks like that:
function init_table_filter(table) {
var params = new URLSearchParams(window.location.search);
params.forEach((value, key) => {
var column = table.api().column(key + ':name');
$('td:nth-child(' + (column.index() + 1) + ') input',
$(column.header()).closest('thead'))
.val(value).trigger('change');
$('td:nth-child(' + (column.index() + 1) + ') select',
$(column.header()).closest('thead'))
.val(value).trigger('change');
});
}
Filter applying function is defined like that:
function init_search_input(target, column) {
$(target).on('keyup change clear', function () {
if ( column.search() !== this.value ) {
column
.search( this.value )
.draw();
}
});
}
I made sure the columns where initial filter can be set have name
property. When the page is loaded I see the filter value is set to the search field and change
event is triggered and event handler is called. However no filtering occurs.
If I type filter value after the page is loaded filtering works just fine.
How can I enforce it at page load?
Answers
This blog post here should help, it's discussing this issue.
Colin
So if I understand it correctly the idea is to initialize search in table creation object, not later? I can deal with initialization of search fields separately. And as for column wise filtering that can be initialized in same way?
Ok, I figured out what was wrong with my code.
I had search initialized like this:
The
init_search()
function initializes column wise search with inputs for each column:The problem was that
init_search()
function executed asynchronously andinit_table_filter()
didn't wait for one. So attempt to set initial filter was made before the searching capability was actually established.So I've remade search capability initialization this way:
and then set initial filter this way:
Excellent, thanks for reporting back and showing the solution,
Colin