jQuery DataTable – Hide rows the intended way

You need to write a custom filter for that table. Example:

$.fn.dataTableExt.afnFiltering.push(function (oSettings, aData, iDataIndex) {
    if ($(oSettings.nTable).hasClass('do-exclude-filtering')) {
        return aData[16] == '' || $('#chkShowExcluded').is(':checked');
    } else return true;
});

In that example we dynamically add and remove the ‘do-exclude-filtering’ class to a table, and if it has the class, it checks each row to see if a given cell has a value. The logic can be anything you can dream up, just keep it fast (this is executed for every row, on every draw, for every table on the page (note it’s added to a ‘global’ array in DT, not an individual instance)

Return true to include the row, return false to hide the row

Here is the datatable reference to use the afnFiltering capabilities: http://datatables.net/development/filtering

The advantage to this instead of using .fnFilter() is that this works ALONG WITH, so the user can still use the filtering box in the top right (by default, I see yours is bottom right) to further filter the results you choose to show them. In other words, say you hide all ‘completed’ items, so the user only sees ‘incomplete’ items in the table. Then they can still filter the table for ‘laptop’ and see only the rows that are BOTH incomplete and have ‘laptop’ in their description

Leave a Comment