JQuery dataTables add ID to line added

It is possible to add ID to lastest added row with jQuery datatable (tr id='myid') ?

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

(Add id to this new row)

Use the fnCreatedRow 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

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'
]);
我来评几句
登录后评论

已发表评论数()

相关站点

+订阅
热门文章