简体   繁体   English

jQuery DataTables:如何为每个动态添加的行添加行ID?

[英]jQuery DataTables: How do I add a row ID to each dynamically added row?

Summary 摘要

I am using the great dataTables jQuery plugin from http://www.datatables.net . 我正在使用来自http://www.datatables.net的伟大的dataTables jQuery插件。 In my script, I am dynamically adding rows based on an event firing using fnAddData . 在我的脚本中,我基于使用fnAddData触发的事件动态添加行。 Using fnRowCallback , I add in a unique row ID. 使用fnRowCallback ,我添加一个唯一的行ID。 This sometimes fails and does not add a row ID. 这有时会失败,并且不会添加行ID。 In a test of 46 row additions, usually 6 to 8 rows do not get a row ID. 在46行添加的测试中,通常6到8行不会获得行ID。


Add Row function 添加行功能

function ps_ins(row)
{
  var rowArray = row.split('|');
  row = rowArray;
  var alarmID = parseInt(row[1],10);

  $('#mimicTable').dataTable().fnAddData([
    alarmID, 'col2', 'col3', 'col4',
    'col5', 'col6', 'col7'
  ]);
}

Rows are added to the table correctly. 行正确添加到表中。 The test I am running adds 46 rows, and all appear as expected. 我正在运行的测试添加了46行,并且都按预期显示。


Add the row ID 添加行ID

I am trying to add a unique ID to each row so I can later modify a specific row and refer to it in dataTable's cache using a combination of fnGetRows and .filter. 我正在尝试为每一行添加一个唯一的ID,以便稍后我可以修改一个特定的行,并使用fnGetRows和.filter的组合在dataTable的缓存中引用它。

I am doing this using fnRowCallback during the initialisation stage. 我在初始化阶段使用fnRowCallback执行此操作。 I've kept all the other settings just incase there's anything there that might have an impact on the problem. 我保留了所有其他设置只是因为那里可能会对问题产生影响。

  $('#mimicTable').dataTable({
    "sDom": 'lip<"mimicSpacer">f<"numUnAck">rt',
    "sScrollY": "365px",
    "aaSorting": [[4,'desc'],[5,'desc']],
    "iDisplayLength": 15,
    "aLengthMenu": [[15, 50, 100, -1], [15, 50, 100, 'All']],
    "bAutoWidth": false,
    "aoColumns": [
      null,
      { "sWidth": "20%" },
      { "sWidth": "22%" },
      { "sWidth": "9%" },
      { "sWidth": "9%" },
      { "sWidth": "20%" },
      { "sWidth": "20%" }
    ],
    "fnRowCallback": function( nRow, aData, iDisplayIndex, iDisplayIndexFull) {
      $(nRow).attr("id",'alarmNum' + aData[0]);
      return nRow;
    }
  });

A console.log of the dataTable object shows: dataTable对象的console.log显示: 缺少行ID

As you can see, #14 has the row id and also the correct rowStripe class. 如您所见,#14具有行ID和正确的rowStripe类。 #15 (and onwards) doesn't. #15(及以后)没有。

So, why is fnRowCallback not firing each time a row is added, only sometimes? 那么,为什么每次添加行时fnRowCallback都不会触发,有时只会触发? Maybe there is a better way to add a row ID when a row is added? 也许在添加行时有更好的方法来添加行ID?

Found the solution: http://www.datatables.net/forums/discussion/2119/adding-row-attributes-after-fnadddata/p1 找到解决方案: http//www.datatables.net/forums/discussion/2119/adding-row-attributes-after-fnadddata/p1

For anyone else wanting a quick solution, I did: 对于其他想要快速解决方案的人,我做了:

var addId = $('#mimicTable').dataTable().fnAddData([
  alarmID,
  'col2',
  'col3',
  'col4',
  'col5'
]);

var theNode = $('#mimicTable').dataTable().fnSettings().aoData[addId[0]].nTr;
theNode.setAttribute('id','alarmNum' + alarmID);

I know this is quite an old thread, but this might help others. 我知道这是一个很老的线索,但这可能对其他人有所帮助。 The easiest way I do it to add id attribute to the last added row as soon as I called the data table function fnAddData is: 我调用数据表函数fnAddData ,最简单的方法是将id属性添加到最后添加的行中:

$('#table').dataTable().fnAddData( ['col1','col2','col3'] );     

$('#table tr:last').attr('id','col'+row_id);

Try this it worked for me nicely: 试试这个很适合我:

var newRow = oTable.fnAddData( [ etc..]);
var oSettings = oTable.fnSettings(); 
var nTr = oSettings.aoData[ newRow[0] ].nTr;
nTr.id = 'new id';

You can use DT_RowId when you add new row, and use object instead an array, see below: 添加新行时可以使用DT_RowId,而使用对象代替数组,请参见下文:

 $('#mimicTable').dataTable().fnAddData({
            'DT_RowId': alarmID, 0:'col2', 1:'col3', 2:'col4',
            3:'col5', 4:'col6', 5:'col7'
        });

There is something unexpected with your code in the jsbin, even if it appears to be working. 你的代码在jsbin中有一些意想不到的东西,即使它看起来有效。 It initializes with the "browsers" data set. 它使用“浏览器”数据集进行初始化。 Then it clears. 然后它就会清除。 Then it makes a new table with one row. 然后它创建一个包含一行的新表。 Then it clears. 然后它就会清除。 Then it makes a new table with two rows. 然后它创建一个包含两行的新表。 Then it clears. 然后它就会清除。 Then it makes a new table with three rows. 然后它创建一个包含三行的新表。

I'd have to get a better sense of the code sample and its end goal (and alas my attention is diverted elsewhere right now) but you should be aware that fnRowCallback SHOULD be the way to do this, and that there are many redundant table constructions and row additions that are going to impact performance as well as flexibility with modifying rendering in the future. 我必须更好地了解代码示例及其最终目标(并且我的注意力现在转移到其他地方)但你应该知道fnRowCallback应该是这样做的方式,并且有很多冗余表将影响性能的构造和行添加以及将来修改渲染的灵活性。

This worked for me 这对我有用

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM