簡體   English   中英

如何使用 jquery ajax 服務器端將 Id 添加到行

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

我需要將 id 屬性添加到使用 jquery ajax 服務器端動態添加的表數據的 TR 並在那里寫入數據。 我怎樣才能做到這一點。 我將下面的代碼附帶的 Json 數據添加到行中。

"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" },
        ],

我想要做;

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

注意:IdNumber 在數據中(json 列表)

鑒於您使用 Datatable 庫,您可以使用設置的createdRow屬性根據需要修改每一行。 嘗試這個:

$('#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...
});

我解決了我的問題;

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM