What is the right format to change Datatables column using array?

…衆ロ難τιáo~ 提交于 2019-12-25 16:56:02

问题


I'm trying to change the column numbers of my Datatables after I initial the table:

where retitle is an array (i.e. an array of length 4) and c is the length of retitle (i.e. c=4), which is previously defined.

   var atarget = [];
   var stitle = [];
   for(var i=0; i<c; i++){
     atarget[i] = i;
     stitle[i] = retitle[i];
   }

var oTable = $('#table_id').dataTable({
 "bPaginate": false,
 "bProcessing": true,
 "bLengthChange": true,
 "bFilter": true,
  "bRetrieve": true,
 "bInfo": false,
 "bAutoWidth": false,
 "bServerSide": true,
 "sDom": '<"top"iflp<"clear">>rt<"bottom"iflp<"clear">>',
 "aLengthMenu": [[10, 25, 50, -1], [10, 25, 50, "All"]],
 "sAjaxSource": './aadata.txt',
 "aoColumnDefs": [
    {"sTitle":stitle,"aTargets":aTarget}
  ]

 }); 

But it just doesn't work.


回答1:


you can change the settings like this:

var oTable;

$(document).ready(function() {
    $('.something').click( function () {
        oTable._iDisplayLength = 50;
        oTable.fnDraw();
    });

    oTable = $('#table_id').dataTable();
});



回答2:


I solved the question by changing the way of initialising table in HTML instead of altering the settings in Datatables.

What I did is: first, remove the existing table, as well as the table wrapper!

 $('#table_id').remove();
 $('#table_id_wrapper').remove();

Then initialise a new table. and set the format of header/body according to your data:

 var content = "<table id='table_id' class='display datatable' style='width:100%;'><thead>";
 content +='<tr>';


 re = re.substring(0,re.length-1);
 // alert(re);
 var retitle = re.split(",");
    alert (retitle + 'x');
   var c = retitle.length;
   var atarget = [];
   var stitle = [];
   for(var i=0; i<c; i++){
     atarget[i] = i;
     stitle[i] = retitle[i];
     content += '<td>' +retitle[i]  + '</td>';

   }

  content +=' </tr></thead>';
  content +='<tbody></tbody>'
  content += "</table>";

Finally, append your table to your webpage. Here I attached it to my tab:

  $('#tab3').append(content);

By the way, thank you, @Volkan Ulukut all the same for your help.



来源:https://stackoverflow.com/questions/21730642/what-is-the-right-format-to-change-datatables-column-using-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!