How do a pass a variable of columns to aoColumns?
How do a pass a variable of columns to aoColumns?
danbuntu
Posts: 11Questions: 2Answers: 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
This discussion has been closed.
Answers
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."
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 atcolumnDefs
.Allan
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
That's a string, not an array...
Allan
I've tried with various variations of:
and still no dice
I can see
Can you link us to the page please?
Allan