Prevent row deselection

Prevent row deselection

ILyaCycloneILyaCyclone Posts: 9Questions: 2Answers: 0

Hello.
I'm using DataTables with Select extension.
select: {style: "single"}
So user selects a row by clicking on it.
I don't want the row to be deselected when user clicks on it again. Just do nothing.
Any informaton on how to stop deselect event?

"datatables.net": "^1.10.16",
"datatables.net-bs": "^2.1.1",
"datatables.net-select-bs": "^1.2.3",

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,457Questions: 1Answers: 10,466 Site admin
  • allanallan Posts: 63,457Questions: 1Answers: 10,466 Site admin

    Use the user-select event to prevent Select taking actions. Currently there isn't a built in mode in Select that will force there always to be a row selected.

    Allan

  • ILyaCycloneILyaCyclone Posts: 9Questions: 2Answers: 0

    What would be the proper way to tell "deselect" event from "select" in "user-select" handler?

  • allanallan Posts: 63,457Questions: 1Answers: 10,466 Site admin
    Answer ✓

    Select itself hasn't run its logic to determine if it should select or deselect at the point the user-select event runs. What you would need to do is check to see if the row(s) clicked on are selected or not - e.g.:

    table
        .on( 'user-select', function ( e, dt, type, cell, originalEvent ) {
            var row = dt.row( cell.index().row ).node();
            if ( $(row).hasClass('selected') ) {
                // deselect
            }
            else {
                // select
            }
        } );
    

    Allan

  • ILyaCycloneILyaCyclone Posts: 9Questions: 2Answers: 0

    Perfect. Works fine, thank you very much.

This discussion has been closed.