Putting data and titles together in the same JSON structure
Putting data and titles together in the same JSON structure
I have a JSON string like this:
[{"sid":"38","name":"Alex","Last Name":"hori","email":"noz@gmail.com"},{"sid":"59","name":"Andy","Last Name":"numa","email":"tommy@gmail.com"},{"sid":"40","name":"Ann","Last Name":"hama","email":"ohama@gmail.com"}]
Then I made this structure, to fit datatables' needs:
{title: "sid"},{title: "name"},{title: "Last Name"},{title: "email"}
I'm trying to put the titles (column header) at the same time as the data, and nothing is working. The closest I've gotten is an offset of the title and it's column.
$('#example').DataTable( {
data: myList,
columns: [{data:"sid"},{title: "sid"},{data:"name"},{title: "name"},{data:"Last Name"},{title: "Last Name"},{data:"email"},{title: "email"}]
} );
This generates an error: DataTables warning: table id=example - Requested unknown parameter '1' for row 0, column 1.
and DataTables warning: table id=example - Requested unknown parameter '1' for row 0, column 1
and then the columns don't match:
How can I make this work?
__
This question has an accepted answers - jump to answer
Answers
The problem is that you need to combine the
columns.data
andcolumns.title
into the same object, or they'll be treated as different columns - which is what you're seeing.So,
should be something like:
Cheers,
Colin
Thank you Colin.
I did that and it worked!
Then, I tried to make it a variable... and it didn't
cols = '[{ data: "sid", title: "sid"},{ data: "name", title: "name"},{ data: "Last Name", title: "Last Name"},{ data: "email", title: "email"}]';
data: myList,
columns: cols
any ideas?
You are defining
cols
as a string using'
. It should be an array, remove the two'
.Kevin
ah, I built it as a string...
I only used the single quotes to illustrate that.