dataTable javascript sourced data [tr td and class ]
dataTable javascript sourced data [tr td and class ]
hi everybody i found one example in dataTable documentation about populating dataTable with javascript
http://www.datatables.net/examples/data_sources/js_array.html
however its not what i search , first this is my dataTable initialization :
$('#example').dataTable( {
"sDom": 'Rlfrtip',
"scrollX": true,
"stateSave": true
} );
as you see i use colreorder to reorder columns and i use stateSave to save the latest order . for php display my dataTable works fine , but when the user click on show all data , only the header save the lastest state but the data not match the order of column
this is a part of script :
$( "#allcontrat" ).on( 'click', function () {
var url=$("#contrats").attr("href");
$.post(url,function(data){
var content = '';
$('#example').dataTable().fnDestroy();
for (var i =0; i < data.contrats.length; i++) {
content +='<td class="nc">'+data.contrats[i]['contratID']+'</td>';
content += '<td class="ww">'+data.contrats[i]['ww']+'</td>';
......
content += '</tr>';
$('#example').dataTable( {
"sDom": 'Rlfrtip',
"scrollX": true,
"stateSave": true
} );
$("#tbd").empty().append(content);
Answers
Can you link to a test page showing the problem please.
Allan
http://jsfiddle.net/q79h18qd/
how could i append my content to my dataTable , i used $("#tbd").empty().append(content);
it populate my table but it doesn't match data with the header ( i used colreorder and save state )
i need somthing like this but for many row :
var newRow = "<tr><td>row 3, cell 1</td><td>row 3, cell 2</td></tr>";
var table = $('table').DataTable();
table.row.add($(newRow )).draw();
You would use the API. See the FAQs.
Use
rows.add()
Allan
not working i have this error Uncaught TypeError: Cannot read property 'add' of undefined
this my code :
for (var i =0; i < data.contrats.length; i++) {
var content = ''";
content+= '<tr><td>......</tr></td>';
$('#example').dataTable().row.add(content).draw();
}
that's work now but i have probleme with stateSave when it's on true only the first row is ordered and match the header title
i have the same problème of this topic:
http://datatables.net/forums/discussion/19625/colreorder-does-not-work-when-data-is-add-using-datatable-rows-add-data
i download colreorder.js but i still have a same problème even if i desactivate stateSave