Is it possible to specify that only some rows are selectable using the Select extension?

Is it possible to specify that only some rows are selectable using the Select extension?

taltal Posts: 5Questions: 3Answers: 0
edited April 2022 in Free community support

When using the select extension, with multi-select enabled, is there a simple way to specify that there are certain rows we don't want selectable?

I know there's a user-select event which we can use, but if we enable the checkbox column with

columnDefs: [
  {
    targets: 0,
    checkboxes: {
        selectRow: true
    }
  }
]

It will show a checkbox on every single row, even ones the user-select event will prevent from being selected.

When all rows are selectable, you don't necessarily need to tell the user which rows are selectable, but if only certain rows are selectable, it becomes important to have some way to tell them, like the checkbox column. Unfortunately, I can't get this column to only generate checkboxes for the rows that are selectable, and skip all the other rows.

Answers

  • rf1234rf1234 Posts: 2,950Questions: 87Answers: 416
    edited April 2022

    If you use the row checkbox with the "select" extension there should be an easy way to do this. Use "rowCallback" and for each row you don't want selectable replace the checkbox with something else using css.
    https://datatables.net/reference/option/rowCallback

    Alternatively you can try with the "select.selector".
    https://datatables.net/reference/option/select.selector

    I actually used a row selector in combination with rowCallback already. Here it is:

    First step: add a class to the rows you want selectable using rowCallback:

    rowCallback: function (row, data) {
        //if it is not the summation row the row should be selectable
        if ( data.cashflow.position !== 'L' ) {
            $(row).addClass('selectRow');
        }
    }
    

    Second step: add the row selector:

    select: {
        style: 'multi',
        selector: 'tr.selectRow td:not(:first-child)'
    },
    

    The above code makes all rows selectable except for those that don't have class "selectRow". The first column of each row generally is not selectable.

Sign In or Register to comment.