Search and selectAll : select only searched rows

Search and selectAll : select only searched rows

teriblusteriblus Posts: 4Questions: 1Answers: 0

I wanted to add something on this thread : https://datatables.net/forums/discussion/9861
But it is closed !

So for those wondering how to select all searched rows, here is the code i use :

datatable.rows({search: 'applied'}).select();

If you need a button to select all, here is the code to add to the "buttons" option of the datatable:

buttons : [{
            text: 'Tout sélectionner', action: function (e, dt, node, config) {
                datatable.rows({search: 'applied'}).select();
}]

Replies

  • rf1234rf1234 Posts: 2,958Questions: 87Answers: 418

    Thanks for sharing!

    Maybe one hint: If you know the name of the Data Table you can of course use the name as you do in "datatable.rows..." but since "dt" is passed into the action function you can use "dt.rows ..." which doesn't require you to know the name of the Data Table beforehand.

    You can also make a reusable custom button from this called "selectAllSearchApplied".

    //custom button to select all rows of a data table
    $.fn.dataTable.ext.buttons.selectAllSearchApplied = {
        text: "the buttons label"
        action: function ( e, dt, button, config ) {
            dt.rows({ search: 'applied' }).select();
        }
    };
    

    And use it like this in any of your Data Tables without having to repeat its definition.

    buttons : [ "selectAllSearchApplied" ]
    

    And another one selecting just the first twenty rows:

    //custom button to select the first 20 rows
    $.fn.dataTable.ext.buttons.selectTwentyRows = {
        text: selectTwentyRowsLabel,
        action: function ( e, dt, button, config ) {
            dt.rows({ search: 'applied' }).every( function ( rowIdx, tableLoop, rowLoop ) {
                if ( dt.rows({ selected: true }).count() < 20 ) {
                    dt.row(this).select();
                }
            });
        }
    };
    
  • jpattersonjpatterson Posts: 8Questions: 1Answers: 0

    Thanks for this, @rf1234!

Sign In or Register to comment.