jQuery 数据表将 id 添加到添加的行 [英] jQuery dataTables add id to added row

查看:22
本文介绍了jQuery 数据表将 id 添加到添加的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以使用 jQuery DataTables 将 ID 添加到最后添加的行(例如,<tr id="myid">...</tr>)?

Is it possible to add ID to last added row with jQuery DataTables (e.g., <tr id="myid">...</tr>) ?

$('#example').dataTable().fnAddData( [
        "col_value_1",
        "col_value_2",
        "col_value_3",
        "col_value_4" ] );

(将 id 添加到这个新行)

(Add id to this new row)

推荐答案

使用 fnCreatedRow/createdRow 回调.最好在创建行时设置表行的 id 属性.使用 API 提供的内容,您无需破解它或编写混乱的代码

Use the fnCreatedRow/createdRow Callback. It is best to set the id attribute of the table row on creation of the row. Use what the API has provided and you won't need to hack it or have messy code

此函数在创建 TR 元素时调用(并且所有 TD 子元素都已插入),或者在使用 DOM 源时注册,允许操作 TR 元素(添加类等).

This function is called when a TR element is created (and all TD child elements have been inserted), or registered if using a DOM source, allowing manipulation of the TR element (adding classes etc).

//initialiase dataTable and set config options
var table = $('#example').dataTable({
    ....
    'fnCreatedRow': function (nRow, aData, iDataIndex) {
        $(nRow).attr('id', 'my' + iDataIndex); // or whatever you choose to set as the id
    },
    ....
});

// add data to table post-initialisation
table.fnAddData([
    'col_value_1',
    'col_value_2',
    'col_value_3',
    'col_value_4'
]);

这篇关于jQuery 数据表将 id 添加到添加的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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