How do a pass a variable of columns to aoColumns?

How do a pass a variable of columns to aoColumns?

danbuntudanbuntu Posts: 10Questions: 1Answers: 0

I'm trying to pass a variable that contains my column definitions into the aoColumns options. This is so that I can refuse the same block of JS on multiple pages by just passing in the new column layouts as needed.

I though I should just be able to pass in a variable and be done, like:

<code>
var myArray1 = [null, null];

var oTable = $('#example').dataTable({
"aoColumns":myArray1
});
</code>

But I just get
Uncaught TypeError: Cannot read property 'mData' of undefined

Answers

  • RpiechuraRpiechura Posts: 98Questions: 3Answers: 14

    Are you making sure that the number of columns in the array are the same as the number in the table? That seems to be the most likely reason that it would throw that error. From the documentation. "aoColumns: If specified, then the length of this array must be equal to the number of columns in the original HTML table. Use 'null' where you wish to use only the default values and automatically detected options."

  • allanallan Posts: 61,757Questions: 1Answers: 10,111 Site admin

    As Rpiechura says the columns array must be the same length as the number of columns in the table. No way around that, it must be the case. If you want a variable length array look at columnDefs.

    Allan

  • danbuntudanbuntu Posts: 10Questions: 1Answers: 0

    I double checked this morning and I do have a matching count of columns and variables

    in firebug i get

    TypeError: c is undefined

    if enter the cols by hand then it works fine. Like;

    <code>
    "aoColumns": [null,null,null,null,null,null,{ "sType": "date-uk" },null,null,null,null,null,null,null,null,null,null,null,null,null,null,null]
    </code>

    with a variable like:

    <code>
    var myArray1 = '[null,null,null,null,null,null,{ "sType": "date-uk" },null,null,null,null,null,null,null,null,null,null,null,null,null,null,null]';
    "aoColumns": myArray1
    </code>

    then it fails

  • allanallan Posts: 61,757Questions: 1Answers: 10,111 Site admin

    var myArray1 = '[

    That's a string, not an array...

    Allan

  • danbuntudanbuntu Posts: 10Questions: 1Answers: 0

    I've tried with various variations of:

    var myArray1 = new Array("null","null");
    
    var myArray1 = ["null","null"];
    

    and still no dice

    I can see

  • allanallan Posts: 61,757Questions: 1Answers: 10,111 Site admin

    Can you link us to the page please?

    Allan

This discussion has been closed.