jQuery的数据表添加ID来添加的行

问题描述 投票:11回答:6

是否有可能增加ID到最后添加的行使用jQuery数据表(例如,<tr id="myid">...</tr>)?

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

(ID添加到这个新行)

jquery datatables
6个回答
22
投票

使用fnCreatedRow/createdRow回调。最好是设置表行的id属性上创建行。使用API​​提供什么,你不会需要破解它还是有乱码

创建TR元件时,该函数被调用(和所有TD子元素已被插入),或者如果使用DOM源,允许TR元件的操作(增加类等)登记。

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

8
投票

这为我工作

var MyUniqueID = "tr123"; // this is the uniqueId.
var rowIndex = $('#MyDataTable').dataTable().fnAddData([ "column1Data", "column2Data"]);
var row = $('#MyDataTable').dataTable().fnGetNodes(rowIndex);
$(row).attr('id', MyUniqueID);

3
投票

如果你使用row.add()很容易设置id:

   var table = $('#example').DataTable();
   var rowNode = table.row.add([
        "col_value_1",
        "col_value_2",
        "col_value_3",
        "col_value_4"
    ]).node();

    $(rowNode).attr("id", myid);

所述.node()返回新添加的行的元件。


2
投票

这里是一个更简洁的方法,我发现here

table.row.add( [ ... ] ).node().id = 'myId';
table.draw( false );

1
投票

这为我工作

var rowNode=$('#MyTable').DataTable().row.add([1,2,3]).draw( false );

rowNode.id='myId';

rowNode.id = 'MYID';


-1
投票

希望下面的代码将帮助您

var rowid = $('#example').dataTable().fnAddData( [
    "col_value_1",
    "col_value_2",
    "col_value_3",
    "col_value_4" ] );
var theNode = $('#example').dataTable().fnSettings().aoData[rowid[0]].nTr;
theNode.setAttribute('id','your value');
© www.soinside.com 2019 - 2024. All rights reserved.