Load DataTable with bi-dimensional array of session.

Load DataTable with bi-dimensional array of session.

giulichajarigiulichajari Posts: 19Questions: 8Answers: 0

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

Answers

  • colincolin Posts: 15,142Questions: 1Answers: 2,586

    Hi @giulichajari ,

    This thread should help, it's asking the same thing.

    Cheers,

    Colin

  • kthorngrenkthorngren Posts: 20,275Questions: 26Answers: 4,765
    edited June 2019

    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:

    [
      {
        cat: "con....",
        cod: "ALF....",
        ....
        unique_id: "7ec..."
      },
      {
        cat: "con....",
        cod: "33",
        ....
        unique_id: "c4c..."
      },
    ....
    ]
    

    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

  • giulichajarigiulichajari Posts: 19Questions: 8Answers: 0
    edited June 2019

    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?

  • kthorngrenkthorngren Posts: 20,275Questions: 26Answers: 4,765
    Answer ✓

    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

  • giulichajarigiulichajari Posts: 19Questions: 8Answers: 0

    So this function in the other topic:

      var q = [];
      var keys = Object.keys(p);
      $.each(keys, function (i, item) {
      var details = p[item];
       q[q.length] = { "name": item, "details": details };
        });
    
         c = { data: q, columns: [{ "data": "name" }, { "data": "details.Office" }, { "data": 
        "details.Position" }, { "data": "details.Age" }, { "data": "details.Salary" }] };
    

    return a valid array?

    I have to implement it like that?:

        data:c
    
  • giulichajarigiulichajari Posts: 19Questions: 8Answers: 0

    I'm happy to tell you that works ok like that:

            var q = [];
            var keys = Object.keys(datas);
             $.each(keys, function (i, item) {
                  var details = datas[item];
                q[q.length] = { "nombre": item, "details": details };
               });
    
              c = { data: q };}
    
               var t = $('#nuevoart').DataTable({
    
               data:q,
           columns: [
        {title: "COD.:",data:"details.cod"},
        {title: "NOMBRE" ,data:"details.nombre"},
        {title: "MARCA" ,data:"details.marca"},
         ... });
    
This discussion has been closed.