Search code examples
datatablesdatatables-1.10yadcf

Is there a way to programatically extract all record IDs from a filtered (server-side) datatables recordset?


Using datatables in server-side mode, with YADCL for filtering, is there any way that I can programatically extract the record IDs (e.g. column 0 contains those IDs) for the entire filtered recordset (not just the paginated results shown) such that I can then carry out an action on that recordset.

Example: a 'customers' database table contains 80k records and I use YADCF filtering to narrow that down to a recordset of 1k customers, 20 of which are displayed on the web-page. I would like to have a button on the page, labeled 'Mark all filtered records', that when clicked, fires off an ajax script that changes the 'mark' field on the filtered 1,000 records from 0 to 1.

I can handle the marking of the records independently of datatables, but I don't know how to programatically work out which records need marking i.e. I need to extract some form of recordset identification that I can then use to target with my SQL UPDATE action. Any ideas anyone?


Solution

  • I had a similiar problem, here's what I did:

    • added a hidden input-field
    • used the drawCallback-option of Datatables to trigger a function that wrote the ids of all active (filtered) records to that input-field
    • on callbacks to the server, included the content of that input-field

    The tricky detail was the drawCallback-function:

    function (settings) {                                                              
    dtObj = this.api();                                                                
    var dtData = dtObj.column(0,{ search: 'applied', selected: true }).data().toArray();   
    $("#dtdata").val(JSON.stringify(dtData));                                          
    }                                                                                  
    
    • the first argument of that column-call is the index of the column you're interested in (0 has my index)
    • that 2nd argument selects all rows that matched search/filter-criteria. They also work with yadcf-filtering!