Load DataTable with bi-dimensional array of session.
Load DataTable with bi-dimensional array of session.

i have an array like that:
Is obtained by json_encode function in php, is an array from a session, because i'm trying to have a shopping cart.
But the DataTable doesn't give me any error message, is just empty:
$(document).ready(function() {
$.ajax({
type: "POST",
data:{"accion":"listar"},
url: "../gestionn/views/modules/articulo/carroinsert.php",
dataType: "json",
success: function(datas){
var t = $('#nuevoart').DataTable({
data:datas,
columns: [
{title: "COD.:",data:"cod"},
{title: "NOMBRE" ,data:"nombre"},
{title: "MARCA" ,data:"marca"},
{"title": "P/U ($)" , render: $.fn.dataTable.render.number( ',', '.', 0, '$' ),data:"precio" },
{title: "CAT.:" ,data:"cat"},
{title: "SUBCAT.:" ,data:"subcat"},
{title:"IVA (%)",data:"iva"},
{title:"ACCION"}
], }
}); });
And the question is how to load data like this? is as you see, a bidimiensional array or object array?
This question has an accepted answers - jump to answer
This discussion has been closed.
Answers
Hi @giulichajari ,
This thread should help, it's asking the same thing.
Cheers,
Colin
This page discusses the data structures Datatables supports:
https://datatables.net/manual/data/#Data-source-types
Datatables expects an array of rows where each element in the array is a row. You data structure contains objects. Your data structure will need to be converted to something like this:
Here is an example of ajax with object based data:
https://datatables.net/examples/ajax/objects.html
Click on the
Ajax
tab to see the data structure.Kevin
What happens if i use JSON.stringify function to obtain a char var and i format the JSON?
With stringify i obtain:
{"c4ca4238a0b923820dcc509a6f75849b":{"id":"1","cod":"3ALFANUMERICO","nombre":"3NOMBRE","marca":"OTTRA","precio":"0,33","cat":"construccion en seco","subcat":"Leo","iva":"21","unique_id":"c4ca4238a0b923820dcc509a6f75849b"},"c81e728d9d4c2f636f067f89cc14862c":{"id":"2","cod":"FANTA","nombre":"COCA","marca":"OTTRA","precio":"0,33","cat":"general","subcat":"general","iva":"21","unique_id":"c81e728d9d4c2f636f067f89cc14862c"},
So i have to replace the firs and the last "{" "}"; Is it possible?
That will likely result in an invalid JSON string. You can try it and test it at https://jsonlint.com/ . But even if you do that its still not in a structure that Datatables supports as the data inside is not in an array structure.
You will need either modify the
carroinsert.php
script to return the data in the supported format or, in the ajax success function, loop through the returned objects to build the supported array.Kevin
So this function in the other topic:
return a valid array?
I have to implement it like that?:
I'm happy to tell you that works ok like that: