custom search on a datatable within a page with multiple datatables

custom search on a datatable within a page with multiple datatables

itramitram Posts: 43Questions: 15Answers: 0
edited July 12 in DataTables 2

Link to test case:
Debugger code (debug.datatables.net):
Error messages shown:
Description of problem:
I have a web page containing multiple DataTables, and I've implemented a custom search/filter functionality using the following approach:

$.fn.dataTable.ext.search.push(function (settings, data, dataIndex) {
const tableId = settings.nTable.id;
// Custom filter logic goes here
});
This method works correctly; however, I noticed that the custom filter seems to iterate over all records in all DataTables on the page, which can be resource-intensive especially with large datasets.

To optimize this, I introduced an early return strategy based on the tableId to restrict the filter to a specific DataTable:

$.fn.dataTable.ext.search.push(function (settings, data, dataIndex) {
const tableId = settings.nTable.id;

// Apply the filter only to the desired DataTable
if (tableId !== searchableDataTableId) {
return true;
}

// Custom filter logic goes here
});
This approach improves performance by ensuring that the custom filter is only applied to the DataTable identified by searchableDataTable, thereby avoiding unnecessary iterations over other DataTables on the page.

However, is there a method available to apply the filter exclusively to the DataTable to which it is intended, and not loop other datatables?

This question has an accepted answers - jump to answer

Answers

  • kthorngrenkthorngren Posts: 21,443Questions: 26Answers: 4,974
    Answer ✓

    If you are using Datatbles 2.0 then search.fixed() might be a good option. Seems like it can have the same functionality as a search plugin. Otherwise with Datatables 1.x the search plugin is applied globally to all Datatables on the page. What you have is the correct way to limit which tables the plugin is applied to.

    Kevin

  • itramitram Posts: 43Questions: 15Answers: 0

    Thanks Kevin, it worked perfectly.

Sign In or Register to comment.