How can I add Id to row with jquery ajax server-side

Viewed 673

I need to add id property to TR of table data dynamically added with jquery ajax server-side and write data there. how can I do that. I add the Json data coming with the code below to the lines.

"ajax": {
        "url": '/Siparis/SiparisleriDoldur/',
        "type": 'POST',
        "dataType": 'json',
        "data": { liste }
    },
    "columns":
        [
            { "data": "MusteriAdi", "name": "Müşteri Adı" },
            { "data": "MalzemeAdi", "name": " Ürün Adı" },
            { "data": "SiparisAdet", "name": "Sipariş Adedi" },
        ],

i want to do;

  <tr id='IdNumber' data-id='IdNumber'>
<td>bla</td>
<td>bla</td>
<td>bla</td>
</tr>

Note: IdNumber is in data(json list)

2 Answers

Given your use of the Datatable library you can use the createdRow property of the settings to amend each row as required. Try this:

$('#yourDatatable').dataTable({
  "createdRow": function(row, data, dataIndex) {
    let id = data[0]; // amend 'data[0]' here to be the correct column for your dataset
    $(row).prop('id', id).data('id', id); 
  },
  "ajax": {
    // ...
  },
  // other settings as normal...
});

I fixed my problem with;

"createdRow": function (row, data) {
            var id = data.Id;
            $(row).prop('id', id).data('id', id);
            $(row).attr('data-id', id).data('data-id', id);
        },
Related