0

Im using Datatables Jquery plugin to do a bit of work, but i havent been able to understand how to process the Json response im getting from the server . I get this response:

    "aaData":[
[
 {
  "idTreatment":23,
  "treatment":"Hospitalización",
  "directions":"Directions",
  "active":"1"
 },
 {
  "idCIE10":1,
  "cieCode":"A00",
  "diagnostic":"COLERA",
  "description":null,
  "lastUsed":1386649580000,
  "active":1
 }
],
[
 {
  "idTreatment":27,
  "treatment":"Antibiótico",
  "directions":null,
  "active":"1"
 },
 {
  "idCIE10":1,
  "cieCode":"A00",
  "diagnostic":"COLERA",
  "description":null,
  "lastUsed":1386649580000,
  "active":1
 }
],
[
 {
  "idTreatment":33,
  "treatment":"Hidratación Oral",
  "directions":"Directions",
  "active":"1"
 },
 {
  "idCIE10":1,
  "cieCode":"A00",
  "diagnostic":"COLERA",
  "description":null,
  "lastUsed":1386649580000,
  "active":1
 }
]
]
}

I have been trying to use mData or mRender, but im not familiarized with datatables yet, so i would be really grateful if someone could thell me how can in print this in a table on html

thanks in advance

this javascript fucntion is the one im using to initialize the table, im trying to use aoColumns based on examples but im not sure if this is the best option; also based on example im using mData, to show just 2 columns for test, as you can see im trying to acces the "array" or the json response using the objects field names, but when the page is rendering theres a warning saying that it cant find the field.

function initializeTreatmentsTable(id){
        treatmentsTable = $('#treatmentsTable').dataTable( {
                "bSort":false,
                "sAjaxSource":"./diagnosticTreatment",
                "fnServerParams": function ( aoData ) { aoData.push( {name:"diagnosticId",value:id} ); },
                "aoColumns": [
                             { "mData": "idTreatment" },
                             { "mData": "treatment" }
                             ]
    });
6
  • What exactly are you trying to achieve here? Which columns should be in the table? Commented Jan 15, 2014 at 14:14
  • im just trying to show the treatment field, but im unable to access the object correctly, also i would like to know how because i dont know if i will have to do something similar later. Commented Jan 15, 2014 at 19:43
  • What fields are being shown on your table right now? Do you have a jsfiddle to show your table in action? Commented Jan 15, 2014 at 20:07
  • at the moment i cant display anything in the table, just the header , because i dont know how to print the objects whe i try to use mData it say it cant find the field i specify i have tried whit a few fields. Commented Jan 15, 2014 at 23:20
  • It's almost impossible for anyone to help without seeing what you've tried. I suggest you setup a jsbin/jsfiddle with what you've done so far. A snippet of an array of objects doesn't say much really Commented Jan 15, 2014 at 23:59

1 Answer 1

0

Well after looking several times over the API in the official website datatables.net i came to the conclusion that i had to test out. If my problem isn't clear i will explain it again.

i have a table, and that table's data is obtained by a ajax request and a json response, the json response is a array of object but the objects themselves are composed of at least 2 objects ( {[[object1][object2]],[[object1][object2]]} ) so accessing them with mData was impossible at least for me, and the solution was so simple that i wonder i never tried, i just had to use mRender and a function.

function initializeTreatmentsTable(id){
        treatmentsTable = $('#treatmentsTable').dataTable( {
                "bSort":false,
                "sAjaxSource":"./diagnosticTreatment",
                "fnServerParams": function ( aoData ) { aoData.push( {name:"diagnosticId",value:id} ); },
                "aoColumns": [
                             {   "bVisible": false,
                                 "mRender": function(data,type,full){
                                    return data.idTreatment;}
                             },
                             { "mRender": function(data,type,full){
                                     return full[0].treatment;
                             } }
                             ]
    });

The function above its the one im using to initialise the table for the first time, i got the problem that i just wanted 2 field from the first object and none from the second one, but i dont now how to do it, so i used the "full" parameter that contains the whole json response, and accesed it.

This might not be the best answer but it did the work for me, so i hope that if someone know how to improved it , feel free to do it, or so just comment , and i also hope this can help someone else.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.