access row data

access row data

aziegler3aziegler3 Posts: 53Questions: 12Answers: 1

If I have something like:

    table.on( 'select', function ( e, dt, type, indexes ) {
        if ( type === 'row' ) {
            blah, blah
        }
    } );

how can I access the values of the data on the row that was selected?
I tried:

table.row(this).data()

but it comes undefined. Not sure why.

This question has accepted answers - jump to:

Answers

  • kthorngrenkthorngren Posts: 21,840Questions: 26Answers: 5,049

    Depending on your configuration more than one row can be selected. The indexes parameter contains an array of indexes of the selected items. See the examples in the select event docs to learn how to access the selected rows. In addition take a look at these examples:
    https://datatables.net/extensions/select/examples/api/get.html
    https://datatables.net/extensions/select/examples/api/events.html

    Kevin

  • aziegler3aziegler3 Posts: 53Questions: 12Answers: 1

    Thank you Kevin. I figured a way of making this work and access the values via table.cell(index,column).
    I guess, my question remains, more like rhetorical: why table.row(this).data() is null in table.on('select'....
    Just a curiosity

  • aziegler3aziegler3 Posts: 53Questions: 12Answers: 1

    Thank you Kevin. I figured a way of making this work and access the values via table.cell(index,column).
    I guess, my question remains, more like rhetorical: why table.row(this).data() is null in table.on('select'....
    Just a curiosity

  • kthorngrenkthorngren Posts: 21,840Questions: 26Answers: 5,049
    Answer ✓

    Go to this example and paste the following code into the console:

    $('#example').DataTable().on('select', function (e, dt, type, indexes) {
        console.log(this);
    });
    

    You will see this is the table. This makes sense as one element can be selected, multiple elements can be selected and there is the select() API.

    Kevin

  • allanallan Posts: 64,230Questions: 1Answers: 10,599 Site admin
    Answer ✓

    Yup, in DataTables this is the table element (I do wish it was the API, but that's a legacy thing...). It is not the table row, which it sounds like you were expecting @aziegler3.

    The indexes parameter passed in gives you the data indexes of the row(s) that were selected, so you can do:

    table.rows(indexes).data();
    

    Allan

  • aziegler3aziegler3 Posts: 53Questions: 12Answers: 1

    Thank you guys. This is helpful to understand. I like to understand how things work.

Sign In or Register to comment.