Installation error td id / name in DataTables 1.10.x

This applies to datatables 1.10.x.

I use this link to create child lines, and it's easy to place HTML inside javascript code that is genereated, for example:

function format ( d ) { return '<div class="slider">'+ '<table id="expandInput" cellpadding="5" cellspacing="0" border="0" style="margin: 0 auto;">'+ '<tr>'+ '<td class="dropHeader">Cost</td>'+ '<td class="dropInfo"><input required type="text" id="cost" name="cost" value="'+d.cost+'"></input></td>'+ '</tr>'+ '</table>'+ '</div>'; } 

But this only affects the child child that is generated by clicking the mouse. I have no idea how to create an id or name using the standard datatables syntax for cells that generate the data itself. The only example I could find on the datatables website is to create an id using the server side

 var table = $('#ltc-table').DataTable( { "data" : json, "columns" : [ { data : 'cost' }, { data : 'resale' } ], "columnDefs": [ { className: "details-control", "targets": [ 0 ] } ] }); 

I know that I can set the td class using columnDefs , as shown here , but I cannot figure out how to add additional criteria, and I need to set unique id and name for each td that is genereated.

+5
source share
1 answer

You need to use the createdRow property to define a callback for every TR element for the table body.

 $('#example').dataTable( { "createdRow": function ( row, data, index ) { $('td', row).eq(1).attr('id', 'td-' + index + '-1'); } }); 

The code $('td', row).eq(1) used to select the second cell in the row of the table using an index based on zero ( 1 for the second cell). The attr('id', 'td-' + index + '-1') code attr('id', 'td-' + index + '-1') set this cell id attribute to td-0-1 for the first line, td-1-1 for the second line, etc. Where is index - row index based on zero.

See this JSFiddle or Callback creation example created on line for a demonstration.

+6
source

All Articles