Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. In my script, I am dynamically adding rows based on an event firing using fnAddData. Using fnRowCallback, I add in a unique row ID. This sometimes fails and does not add a row ID. In a test of 46 row additions, usually 6 to 8 rows do not get a row 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.


Add the row 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.

I am doing this using fnRowCallback during the initialisation stage. 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: missing row ids

As you can see, #14 has the row id and also the correct rowStripe class. #15 (and onwards) doesn't.

So, why is fnRowCallback not firing each time a row is added, only sometimes? Maybe there is a better way to add a row ID when a row is added?

like image 893
psx Avatar asked Nov 23 '11 08:11

psx


People also ask

How to add new row in DataTable using jQuery?

New rows can be added to a DataTable using the row. add() API method. Simply call the API function with the data for the new row (be it an array or object). Multiple rows can be added using the rows.

What is Rowcallback in DataTable?

Description. This callback allows you to 'post process' each row after it have been generated for each table draw, but before it is rendered into the document. This means that the contents of the row might not have dimensions ( $(). width() for example) if it is not already in the document.


1 Answers

Found the solution: 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);
like image 95
psx Avatar answered Oct 05 '22 10:10

psx