Use search plug-in on only one table with a page with multiple tables

Use search plug-in on only one table with a page with multiple tables

torpletorple Posts: 5Questions: 1Answers: 0

What is the best practice to handle a search plug-in needed for a single table on a page with multiple tables? My plan is to implement something like

$.fn.dataTable.ext.search.push(
    function( settings, searchData, index, rowData, counter ) {
        if (settings.sTableId !== 'my-table-id') {
            return true;
        }
        ...
    }
);

This should ignore my other tables, correct? Is there a better way to address this?

This question has an accepted answers - jump to answer

Answers

  • kthorngrenkthorngren Posts: 21,343Questions: 26Answers: 4,954
    Answer ✓

    This thread should answer your question:
    https://datatables.net/forums/discussion/comment/76746/#Comment_76746

    Kevin

  • torpletorple Posts: 5Questions: 1Answers: 0

    That's great information, thank you. It appears I'm on the right track, and I can see the plugin function being called.

    However, when my next datatable loads, my plugin function has been removed from $.fn.dataTable.ext.search. Looking through the code, it appears global settings are reinitialized with every new table. Is this correct? If so, I can make sure the plugin is set when my target table in use, but I want to verify before I go down that path.

  • torpletorple Posts: 5Questions: 1Answers: 0

    Here is what I am working with:

    var mySearch = function(settings, searchData, rowIndex, data, counter) {
        ...
    };
    
    
    $container.on('preDraw.dt', '#my-table', function() {
        if (!$.fn.dataTable.ext.search.includes(mySearch)) {
                $.fn.dataTable.ext.search.push(mySearch);
        }
    

    Making sure my plugin function is set, along with returning true for any table that's not the table in question, looks to be the way to go.

This discussion has been closed.