JQuery Datatables search within input and select

It is not very well documented. And it seems to work differently, or not work at all, between (sub)versions. I think dataTables is intended to automatically detect HTML-columns, but for some reason, most of the times, it doesnt. The safest way is to create your own search-filter :

$.fn.dataTableExt.ofnSearch['html-input'] = function(value) {
    return $(value).val();

This will return 33 on <input>‘s with value 33, and Tokyo on <select>‘s where Tokyo is selected. Then define the desired columns as of type html-input ;

var table = $("#example").DataTable({
    columnDefs: [
       { "type": "html-input", "targets": [1, 2, 3] }

see demo based on http://datatables.net/examples/api/form.html -> http://jsfiddle.net/a3o3yqkw/

Regarding live data: The issue is, that the type based filter only is called once. dataTables then caches the returned values so it not need to “calculate” all the values over and over. Luckily, dataTables 1.10.x has a built-in function for cells, rows and pages called invalidate that forces dataTables to reset the cache for the selected items.

However, when dealing with <input>‘s there is also the problem, that editing the value not is changing the value attribute itself. So even if you call invalidate(), you will still end up in filtering on the old “hardcoded” value.

But I have found a solution for this. Force the <input>‘s value attribute to be changed with the <input>‘s current value (the new value) and then call invalidate :

$("#example td input").on('change', function() {
  var $td = $(this).closest('td');
  $td.find('input').attr('value', this.value);

For textareas use text() instead :

$("#example td textarea").on('change', function() {
  var $td = $(this).closest('td');

This is also the case when dealing with <select>‘s. You will need to update the selected attribute for the relevant <option>‘s and then invalidate() the cell as well :

$("#example td select").on('change', function() {
  var $td = $(this).closest('td');
  var value = this.value;
  $td.find('option').each(function(i, o) {
    if ($(o).val() == value) $(o).attr('selected', true);

forked fiddle -> http://jsfiddle.net/s2gbafuz/ Try change content of the inputs and/or the dropdowns, and search for the new values …

Leave a Comment