Deeply nested arrays in a json
Deeply nested arrays in a json
Hello im pretty new using this library i was working just fine with it, but now i have a deeply nested array and i don't know how to interact with it. I read a lot of "render" api but i cant make it work.
I have a json llike this:
{
"draw": 0,
"recordsTotal": 1,
"recordsFiltered": 1,
"data": [
{
"id": "1",
"empresa_id": "1",
"usuarioRepartidor_id": "1",
"usuario_id": "1",
"status": "1",
"total": "1",
"created_at": null,
"updated_at": null,
"pivot": [
{
"producto_id": "1",
"pedido_id": "1",
"productos": {
"id": "1",
"nombre": "bolias",
"descripcion": "qwewqe",
"stock": "12",
"imagen": "Screenshot from 2017-10-05 10-13-39.png",
"created_at": "2017-10-30 22:41:11",
"updated_at": "2017-10-30 22:41:11",
"cola": [
{
"producto_id": "1",
"tipo": "A1",
"precio": "123",
"created_at": "2017-10-30 22:41:11",
"updated_at": "2017-10-30 22:41:11"
},
{
"producto_id": "1",
"tipo": "A2",
"precio": "123",
"created_at": "2017-10-30 22:41:11",
"updated_at": "2017-10-30 22:41:11"
},
{
"producto_id": "1",
"tipo": "A3",
"precio": "123",
"created_at": "2017-10-30 22:41:11",
"updated_at": "2017-10-30 22:41:11"
},
{
"producto_id": "1",
"tipo": "A4",
"precio": "123",
"created_at": "2017-10-30 22:41:12",
"updated_at": "2017-10-30 22:41:12"
}
]
}
},
{},
{}
]
}
],
"queries": [
{
"query": "select count(*) as aggregate from (select '1' as `row_count` from `pedidos` where `id` = ?) count_row_table",
"bindings": [
"1"
],
"time": 2.34
},
{
"query": "select * from `pedidos` where `id` = ?",
"bindings": [
"1"
],
"time": 2.52
},
{
"query": "select * from `pedido_producto` where `pedido_producto`.`pedido_id` in (?)",
"bindings": [
1
],
"time": 1.5
},
{
"query": "select * from `productos` where `productos`.`id` in (?, ?)",
"bindings": [
1,
2
],
"time": 1.07
},
{
"query": "select * from `precioProducto` where `precioProducto`.`producto_id` in (?, ?)",
"bindings": [
1,
2
],
"time": 1.3
}
],
"input": []
}
This is my table:
<div class="row-fluid margin-body">
<table id="productosdetalle" class="table table-hover table-condensed " >
<thead>
<tr>
<th>Producto</th>
</tr>
</thead>
</table>
</div>
And my scrip where i think the problem is:
$('#productosdetalle').DataTable({
"processing": true,
"serverSide": true,
"ajax": "ajaxPedido1/"+data.id,
"columns":[
{data: 'pivot.0.productos.nombre', name: 'pivot.productos.nombre'},
],
"language": idioma_esp
});
But i get an sql error "pedido_producto.productos column doesn't exist".
I want to render each "productos.nombre" in my table.
Does some one know my error?
This discussion has been closed.
Answers
I think the problem is here:
data: 'pivot.0.I am guessing that
data.pivotwill contain all the rows if you return more than one. If so then you will probably need to useajax.dataSrcto tell Datatables what object contains the returned data. Maybe something like this:Then use this for your columns config:
{data: 'productos.nombre', name: 'pivot.productos.nombre'},Give it a try and let us know the results.
Kevin
I fear that
ajax.dataSrcdoesn't work whenserverSideis enabled. So the first question is, do you actually need server-side processing? The response from the server (withdraw:0looks wrong) so I'm guessing probably not.Allan
@allan, I noticed this comment in the Server Side docs for the
datareturn parameter:Is this an error or am I misunderstanding the use of
ajax.dataSrcwith server side processing?Kevin
Sorry - you are right - I'm getting myself confused
. The
dataparameter can indeed be changed as Kevin says. Its the parameters such asdrawetc which can't be changed withdataSrc.Go with what Kevin says!
Allan
Well yes, im using server side (yajrabox) for laravel.
i try the kevins answer like this:
But then i get this

and alert of sql telling me that id is ambiguous
Whatever you are using to execute the SQL code probably needs to be modified to identify the table that the column belongs to.
Allan