Using 2D Array

Using 2D Array

Petty_CrimPetty_Crim Posts: 4Questions: 0Answers: 0
edited May 2011 in General
I have a 2d associative array that is generated via ajax and I have seen no where in the documentation on how to use it with this script.

Do I have to manually go around ripping out the headers and sticking them into another array called aaColumns and then shove the data into aaData because it seems rather un practical if thats the case.

Replies

  • allanallan Posts: 63,522Questions: 1Answers: 10,473 Site admin
    Have a look at this blog entry: http://datatables.net/blog/Extended_data_source_options_with_DataTables . It's a new feature in DataTables 1.8 (currently in beta).

    Allan
  • Petty_CrimPetty_Crim Posts: 4Questions: 0Answers: 0
    edited May 2011
    Say my array is like this:
    myarray[0]["name"]="Dog"
    myarray[0]["type"]="Animal"
    myarray[0]["age"]="5"
    myarray[1]["name"]="Dell 486"
    myarray[1]["type"]="computer"
    myarray[1]["age"]="1"

    I want to make a table from that but this is generated dynamically via ajax. Is there a way to use that with datatables?
  • allanallan Posts: 63,522Questions: 1Answers: 10,473 Site admin
    Yes indeed - just use the mDataProp option as shown in the description. "mDataProp": "name" etc for each of the columns. If you don't have a table tag on the page, DataTables won't create the table node for you, but it's trivial to do, as shown here: http://datatables.net/examples/data_sources/js_array.html

    Allan
  • Petty_CrimPetty_Crim Posts: 4Questions: 0Answers: 0
    I tried that mDataProp but its giving me errors - "unknown parameter"
    My code is the following:
    var aaData=myarray;
    $('#example1').dataTable( {

    "aoColumns": [

    { "mDataProp": "vehicle_id" },

    { "mDataProp": "cell2" },

    { "mDataProp": "cell3" }

    ],
    "aaData": aaData});
  • allanallan Posts: 63,522Questions: 1Answers: 10,473 Site admin
    Can you show me what your JSON format looks like? I'm guessing it's different from the name / type / age you posted earlier?

    Allan
  • Petty_CrimPetty_Crim Posts: 4Questions: 0Answers: 0
    edited May 2011
    Its the same format just a bit longer (about 15 fields). its in this format:
    "field name": "value". I have converted the json to a 2d array like it said though so its not a json problem.
  • allanallan Posts: 63,522Questions: 1Answers: 10,473 Site admin
    No - I doubt it's a JSON problem given the error, but I was wondering what cell2 and cell3 are in your JSON. What is the full error that you are getting - DataTables should tell you what parameter is unknown.

    Allan
This discussion has been closed.