How can I filter the table using an external button/function?

How can I filter the table using an external button/function?

Ragos1027Ragos1027 Posts: 4Questions: 2Answers: 0
edited July 2021 in DataTables 1.10

Hi. This seems really simple but I'm having troubles.
Right now I have a few buttons declared within the table, like this:

buttons: [
                        {
                            text: 'Todos',
                            action: function (e, dt, node, config) {
                                dt.column(7).search("").draw();
                            }
                        },
                        {
                            text: 'Completados',
                            action: function (e, dt, node, config) {
                                dt.column(7).search("Completado").draw();                            
                            }
                        },
                        {
                            text: 'Pendientes',
                            action: function (e, dt, node, config) {
                               dt.column(7).search("Pendiente").draw();
                            }
                        }
                    ],

I use them to filter the table based on those given conditions.

Now I need to declare and call a function OUTSIDE the datatable declaration, that also filters the table (based on a condition that I'll decide later).

How can I do it?

Thanks in advance.

Edited by Colin - Syntax highlighting. Details on how to highlight code using markdown can be found in this guide.

This question has an accepted answers - jump to answer

Answers

  • colincolin Posts: 15,112Questions: 1Answers: 2,583

    We're happy to take a look, but as per the forum rules, please link to a test case - a test case that replicates the issue will ensure you'll get a quick and accurate response. Information on how to create a test case (if you aren't able to link to the page you are working on) is available here.

    Cheers,

    Colin

  • Ragos1027Ragos1027 Posts: 4Questions: 2Answers: 0

    Hi Colin, thanks for the answer (and sorry for the delay)

    I wouldn't know what to write in the test case (never wrote one), because my doubt is exactly something I have no idea how to write.

    I just want to write a Javascript function that filters the table depending on the value of a certain column. The parameter of said function would be the value.

    I want this:

           dt.column(7).search("").draw();
    

    but on a Javascript function OUTSIDE my datatable declaration, which is:

    $('#tableName').dataTable( {

         buttons: [
               something...
               action: function(....) {
                           dt.column(7).search("").draw();
               }
         ]
    

    });

    If you still need a test case it's fine. Thanks in advance

  • kthorngrenkthorngren Posts: 20,141Questions: 26Answers: 4,736
    Answer ✓

    Does this example help?
    http://live.datatables.net/bexukuka/1/edit

    If you still need help then please update the test case showing what it is you are trying to do. Or provide a more detailed description of the solution you are trying to acheive.

    Kevin

  • Ragos1027Ragos1027 Posts: 4Questions: 2Answers: 0

    That looks exactly like what I need. Thank you so much!

Sign In or Register to comment.