Deeply nested arrays in a json

Deeply nested arrays in a json

ArivulArivul Posts: 3Questions: 2Answers: 0
edited October 2017 in Free community support

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?

Answers

  • kthorngrenkthorngren Posts: 21,179Questions: 26Answers: 4,923

    {data: 'pivot.0.productos.nombre', name: 'pivot.productos.nombre'},

    I think the problem is here: data: 'pivot.0.

    I am guessing that data.pivot will contain all the rows if you return more than one. If so then you will probably need to use ajax.dataSrc to tell Datatables what object contains the returned data. Maybe something like this:

    "ajax": {
      url: "ajaxPedido1/"+data.id,
      dataSrc: "data.pivot"
    },
    

    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

  • allanallan Posts: 63,226Questions: 1Answers: 10,416 Site admin

    I fear that ajax.dataSrc doesn't work when serverSide is enabled. So the first question is, do you actually need server-side processing? The response from the server (with draw:0 looks wrong) so I'm guessing probably not.

    Allan

  • kthorngrenkthorngren Posts: 21,179Questions: 26Answers: 4,923

    @allan, I noticed this comment in the Server Side docs for the data return parameter:

    Note that this parameter's name can be changed using the ajax option's dataSrc property.

    Is this an error or am I misunderstanding the use of ajax.dataSrc with server side processing?

    Kevin

  • allanallan Posts: 63,226Questions: 1Answers: 10,416 Site admin

    Sorry - you are right - I'm getting myself confused :). The data parameter can indeed be changed as Kevin says. Its the parameters such as draw etc which can't be changed with dataSrc.

    Go with what Kevin says!

    Allan

  • ArivulArivul Posts: 3Questions: 2Answers: 0

    Well yes, im using server side (yajrabox) for laravel.
    i try the kevins answer like this:

    $('#productosjjeje').DataTable({
                     "processing": true,
                     "serverSide": true,
                     "ajax": {
                       "url": "ajaxPedido1/"+data.id,
                       "dataSrc": "data.pivot"
                     },
                     "columns":[
                             {data: 'productos.nombre', name: 'pivot.productos.nombre'},
                     ],
                     "language": idioma_esp
    
             });
    

    But then i get this

    and alert of sql telling me that id is ambiguous

  • allanallan Posts: 63,226Questions: 1Answers: 10,416 Site admin

    Whatever you are using to execute the SQL code probably needs to be modified to identify the table that the column belongs to.

    Allan

This discussion has been closed.