Get the selected value of the checkbox

Get the selected value of the checkbox

lenamtllenamtl Posts: 265Questions: 65Answers: 1

Hi,

I'm using Select and jquery-datatables-checkboxes https://github.com/gyrocode/jquery-datatables-checkboxes

I'm retriving the selected value like this
var tempContactId = table52.rows( { selected: true } ).data().pluck(2).toArray();

I'm wondering is there another way using Datatables function to get the same result, I'm wondering if there is a better way or this code is just ok?

Note I'm not using server-side process.

Thanks

This question has an accepted answers - jump to answer

Answers

  • kthorngrenkthorngren Posts: 21,172Questions: 26Answers: 4,923

    That is the way to get an array of selected rows where the array contains the values in column 2. Are you trying to achieve something different?

    Kevin

  • gyrocodegyrocode Posts: 126Questions: 6Answers: 30
    edited August 2020

    If column with index 2 is the column containing checkboxes, you could use:

    table.column(2).checkboxes.selected()
    

    For all other columns I thought you could use selector-modifier as shown below, however it doesn't work in my tests, could be a bug in the DataTables possibly ?

    table.column(2, { selected: true }).data()
    

    I created an example where column().data() doesn't work as expected, maybe Allan can take a look at it.


    See more articles about jQuery DataTables on gyrocode.com.

  • kthorngrenkthorngren Posts: 21,172Questions: 26Answers: 4,923

    table.column(0, { selected: true } ).data();

    This won't work with row selection. You need to use rows() when selecting by rows.

    Kevin

  • gyrocodegyrocode Posts: 126Questions: 6Answers: 30
    edited August 2020

    Maybe then documentation is not 100% correct, since column() API method documentation states that selector-modifier can be used for operations which involve rows.


    See more articles about jQuery DataTables on gyrocode.com.

  • kthorngrenkthorngren Posts: 21,172Questions: 26Answers: 4,923
    edited August 2020

    Maybe then documentation is not 100% correct,

    Could be. Allan can comment on that part.

    Maybe Allan's answer in this thread will help explain why it doesn't work as expected.

    Kevin

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

    It is working as expected, though possibly counter-intuitively.

    By default, the Select extension will select rows - so when you use column() and selector-modifier {selected: true}, since no columns are selected, everything is returned.

    If you set select.items to columns, so you select the columns, then you do get what you expect - see example here.

    Hope that helps make it clearer. So for you, Kevin's suggestion of rows() is the way to go.

    Colin

  • lenamtllenamtl Posts: 265Questions: 65Answers: 1

    Hi @Colin you code example having an error, missing a semi colon...

  • lenamtllenamtl Posts: 265Questions: 65Answers: 1

    Thanks

This discussion has been closed.