【问题标题】:How to add attribute in TR and TD?如何在 TR 和 TD 中添加属性?
【发布时间】:2017-05-28 07:35:21
【问题描述】:

我想使用数据数据表添加行,我可以这样做

var table = $('#mytable').DataTable();
table.add.row(['first column', 'second column', 'three column', 'etc']);

我需要的是这样的(TR和TD标签中的一些属性)

<tr id="someID">
<td>first column</td>
<td>second column</td>
<td>three column</td>
<td id="otherID">etc</td>
</tr>

如何使用数据表做到这一点?

【问题讨论】:

    标签: datatables


    【解决方案1】:

    使用createdRowcolumns.createdCell 选项定义将在创建TRTD 元素时调用的回调函数。

    $('#example').dataTable( {
      'createdRow': function( row, data, dataIndex ) {
          $(row).attr('id', 'someID');
      },
      'columnDefs': [
         {
            'targets': 3,
            'createdCell':  function (td, cellData, rowData, row, col) {
               $(td).attr('id', 'otherID'); 
            }
         }
      ]
    });
    

    有关代码和演示,请参阅this example

    【讨论】:

    • 感谢@Gyrocode.com 在您通过 AJAX 加载的示例数据中,如果我像我的示例一样手动添加行呢?
    • @DarkCyber​​,没关系,但是我更新了示例以使用row.add()
    【解决方案2】:
    "fnRowCallback": function (nRow, aData) {
        var $nRow = $(nRow);
        $title = `Detalles de la Orden No. ${aData['noOrden']}`;
        $nRow.attr("title", $title);
    
        return nRow;
      },
    

    【讨论】:

    • 欢迎来到 Stack Overflow。虽然此代码可能会回答问题,但提供有关此代码为何和/或如何回答问题的额外上下文可提高其长期价值。另外,当已经有一个接受的答案时,为什么这个答案甚至是必要的? How to Answer
    猜你喜欢
    • 1970-01-01
    • 2012-10-29
    • 1970-01-01
    • 2015-12-24
    • 2021-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-11
    相关资源
    最近更新 更多