get first row information in datatable

get first row information in datatable

denizdianadenizdiana Posts: 15Questions: 8Answers: 0
edited April 2 in Free community support

Hello everyone, i want to get first row from datatable.I fill the datatable with json

  var data=  {
    "events": [
        {
            "name": "John",
            "date": "05-03-2020",
            "time": "18:10:11",
            "action": "enter"
        },
        {
            "name": "Jane",
            "date": "05-03-2020",
            "time": "08:40:11",
            "action": "enter"
        }]}

My datatable is :

       dtable = $('#table').DataTable(
                {

                 data : data.events,

        columns: [

          { data: 'name',  title: 'Person Name' },
          { data: 'action',  title: 'Event'  },
          { data: 'date',  title: 'Date' }
 ]

                });

I want to get name, date, time information from first row.

      var d = dtable.row(0).data();
      var name=d.name;

When i want to get name information from here i get an error. What is the problem? :( thank you so much.

Edited by Colin - Syntax highlighting. Details on how to highlight code using markdown can be found in this guide.

This question has an accepted answers - jump to answer

Answers

  • kthorngrenkthorngren Posts: 8,931Questions: 25Answers: 2,040

    Looks like it should work. What is the error? Can you provide a test case so we can take a look?
    https://datatables.net/manual/tech-notes/10#How-to-provide-a-test-case

    Kevin

  • denizdianadenizdiana Posts: 15Questions: 8Answers: 0

    I dont know why when i change it to
    var d = dtable.row(':eq(0)').data();
    it works :)
    Thanks Kevin.

  • kthorngrenkthorngren Posts: 8,931Questions: 25Answers: 2,040
    edited April 2 Answer ✓

    Not sure without seeing it. Using row(0) is the row index. The index is the order the table is loaded not the order of the table. If you want to get the row that is displayed in the top row, regardless of ordering, then use :eq(0).

    Kevin

  • colincolin Posts: 8,661Questions: 0Answers: 1,449

    Even with 0, it works here - so something odd going on.

    But, at least you've got a solution so all good.

    Colin

Sign In or Register to comment.