I have seen this requirement handled in one of 2 ways (or some variation on one of these approaches).
Both ways involve the source data providing the ID in each row array (or object). So, for example, if Tiger Nixon's data is sourced from an "employees" table in a database, then this value is assumed to be the primary key of the record.
Assuming the ID in this example is 1234
...
1) Place the ID value in a hidden column
var table = $('#example').DataTable( {
"columnDefs": [
{
"targets": [ -1 ], // the final column in the table
"visible": false,
"searchable": false
}
} );
table.row.add( ["Tiger Nixon", 32, "System Architect", 1234] );
2) Place the ID in the <tr>
element as an attribute
var rowData = ["Tiger Nixon", 32, "System Architect", 1234];
var rowID = rowData.pop();
var row = table.row.add( rowData ).draw();
$( row.node() ).attr( 'data-record-id', rowID );
This may be a completely unnecessary footnote, but just in case:
You cannot rely on DataTables to provide a durable ID. It does assign unique index values to each row, but that applies to one instance of a DataTable. That values will not persist across different creations of the table - and so there is no guarantee that "Tiger Nixon" will always be assigned the same index number.
If your data source does not have (or cannot provide) a unique ID already, then you could generate an ID yourself, upon row creation using a GUID generator, or something similar - and then add that value to the data array used to create your row. But here, again, you would have to ensure that the ID you generated is used consistently in every situation where the "Tiger Nixon" data is used. I think that could be challenging, or impossible.