1

This is the data returned by ajax call.

{
  "totalRecords": 20,
  "draw": 2,
  "data": [
    {
      "DT_RowId": "row_1",
      "first_name": "Tiger",
      "last_name": "Nixon",
      "addInfo": [
        {
          "city": "Texas",
          "familyDetails ": {
            "name1 ": "Arxin",
            "name2 ": "Drav"
          }
        }
      ]
    },
    {
      "DT_RowId": "row_2",
      "first_name": "Garrett",
      "last_name": "Winters",
      "addInfo": [
        {
          "city": "Texas",
          "familyDetails ": {
            "name1 ": "Rog",
            "name2 ": "Arim"
          }
        }
      ]
    }
  ]
}

This is the data table initialization, which uses server side pagination.

$(document).ready(function() {
 $('#example').DataTable( {
        serverSide: true,
    paging:true,
        ajax: "../php/staff.php",
        columns: [
            { data: "first_name" },
            { data: "last_name" }
        ]
    } );
    
      $('#example tbody').on('click', 'tr', function () {
        var data = table.row( this ).data();
        alert( 'You clicked on '+data[0]+'\'s row' );
    } );
});

Here, I want to get the additional information on data row click other than thae data table row data. How can I get the json "addInfo" associated with each row.

Note: Found an option to add as hidden variable, but keeping json as hidden is tedious.

5
  • You can check this link stackoverflow.com/questions/37316811/… Commented Aug 17, 2021 at 4:44
  • I am able to get the information on row click. In above mentioned json response, "first_name", "last_name" information is retrieved. THis "addInfo" value which is not present in data table, how can I retireve that on row click. Commented Aug 17, 2021 at 4:49
  • You have to put data somewhere. Check this example datatables.net/examples/ajax/null_data_source.html Commented Aug 17, 2021 at 4:56
  • As per the requirement I dont want to add any column extra for this. Is there any other way to achieve this without adding a click or extra column. Commented Aug 17, 2021 at 5:01
  • You don't want any extra column or hidden column. In my knowledge i can't say it is possible or not. Commented Aug 17, 2021 at 5:04

1 Answer 1

1
+50

It's actually easier than you think, you can bind whole row data into it by using data: {}

$(document).ready(function() {
 $('#example').DataTable( {
        serverSide: true,
    paging:true,
        ajax: "../php/staff.php",
        columns: [
            { 
                data: {}, render: function (data) {
                    return data.first_name;
                }
            },
            { data: "last_name" }
        ]
    } );
    
      $('#example tbody').on('click', 'tr', function () {
        var data = table.row( this ).data();
        alert( 'You clicked on '+data.addInfo[0].city+'\'s row' );
    } );
});
Sign up to request clarification or add additional context in comments.

2 Comments

Error : requested unknown parameter 'first_name' for row 0, column 0
@user630209 Can you try to debug in the render function? or maybe add console.log(data) before return

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.