Datatables ajax json doesn't like key/value format

Datatables ajax json doesn't like key/value format

islamelshobokshyislamelshobokshy 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

Answers

  • kthorngrenkthorngren Posts: 21,299Questions: 26Answers: 4,945
    edited May 2018

    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

  • islamelshobokshyislamelshobokshy Posts: 99Questions: 20Answers: 1
    edited May 2018

    I would have used

    columns: [
            { data: 'name' },
            { data: 'position' },
            { data: 'salary' },
            { data: 'office' }
        ]
    

    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)?

  • kthorngrenkthorngren Posts: 21,299Questions: 26Answers: 4,945
    Answer ✓

    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

  • islamelshobokshyislamelshobokshy Posts: 99Questions: 20Answers: 1

    I get an Uncaught TypeError: Cannot read property 'style' of undefined error tho when I do that.

  • kthorngrenkthorngren Posts: 21,299Questions: 26Answers: 4,945

    Uncaught TypeError: Cannot read property 'style' of undefined

    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

This discussion has been closed.