How to redraw table on custom keyup event?
How to redraw table on custom keyup event?
Camo
Posts: 33Questions: 6Answers: 0
Hi, I try to do some custom search for datatable, so according the cunstom range filter example I tried to call appDataTable.draw(); But it throws me an error: draw() is not a function. If I remove it, it listen on press enter and it works, but it also reorder the table (cause input is in the same th as order arrows). I need to call it on keyup. What am I doing wrong?
jQuery( document ).ready(function($) {
$('#datatable th input')
.on('click', function(e) { return false }) // Prevent reorder
.on('keydown', function(e) {
let code = e.keyCode || e.which;
if( code === 13 ) return false;
});
$('#datatable th input').on('keyup', function(e) {
appDataTable.draw(); // This is from documentation but throws me an error.
});
$.fn.dataTable.ext.search.push(
function( settings, data, dataIndex ) {
var result = true;
var codeInputVal = $('#code').val();
var valueVal = $('#value').val();
var typeVal = $('#type').val();
if( valueVal && parseInt(data[2]) !== parseInt(valueVal) ) result = false;
else if( typeVal && data[1].indexOf(typeVal) < 0 ) result = false;
else if( codeInputVal && data[3].indexOf(codeInputVal) < 0 ) result = false;
return result;
}
);
});
Answers
This was caught by the spam filter - apologies for that. I suspect the problem is just the scope of the
appDataTable
variable - it was probably declared in a function or in a place that thekeyup
function doesn't have access to.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
Hi, thanks for help. I made the example http://live.datatables.net/zoduqapi/1/edit There it is: draw is not a function in console. The scope should be ok.
...
Ok so the problem is in dataTable() call on init. It has to be Capitalize DataTable().
https://stackoverflow.com/questions/25207147/datatable-vs-datatable-why-is-there-a-difference-and-how-do-i-make-them-w
uf...
Ah yep, that'll do it!
Colin