dataTable javascript sourced data [tr td and class ]

dataTable javascript sourced data [tr td and class ]

usernetusernet Posts: 12Questions: 6Answers: 0
edited September 2014 in Free community support

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

  • allanallan Posts: 62,235Questions: 1Answers: 10,209 Site admin

    Can you link to a test page showing the problem please.

    Allan

  • usernetusernet Posts: 12Questions: 6Answers: 0

    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 )

  • usernetusernet Posts: 12Questions: 6Answers: 0

    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();

  • allanallan Posts: 62,235Questions: 1Answers: 10,209 Site admin

    how could i append my content to my dataTable

    You would use the API. See the FAQs.

    i need somthing like this but for many row

    Use rows.add()

    Allan

  • usernetusernet Posts: 12Questions: 6Answers: 0

    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();
    }

  • usernetusernet Posts: 12Questions: 6Answers: 0
    edited September 2014

    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

  • usernetusernet Posts: 12Questions: 6Answers: 0
    edited September 2014

    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

This discussion has been closed.