How do I access the data in a row?

How do I access the data in a row?

1DMF1DMF Posts: 16Questions: 5Answers: 0
edited July 2014 in Free community support

Hi,

I seem to be going round in circles unable to find information on how I get the data out of a datatables object.

I have a row passed in to me from :

myTable.on( 'click', 'tr', function () {
myFunction($(this));
}

None of the table columns have any HTML5 'data' attributes, no identifying class, there seems nothing I can use to say get me the value for column labelled 'Status'.

$(this) is just a table row element with a bunch of child table data elements with nothing to identify them?

If I assigned column names when initialising the DataTable

"columns": [
{ "data": "FPID" },
{ "data": "Promo_Desc" },
{ "data": "Publication" },
{ "data": "Period" },
{ "data": "Sub_Date" },
{ "data": "Status" }
],

And I now have a selected row, how do I access the actual data by their JSON object names?

Thanks,
1DMF

This question has an accepted answers - jump to answer

Answers

  • 1DMF1DMF Posts: 16Questions: 5Answers: 0

    OK, I finally managed to get to some data via...

    myDataTable.row($(this)).data().myJSONKey

    ie.

    myDataTable.row($(this)).data().Status

    or

    myDataTable.row($(this)).data().Sub_Date

    etc..

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    Answer ✓

    Have you tried using the row().data() method?

    Allan

  • 1DMF1DMF Posts: 16Questions: 5Answers: 0

    Isn't that what I posted I am using?

    Sorry, I'm confused can you elaborate on what you mean if

    .row($(this)).data().

    isn't using row().data() method?

  • gamepawngamepawn Posts: 1Questions: 0Answers: 0

    I was having a problem with getting data too...until i changed:

    var table = $('#example').dataTable();

    var table = $('#example').DataTable();

    ^with a capital D

    Then out of nowhere, this worked:

    $('#example tbody').on( 'click', 'button', function () {
        var data = table.row( $(this).parents('tr') ).data();
        alert( data[0] );
         } );
    

    Maybe you have the same issue?

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin

    @1DMF - Apologies, I read your code incorrectly. Looks like you've got it working from your second post :-)

    Allan

This discussion has been closed.