JQuery dataTables adds id to added row

Is it possible to add an identifier to the last added row using jQuery DataTables (for example, <tr id="myid">...</tr> )?

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

(Add id to this new line)

+10
jquery jquery-datatables datatables
source share
6 answers

Use the fnCreatedRow / madeRow callback . It is best to set the id attribute of the table row when creating the row. Use what the API provided and you won’t need to crack it or have dirty code

This function is called when the TR element is created (and all TD child elements have been inserted), or registered using the DOM source, which allows you to manipulate the TR element (add 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' ]); 
+21
source share

It 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); 
+8
source share

If you use row.add (), it is easy to set 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 () returns the element of the newly added string.

+1
source share

It worked for me

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

rowNode.id = 'MyID';

+1
source share

Reliability below code will help you

 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'); 
0
source share

Here is a cleaner approach I found here :

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

All Articles