Steve Steve - 4 months ago 18
jQuery Question

Jquery Datatable search

I've created a datatable using Jquery Datatable.

$('#mytable').dataTable({
"pagingType": "simple_numbers",
"bLengthChange": false,
"iDisplayLength": 10,
"aaSorting": [[0, 'asc']],
"processing": true,
"serverSide": true,
"ajax": $('#mytable').data('source'),
"aoColumnDefs": [
{'bSortable': false, 'aTargets': [3, 4]}
]


});

But How can I search on the data displayed on the table?

Answer
var dataTable = $('#mytable').dataTable();

  $('#yourInputFieldID').on('input keyup paste', function () {
    dataTable.fnFilter(this.value);
  });

// To prevent submit on enter we need to prevent default behaviour of Enter key on keydown event

$('#yourInputFieldID').keydown(function (event) {
    if (event.keyCode === 13) {
      event.preventDefault();
      return false;
    }
  });
Comments