Filter search via button click...across numerous datatables

Filter search via button click...across numerous datatables

iSabieriSabier Posts: 8Questions: 0Answers: 0
edited July 2010 in General
Hello folks,
First off, a big round of applause from me to Allan for a magnificent plugin. It's helped make my project simpler. Thanks man.

I tweaked the plugin a bit by adding a new feature called "customFilter". It has a value of 1 or 0. If it's 0, it basically displays the default filter textfield. If it's 1, it replaces the default with a custom filter and a button. This way, I can decide when I want the filter to be fired dynamically or through a button click.

By default, I have set the value to 1 in the plugin itself so all my pages display the custom filter. However, I have come across a massive problem.

As you may have guessed, I can initiate the filtering by binding this command to the button.

[code]
tableobj = (table_id).datatable();
[/code]
[code]
$(custom_button_id).click(function() {
tableobj.fnFilter(custom_text_id.val());
});
[/code]

The problem is I have dozens of php files that have different table ids. My instructions are to not add the code above to all the phps.

You may have guessed my requirement by now. Rather than write this function in each php file, I wish to change this to a generic code within the plugin that can grab the table Id, instantiate the datatable and perform the filtering. How do I do this?

I appreciate any kind of advice. Thanks for helping me out.

Regards,
Sabier

Replies

  • iSabieriSabier Posts: 8Questions: 0Answers: 0
    Or rather....please tell me this at least.

    Which variable in the plugin stores the object "tableobj"? If that can be accessed directly in the js, I can simply write the code above in the plugin itself and not worry about accessing the table ID directly.
  • sd_zuosd_zuo Posts: 78Questions: 1Answers: 0
    With jQuery, you can grab the class of your tables rather than the id of them. If they share the same class name, it is not so difficult to capture them with the same code.
  • iSabieriSabier Posts: 8Questions: 0Answers: 0
    I know that but that's not what I'm looking for. :)
    To add a class right now is to change the files anyways which is a big no-no. What I seek is simpler.

    Somewhere in datatables.js is a variable that holds the table object i.e tableobj (see code above)

    All I need is access to that object so I can directly write the following in the js itself.
    [code]
    $(custom_button_id).click(function() {
    tableobj.fnFilter(custom_text_id.val());
    });
    [/code]

    That way I don't have to worry about grabbing the ID. If such a thing is possible, I'd be much obliged. Thanks.
  • sd_zuosd_zuo Posts: 78Questions: 1Answers: 0
    edited July 2010
    If the efficiency of matching by class is not a problem, you can grab the DataTables' wrapper class:
    [code]
    $("div.dataTables_wrapper table")....
    [/code]
    I don't know whether Allan agrees this :)
  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    Can you just do something like document.getElementsByTagName('table') and then filter down to the tables you want to make into a DataTable by checking for the ID and or class name? Then just pass that found node to jQuery and initialise the DataTable. In 1.7 betas it is also possible to obtain the DataTable object for a table by calling the dataTable function again on the same object, so you don't need a global reference (third point here: http://datatables.net/new/1.7 ).

    Allan
  • iSabieriSabier Posts: 8Questions: 0Answers: 0
    edited July 2010
    It's encouraging to hear the datatable object can be accessed in 1.7. Until then though... :(

    Initializing the table is not a problem at all. The trouble is I am supporting an application written by someone else and he initialized it in each php. I was hoping I could resolve it in the js itself by just accessing the object directly. Oh well, guess I gotta do it the hard way.

    But thanks to both of you for clearing it up.

    Regards,
    Sabier
This discussion has been closed.