nested array problem json values in same column and row

nested array problem json values in same column and row

LitapionnaLitapionna Posts: 1Questions: 1Answers: 0

I am trying to list all items into the table.

json structure {
"items": [
{"name": "Gelatine Leaves - Bulk",
"id": 1,
"timestamp": "2017-08-12 01:07:22",
"price": 7.08}
]
"username": "Oralla",
"user_id": 4
}

Problem is that values like name are in the same column. Every item should be in their own row.
Can some give some hint what should I be doing?

picture of problem

code

Live example
http://live.datatables.net/yaqirime/4/edit

This question has an accepted answers - jump to answer

Answers

  • kthorngrenkthorngren Posts: 21,303Questions: 26Answers: 4,947
    Answer ✓

    I modified your data format, example here:
    http://live.datatables.net/qejoyupo/1/edit

    Not sure this is what you are looking for but your data variable was an array with your data duplicated multiple times. I removed the array and duplicated data. So now the structure looks like this:

    var data= 
        
      {
            "items": [
                {
                    "name": "Gelatine Leaves - Bulk",
                    "id": 1,
                    "timestamp": "2017-08-12 01:07:22",
                    "price": 7.08
                },
    ....
            ],
            "username": "Oralla",
            "user_id": 4
        };
    

    Then I changed your data and option columns.data config to this:

            'data':data.items,
                
              columns:[
                {data: 'name'
                          
                        }, //name
                        {data:  'price'
                        }, //price
                        {data: 'timestamp'
                         
                        }, //time
                        {data: 'id'
                        } // id  
              ]
    

    Now the table populates as expected.

    Kevin

This discussion has been closed.