Datatables ajax json doesn't like key/value format
Datatables ajax json doesn't like key/value format
islamelshobokshy
Posts: 99Questions: 20Answers: 1
I get this error : DataTables warning: table id=achats_table - Requested unknown parameter '1' for row 0, column 1. For more information about this error, please see http://datatables.net/tn/4
I get it because my json is of this format :
"status":"success",
"data":[
{
"0":"PrimaDonna",
"quantite":"4076",
"taux_de_livraison":"0.00",
"retour":"0",
... etc
And it wants it to be that way :
"status":"success",
"data":[
{
"PrimaDonna",
"4076",
"0.00",
"0",
... etc
How can I tell it not to look for keys, but rather values?
This question has an accepted answers - jump to answer
This discussion has been closed.
Answers
Looks lie you need to use
columns.data
. This doc discusses the data sources types and how to use them:https://datatables.net/manual/data/#Data-source-types
Also you can look at the ajax examples:
https://datatables.net/examples/ajax/index.html
Kevin
I would have used
to specify the order, but my order is dynamic ... At the first position there could be several different ones, same for all other positions. That's actually my biggest problem.
It's explicity said that
The down side of using objects is that you need to explicitly tell DataTables which property it should use from the object for each column.
, does that mean I can't have what I'm looking for? Or the columns table is not ordered and I can put whatever I want in there and the order doesn't count (like if it doesn't find the value in the table it ignores it)?Dynamic columns gets asked a lot. Here is an example:
http://live.datatables.net/regatofe/1/edit
My example builds the column names from the keys in the first object returned - simply because I don't have control over the source data. Your server could return an object with the column names.
You would also need to use ajax external from the Datatables options.
Kevin
I get an
Uncaught TypeError: Cannot read property 'style' of undefined
error tho when I do that.I think this is usually a mismatch in the number of table headers defined and the number of columns defined in Datatables.
My example assumes the table header is not defined in HTML and uses
columns.title
to build the table headers.Kevin