0

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.

2 Answers 2

0

you can change the settings like this:

var oTable;

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

    oTable = $('#table_id').dataTable();
});
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks for the reply. If I want to change the 'aTargets', what should I write? According to this link:datatables.net/forums/discussion/11451/…, the aoColumnDefs cannot be changed, is it?
well then this is what you could do; destroy the table with bDestroy and recreate the table with your new initialisation options.
It can work, but I still have the problem when formatting the "aoColumnDefs". I edited the question and changed the code above. Could you have a look please?
c and rtitle are not defined.
They are previously defined. The result shows that if retitle is an three element array = [a,b,c],and c=3. Then the header shows a,b,c | a,b,c | a,b,c| instead of a|b|c
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.