【问题标题】:Issue setting a td id/name in DataTables 1.10.x问题在 DataTables 1.10.x 中设置 td id/name
【发布时间】:2015-05-29 18:56:09
【问题描述】:

这与数据表 1.10.x 相关。

我使用this 引用来创建子行,并且很容易将 HTML 放入生成的 javascript 代码中,如下所示:

function format ( d ) {
    return '<div class="slider">'+ 
    '<table id="expandInput" cellpadding="5" cellspacing="0" border="0" style="margin: 0 auto;">'+                
        '<tr>'+
            '<td class="dropHeader">Cost</td>'+
            '<td class="dropInfo"><input required type="text" id="cost" name="cost" value="'+d.cost+'"></input></td>'+                
        '</tr>'+                       
    '</table>'+
   '</div>'; 
}

但这只会影响点击时生成的子子项。我不知道如何使用数据表本身生成的单元格的标准数据表语法创建idname。我在 datatables 网站上找到的唯一示例与使用服务器端创建 id 相关

var table = $('#ltc-table').DataTable( {    
    "data" : json,        
    "columns" : [
      { data : 'cost' },
      { data : 'resale' }
  ],
  "columnDefs": [
  { className: "details-control", "targets": [ 0 ] }
  ]
});

我知道我可以使用columnDefs 设置td 的类,如here 所示,但我不知道如何添加其他条件,我需要设置一个唯一的idname 用于生成的每个 td。

【问题讨论】:

    标签: javascript jquery datatables


    【解决方案1】:

    您需要使用createdRow 属性来定义每当为表格主体创建TR 元素时的回调。

    $('#example').dataTable( {
       "createdRow": function ( row, data, index ) {
          $('td', row).eq(1).attr('id', 'td-' + index + '-1');
       }
    });
    

    代码$('td', row).eq(1) 用于使用从零开始的索引选择表格行中的第二个单元格(1 用于第二个单元格)。代码attr('id', 'td-' + index + '-1') 会将单元格id 属性设置为第一行td-0-1,第二行td-1-1 等,其中index 是从零开始的行索引。

    请参阅this JSFiddleRow created callback example 进行演示。

    【讨论】:

      【解决方案2】:

      就我而言,它适用于“rowData”。一些代码:

      $('#example').dataTable({
          "columnDefs": [{
              'targets': [4], 'createdCell': function (td, cellData, rowData, row, col) {
                  td.id = "ID_For_TD_" + rowData.ObjId;
              }
          }],
      });
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-10-22
        • 1970-01-01
        • 2016-06-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-04-08
        相关资源
        最近更新 更多