Creating a table passing column data using a programmatic function
Creating a table passing column data using a programmatic function
Can anyone help me with this problem? I can use the following JS to create a datatable, and all is good:
return $('#' + tableName).DataTable({
columns: [
{ data: 'TIMESTAMP'},
{ data: 'SERVER-HOSTNAME'},
{ data: 'SERVER-IP'},
{ data: 'TRACKS'}
]
});
As I have quite a few tables to create in my application, I would like to have a generic function that can be used repeatedly, rather than specifying the column names each time, so I just pass an array of fieldnames to a function, and want to build up the columns data programmatically rather than writing it out each time.
I cannot for the life of me get this to work correctly, e.g. how I can create this...
{columns: [
{ data: 'TIMESTAMP'},
{ data: 'SERVER-HOSTNAME'},
{ data: 'SERVER-IP'},
{ data: 'TRACKS'}
]}
..programmatically in a way that Datatables is happy with. I create what I think is a valid object, which datatables accepts but as soon as I try to populate any data, I get the "Requested unknown parameter '0' for row 0, column 0. " error, which when I debug the datatables object, I can see is because the table is getting initialised with "mData":0 instead of (for example) "mData":"TIMESTAMP".
I just cannot figure out how to resolve this, maybe someone else tried to do the same thing and can help?
This question has an accepted answers - jump to answer
Answers
This example builds the columns from the first record in the Ajax response.
http://live.datatables.net/huyexejo/1/edit
Hope it helps you workout what you need to do.
Kevin
Yes, that nailed it! I was trying to pass a single object reference rather than "columns: columns", I was just passing "columns" thinking that the object I had built up was the same thing! Sorted and working, thank you so much!