How to format to the right JSON for an ajax reload of datatable

How to format to the right JSON for an ajax reload of datatable

MyuruMyuru Posts: 2Questions: 2Answers: 0

Hi i'm using Datatable in my project with Python & Django.

At the moment i have an issue with my index page, it is suppose to load a list from a database.

I call a specific stored procedure to get the right list of variable i need (multiple table etc).

My problem is (i think) that from what i saw on the documentation the data returned can be of two format :

array of arrays :

{ 
   "data": [
      [
         "Tiger Nixon",
         "System Architect",
         "$320,800",
         "2011/04/25",
         "Edinburgh",
         "5421"
      ]
   ]
}

And array of object :

{ 
   "data": [
      {
         "name": "Tiger Nixon",
         "position": "System Architect",
         "salary": "$320,800",
         "start_date": "2011/04/25",
         "office": "Edinburgh",
         "extn": "5421"
      }
   ]
}

and what i get back from my view is :

[{"DateHeureCreat": "2017-10-17 11:08:11.3000000", "Immat_Transporteur": "AA-229-AA", "Libelle": "BLE DUR                       ", "Espece": "C  0  00 ", "Sous_Type": "C - COLLECTE", "Type_Mouvement": "RCP - RECEPTION", "Poids_Charge": 21500.0, "Id": 1, "row": 1}, {"DateHeureCreat": "2017-10-17 11:11:07.8170000", "Immat_Transporteur": "ER-458-GN", "Libelle": "BLE TENDRE                    ", "Espece": "C  0  10 ", "Sous_Type": "S - STOCK", "Type_Mouvement": "CHG - CHARGEMENT", "Poids_Charge": 15650.0, "Id": 2, "row": 2}, {"DateHeureCreat": "2017-10-17 11:12:56.8000000", "Immat_Transporteur": "AB-344-CA", "Libelle": "BLE TENDRE                    ", "Espece": "C  0  10 ", "Sous_Type": "S - STOCK", "Type_Mouvement": "RCP - RECEPTION", "Poids_Charge": 12380.0, "Id": 3, "row": 3}, {"DateHeureCreat": "2017-10-17 12:24:17.2470000", "Immat_Transporteur": "AB-344-CA", "Libelle": "BLE TENDRE                    ", "Espece": "C  0  10 ", "Sous_Type": "S - STOCK", "Type_Mouvement": "RCP - RECEPTION", "Poids_Charge": 44000.0, "Id": 4, "row": 4}, {"DateHeureCreat": "2017-10-17 15:37:06.0500000", "Immat_Transporteur": "334-AB-350", "Libelle": "BLE DUR                       ", "Espece": "C  0  00 ", "Sous_Type": "S - STOCK", "Type_Mouvement": "RCP - RECEPTION", "Poids_Charge": 245120.0, "Id": 5, "row": 5}]

I think the missing "data:" make it difficult for the datatable to know where to put it's data.

At the moment when i launch my page everything load correctly, the ajax request are sent and get the right answe,

but as soon as the page is loaded, i guet first a typeError: f and then a typeError: C after each attempt at realoading.

How can i format my JSON so that datatable accept it ?

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,464Questions: 1Answers: 10,466 Site admin
    Answer ✓

    I think the missing "data:" make it difficult for the datatable to know where to put it's data.

    That's no problem - the ajax.dataSrc option can be used to handle that. Just set it to be an empty string.

    Allan

This discussion has been closed.