如何在 TR 和 TD 中添加属性? [英] How to add attribute in TR and TD?

查看:30
本文介绍了如何在 TR 和 TD 中添加属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to add row using data datatables, and I can do it like this

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

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

What I need is something like this (some attribute in TR and TD tag)

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

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

How I can do it with datatables?

推荐答案

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

Use createdRow and columns.createdCell options to define a callback function that will be called when TR and TD element are created.

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

有关代码和演示,请参阅此示例.

See this example for code and demonstration.

这篇关于如何在 TR 和 TD 中添加属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆