0

I am trying to use datatables by passign a jquery object to it. here is what I am working on from datatables and here is my fiddle

the dataset in the example looks like this(values only):

var dataSet = [
    [ "Tiger Nixon", "System Architect", "Edinburgh", "5421", "2011/04/25", "$320,800" ],
    [ "Garrett Winters", "Accountant", "Tokyo", "8422", "2011/07/25", "$170,750" ],...

But the dataset I want it to work for is like this(key:value): this is the fiddle

    dataSet = [
    {"Name":"Unity Butler",
    "Position":"Marketing Designer",
    "Office":"San Francisco",
    "Extn.":"5384",
    "Start date":"2009/12/09",
    "Salary":"$85,675"
    },
  {"Name":"Unity Butler2",
    "Position":"Marketing Designer",
    "Office":"San Francisco",
    "Extn.":"5384",
    "Start date":"2009/12/09",
    "Salary":"$85,675"
    },
  {"Name":"Unity Butler3",
    "Position":"Marketing Designer",
    "Office":"San Francisco",
    "Extn.":"5384",
    "Start date":"2009/12/09",
    "Salary":"$85,675"
    }   
    ]

I would like to make the change at the datatables end to accept my key:value dataset as opposed to reformatting my dataset to what datatables wants. So my question is, can this be done? Or in this case, is it best practice to change the format of my dataset to what datatables wants?

NOTES: related Q here


EDIT1

tks to @Tharsan Sivakumar below, here is my fiddle using map to reformat the dataset.

//reformat the dataset above from [{key:value,...},{}..] to [[value,...],[]]
dataSet = dataSet.map(function(d) {
  var arr = [];
  for (var key in d) {
    arr.push(d[key]);
  }
  return arr;
})

EDIT2

this is another option https://datatables.net/blog/2011-05-01

but the data has to be in this format {"aaData": [ {key:value,...},{}...]}

{ "aaData": [
    {
        "engine": "Trident",
        "browser": "Internet Explorer 4.0",
        "platform": {
            "inner": "Win 95+",
            "details": [
                "4",
                "X"
            ]
        }
    },
    ...
    ]
}

or specific to this example:

{ "aaData": [
    {"Name":"Unity Butler",
    "Position":"Marketing Designer",
    "Office":"San Francisco",
    "Extn":"5384",
    "Start date":"2009/12/09",
    "Salary":"$85,675"
    },
  {"Name":"Unity Butler2",
    "Position":"Marketing Designer",
    "Office":"San Francisco",
    "Extn":"5384",
    "Start date":"2009/12/09",
    "Salary":"$85,675"
    },
  {"Name":"Unity Butler3",
    "Position":"Marketing Designer",
    "Office":"San Francisco",
    "Extn":"5384",
    "Start date":"2009/12/09",
    "Salary":"$85,675"
    }   
    ] }

I tried to achieve this here in this fiddle but I cant get access to the files.txt"sAjaxSource": "files.txt", there but I have set it up on my server with the directory looking like this(EDIT3 Here is a github working example):

-index.html
-/sources
+files.txt

Just not sure what is the best option now edit 1 or 2?

1 Answer 1

1

As per the Data tabes Official The rows in the table must be an array - so you would need to use jQuery's map function, or something similar, to convert from your object to an array. The elements in the array can be objects or arrays, but the rows container must be an array.

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.