After Global Search sorting is Failed
After Global Search sorting is Failed
Hasanshali
Posts: 4Questions: 1Answers: 0
I'm using global search after filter the value sorting is reloaded not working for filter data. Pls see my code.
# JavaScript Code
`
$(document).ready(function () {
var oTable = $('#tbl').dataTable({
"bPaginate": true,
"bLengthChange": true,
"bFilter": true,
"bSort": true,
"bInfo": true,
"bAutoWidth": true,
});
$('#globalSearchText').unbind();
$('#globalSearchText').bind('keyup', function (e) {
if (e.keyCode == 13) {
var searchTerm = this.value.toLowerCase();
$.fn.dataTable.ext.search.push(function (settings, data, dataIndex) {
//search only in column 1 and 2
if (~data[0].toLowerCase().indexOf(searchTerm)) return true;
if (~data[1].toLowerCase().indexOf(searchTerm)) return true;
if (~data[2].toLowerCase().indexOf(searchTerm)) return true;
if (~data[5].toLowerCase().indexOf(searchTerm)) return true;
return false;
})
oTable.fnDraw();
$.fn.dataTable.ext.search.pop();
}
});
});
~~~~`
This question has an accepted answers - jump to answer
This discussion has been closed.
Answers
Your second screenshot shows "id" sorted in descending order. So probably you didn't need to re-sort at all.
Maybe I'm misunderstanding the question. A better explanation might help.
Don't do this:
Inside a key event handler! That will add the filter every time a key is pressed and its going to kill performance!
Just do it once
Allan
Allan@ I'm using for this functionality ($.fn.dataTable.ext.search.push) search by particular column only.
tangerine@ After global search exact data is filtering but on that time I clicked Id for sorting Table is reloaded not working sorting for global search data.
Sure. But you are adding it every time the return key is pressed. That's going to be very bad for performance.
Allan
Thanks Allan Its working I removed $.fn.dataTable.ext.search.pop();