Javascript 如何向每行添加行ID?

Javascript 如何向每行添加行ID?,javascript,jquery,datatables,Javascript,Jquery,Datatables,我试图使用jquery数据表向tr添加行ID。我甚至试过这个例子 但似乎不起作用,我使用了相同的JSON文件 任何帮助都将不胜感激 它应该给出类似于 <tr id="row_7"> <td>Emkay Entertainments</td> <td>Nobel House, Regent Centre</td> <td>Lothian</td> </tr> <tr id="row_8

我试图使用jquery数据表向tr添加行ID。我甚至试过这个例子 但似乎不起作用,我使用了相同的JSON文件

任何帮助都将不胜感激

它应该给出类似于

<tr id="row_7">
  <td>Emkay Entertainments</td>
  <td>Nobel House, Regent Centre</td>
  <td>Lothian</td>
</tr>
<tr id="row_8">
  <td>The Empire</td>
  <td>Milton Keynes Leisure Plaza</td>
  <td>Buckinghamshire</td>
</tr>

埃姆凯娱乐
摄政中心诺贝尔大厦
洛锡安
帝国
米尔顿凯恩斯休闲广场
白金汉郡
>更新的代码

<script type="text/javascript" charset="utf-8">
  $(document).ready(function () {
    var oTable = $('#example').dataTable(
      /** column structure and ppoluating columns **/ {
      "aoColumnDefs": [{
          "aTargets": [0],
          //"mData": "download_link",
          "mRender": function (data, type, full) {}
        }, {
          "aTargets": [1],
          //"mData": "download_link",
        }, {
          "aTargets": [2],
          //"mData": "download_link",
        }, {
          "aTargets": [3],
          //"mData": "download_link",
        }, {
          "aTargets": [4],
          //"mData": "download_link",
        }],
      "fnRowCallback": function(nRow, aData, iDisplayIndex) {
        nRow.setAttribute('id',some_variable);
      },
      "bFilter": false,
      "sScrollY": "300px",
      "bPaginate": false,
      "bProcessing": true,
      //"bServerSide": true,
      "sAjaxSource": "media/sample.json"
    }
  /*** End of column structure **/).makeEditable({
      sUpdateURL: "UpdateData.php",
      sAddURL: "AddData.php",
      sAddHttpMethod: "GET", //Used only on google.code live example because google.code server do not support POST request
      sDeleteURL: "DeleteData.php"
    });
  });
</script>

$(文档).ready(函数(){
var oTable=$(“#示例”).dataTable(
/**柱结构和柱体**/{
“aoColumnDefs”:[{
“目标”:[0],
//“mData”:“下载链接”,
“mRender”:函数(数据、类型、完整){}
}, {
“目标”:[1],
//“mData”:“下载链接”,
}, {
“目标”:[2],
//“mData”:“下载链接”,
}, {
“目标”:[3],
//“mData”:“下载链接”,
}, {
“目标”:[4],
//“mData”:“下载链接”,
}],
“fnRowCallback”:函数(nRow、aData、iDisplayIndex){
nRow.setAttribute('id',一些变量);
},
“bFilter”:错误,
“sScrollY”:“300px”,
“bPaginate”:错误,
“bProcessing”:正确,
//“bServerSide”:正确,
“sAjaxSource”:“media/sample.json”
}
/***柱端结构**/)。可编辑({
sUpdateURL:“UpdateData.php”,
sAddURL:“AddData.php”,
sAddHttpMethod:“GET”//仅用于google.code实时示例,因为google.code服务器不支持POST请求
sDeleteURL:“DeleteData.php”
});
});
HTML


领域
字段2
字段3
字段4
字段5

您可以将以下内容添加到dataTables初始化选项中:

"fnRowCallback": function(nRow, aData, iDisplayIndex) {
    nRow.setAttribute('id',some_variable);
}
要使用特定表格单元格的值作为每行的ID,请从
aData
变量中提取它:

"fnRowCallback": function(nRow, aData, iDisplayIndex) {
    nRow.setAttribute('id',aData[0]);
}

您是否在JSON中为每行添加了
“DT_RowId”:“some_id”
?@Blazemonger-谢谢。我按照您的建议尝试了两种初始化选项,它似乎没有在JSON上添加“DT_RowId”:“row_7”等,例如:(
"fnRowCallback": function(nRow, aData, iDisplayIndex) {
    nRow.setAttribute('id',aData[0]);
}