Select and serverSide - selected rows are visualy kept after redraw, but Select forget them

Select and serverSide - selected rows are visualy kept after redraw, but Select forget them

jmxjmx Posts: 4Questions: 2Answers: 0

hello,

I'm using the serverSide option for a table, along with the select extension, and made my way to get back the selected rows after using the pager.
It uses an array outside DT to memorize the rowId (data.id below) of the selected rows as described here
https://datatables.net/examples/server_side/select_rows.html

'rowCallback': function( row, data ) {\
    if ( $.inArray(data.id.toString(), selected) !== -1 ) {
        $(row).addClass("selected");
        }
    };

and uses .on( 'select'/'deselect') methods rather than .on('click') to update the selected array.

BUT

visually it's ok for the user, but not for Select, I guess, since user cannot deselect a row with one clic as it should : this clic actually select the row, from a Select point of view.. :)
the second clic is ok, obviously, it deselects.

something in the callback above is missing to feed Select with the previously memorized rowIds.

What is the best practice ?
How can I trigger a real Select method to select() the row from the row Callback - if applicable ?
do I really need to create a dedicated array for this, can I use some Select internals to work with ? Or save it before a redraw ?

thank you for your time

jerome

This question has an accepted answers - jump to answer

Answers

  • colincolin Posts: 15,240Questions: 1Answers: 2,599
    Answer ✓

    Yep, you're only adding the class, so Select extension would have no idea it's been selected. Instead of adding the class, call row().select() to select the row again, or you can use rows().select() to select them all at once,

    Colin

  • jmxjmx Posts: 4Questions: 2Answers: 0

    I already tried this but well Im not fluent in js.. I don't know how to get the row instance back :

    'rowCallback': function( row, data ) {\
        if ( $.inArray(data.id.toString(), selected) !== -1 ) {
            row.select();
            }
        };
    > row.select is not a function
    
  • jmxjmx Posts: 4Questions: 2Answers: 0

    oh ok

    "rowCallback": function( row, data ) {
        if ( $.inArray(data.id.toString(), selected) !== -1 ) {
            mytable.row(row).select();
            }
        }
    

    many thanks Colin :)

    Jerome

This discussion has been closed.