Ajax with json payload

Ajax with json payload

commediacommedia Posts: 21Questions: 5Answers: 0
edited March 2017 in DataTables 1.10

Hi! I have an Ajax data source for my datatables.
My json payload is as follow:

{
  "data": [
    {
      "user": {
        "id": 18,
        "ragione_sociale": "Patrizia",
        },
      "prodotti": [
       {
           "quantitaTot": 100,
           "pezziTot": 1000,
           "pesoTot": 6500,
           "id": 887,
           "lotti": [ ],
           "scarichi": [ ],
           "carichiMeseAttuale": [
              {
                  "id": 302,
                  "user_id": "18",
                  "prodotto_id": "887",
                  "lotto": "20-03-2017_21:48:06",
                  "tipo": "pallet",
                  "quantita_iniziale": "100",
                  "pezzi_iniziale": "1000",
                  "peso_iniziale": "6500",
                  "quantita": "9",
                  "pezzi": "90",
                  "peso": "6500",
                  "posizione": "",
                  "fornitore": "43",
                  "allega": "",
                  "slug": "20-03-2017_21:48:06",
                  "created_at": "03/20/2017 21:48:20",
                  "updated_at": "03/27/2017 18:39:29",
                  "deleted_at": null
                }
            ],
          "prezzoTot": 500,
          "user_id": "18",
          "codice": "A0004",
          "descrizione": "Canna fumaria 1",
          "peso": "65",
          "tipo": "pallet",
          "pezzi": "10",
          "forfait": "1",
          "unita": null,
          "prezzo": "5.00",
          "magazzino": "",
          "slug": "Canna fumaria 1",
          "seen": "0",
          "active": "0",
          "created_at": "03/20/2017 17:52:51",
          "updated_at": "03/24/2017 17:54:39"
        },
        {
          "quantitaTot": 0,
          "pezziTot": 0,
          "pesoTot": 0,
          "id": 902,
          "lotti": [ ],
          "scarichi": [ ],
          "carichiMeseAttuale": [
          {
                  "id": 303,
                  "user_id": "18",
                  "prodotto_id": "902",
                  "lotto": "21-03-2017_21:48:06",
                  "tipo": "pallet",
                  "quantita_iniziale": "110",
                  "pezzi_iniziale": "1000",
                  "peso_iniziale": "6500",
                  "quantita": "9",
                  "pezzi": "90",
                  "peso": "6500",
                  "posizione": "",
                  "fornitore": "43",
                  "allega": "",
                  "slug": "20-03-2017_21:48:06",
                  "created_at": "03/20/2017 21:48:20",
                  "updated_at": "03/27/2017 18:39:29",
                  "deleted_at": null
                }
          ],
          "prezzoTot": 0,
          "user_id": "18",
          "codice": "A0005",
          "descrizione": "Canna fumaria 2",
          "peso": "10",
          "tipo": "pallet",
          "pezzi": "10",
          "forfait": "0",
          "unita": "pallet",
          "prezzo": "10.00",
          "magazzino": "",
          "slug": "Canna fumaria 2",
          "seen": "0",
          "active": "0",
          "created_at": "03/24/2017 17:10:33",
          "updated_at": "03/24/2017 17:53:53"
        }
      ],
      "totale": [ 500 ]
   }
  ]
}

I wonder if I can get a table like this:

details CODICE - DESCRIZIONE - PESO
details A0004 Canna fumaria 1 65
details A0005 Canna fumaria 2 10

details it's product's details (carichiMeseAttuale)

1 LOTTO - PEZZI - PESO - ALLEGA - CREATED_AT
2 LOTTO - PEZZI - PESO - ALLEGA - CREATED_AT
3 LOTTO - PEZZI - PESO - ALLEGA - CREATED_AT

I think that json should be only with row for my datatables... or is it possible to read a json as this?
Thank you
Pat

Answers

  • allanallan Posts: 61,697Questions: 1Answers: 10,102 Site admin

    Hi Pat,

    You could set the ajax.dataSrc option to be data.0.prodotti. That will set the array of data in prodotti to be the base array for the table.

    Having said that, if your data array is only ever going to have a single object in it, it is redundant and you could simply the JSON structure.

    Allan

  • commediacommedia Posts: 21Questions: 5Answers: 0

    Hi Allan!
    thanks! Yes, you are right, but I get json with api. I can't simply the json structure.

    Bye
    Pat

  • commediacommedia Posts: 21Questions: 5Answers: 0

    Hi Allan!
    I wonder if it's possible to read both data.0.prodotti and data.0.user with ajax.dataSrc option. At the same time... I need data.0.prodotti for my rows and data.0.user.ragione_sociale for my table caption.How can I do for this?
    Thanks again
    Pat

This discussion has been closed.