1

I am using the jQuery DataTables plugin. I am trying to assign a parameter to my table, and use that parameter later in a custom search. I however cant find out how to store and get that parameter, and in my custom search i dont know how to get the table object. Here is my code to give a more clear idea of what i am trying to do:

jQuery.fn.dataTable.ext.search.push(
    function( settings, data, dataIndex ) {
        var myParam = ....;
        console.log('i said: '+ myParam)
       return true;
    }
);

var table = jQuery('#myTable').DataTable();
table.myParam = 'hello';
table.draw();

1 Answer 1

4
  • Use global variable:

    var myParam = '';
    
    jQuery.fn.dataTable.ext.search.push(
        function( settings, data, dataIndex ) {   
           console.log('i said: '+ myParam)
           return true;
        }
    );
    
    var table = jQuery('#myTable').DataTable();
    myParam = 'hello';
    table.draw();
    
  • Use data- property:

    jQuery.fn.dataTable.ext.search.push(
        function( settings, data, dataIndex ) {
           var table = new jQuery.fn.dataTable.Api( settings ); 
           var myParam = jQuery(table.table().node()).data('myParam');
    
           console.log('i said: '+ myParam)
           return true;
        }
    );
    
    var table = jQuery('#myTable').DataTable();
    jQuery(table.table().node()).data('myParam', 'hello');
    table.draw();
    
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for your answer. The data() seems a good idea, however it means that for each row check a new object will be created. Do you know if this has any impact on performance and on how to fix that? I am trying to avoid globals.
@user4493177, obviously with data- property method there are several extra functions that will be called in the loop. I'm not sure how that will impact performance, that will depend on the table size.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.