且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

jQuery dataTables将ID添加到添加的行

更新时间:2023-11-30 22:37:40

使用 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'
]);