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

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

问题描述

是否可以将ID添加到jQuery DataTables的最后添加的行中(例如<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)

推荐答案

使用 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 dataTables将ID添加到添加的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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