Noob Question(s)
Noob Question(s)
Hello DataTables community.
I'm fairly new to jQuery and DataTables but right now I'm simply trying to take the JSON returned from an AJAX request and put it into a table. Data coming back looks like this:
[{"sc":"00","stop_date":"2014-12-04T05:00:00Z","comments":"CROSS AUTO FUNCTION CODE 01 CREATED QSAR","part_sequence":"1","document":"763965661","cia":"0","part_qty":"2","part_number":"43W7633","part_status":"DISBURSED ","machine_serial":" ","machine_type":"1726","entity_type":"qsar","part_fc":"01"},
{"sc":"00","stop_date":"2014-12-19T05:00:00Z","comments":"CROSS AUTO FUNCTION CODE 01 CREATED QSAR","part_sequence":"1","document":"764093109","cia":"0","part_qty":"1","part_number":"39R6518","part_status":"DISBURSED ","machine_serial":" ","machine_type":"1726","entity_type":"qsar","part_fc":"01"}]
Pretty straight-forward. I simply want to create the table and have the column headers be set to the key for each row. On top of that I'm trying to set default values as some rows come back missing a key/value pair.
To accomplish this I'm attempting to go about all of this using columnDefs to define the column headers as well as the default content behavior. I'm guessing I then need to pass in the data itself using some other mechanism?
I've developed in Java for a while but a lot of this is simply not intuitive to me. I'm looking through examples on forums and I see people using a mix of "columnDefs", "aaData", "data", "aoColumns" and "columns" in their DataTable defintion. It's not clear which are prefrerred/required. For the record I'm using the latest version of DataTables (1.10.9).
Sorry for any botched terminology above; I can clarify if need be.
Thanks in advance.
Answers
All you'd really have to do is use that data as the data source, via either JavaScript Sourced Data or you can do the AJAX Sourced Data
Just make sure that the columns defined in either
columns
and/orcolumnDefs
match up with the proper keys in each object within the data source.Heres a quick example that took literally 1 minute to setup, I only used 3 of your columns in the JSON content, but hopefully you should be able to figure out the rest..
If you are getting a flat array of data back (and you are using DataTables'
ajax
option you will also need to useajax.dataSrc
and set it to be an empty string so DataTables knows that you are giving it a flat array. See this example for a demo.Allan
Thanks to both for responding. I'm hitting a URL that returns the JSON I pasted in the original post. Just for my clarification, if I'm doing an AJAX request first outside of the DataTable creation and saving the JSON returned to a variable, I need to use the JavaScript Sourced method on that JSON variable, correct? Alternatively, If I use the other method I need to pass the actual URL in as the "url" parameter in the AJAX Sourced DataTable creation?
Couple other follow-ons:
(1) I'm also wondering if there's a way to dynamically create the table column names from the JSON data returned instead of having to list them out in "columns"?
(2) Sometimes the "comments" field in the JSON is not returned and I would like to default that to a empty string (as I'm getting an array index warning when it's missing).
Yes, why not just use the variable as the ajax param? It makes things way easier
Yes, this is what i recommend
Yes, thats what i did, just create a variable, construct it, and pass it to
columns
.This one might be tricky then. Need to either always return something, or parse the json before you hand it to DT and add the comments key if it isnt there
Use the
columns.defaultContent
option to tell DataTables what to do in that situation.Allan
Oh, yep, @allan knows best, my bad. lol.
I was thinking that maybe the
columns
could accept a closure to do this, but according to the reference it doesnt look like it. So just construct the array outside of the table.What I did, was create the table dynamically via PHP, then just add a bunch of
data-*
to it, and use jQuery to parse those tags, somewhat like this.Also, keep in mind that you can specify a URL with
ajax
that contains more than just the data you wish to keep in a table, for example, my JSON array returns a ton of data, something like this:Then I just specify
assets
via thedataSrc
setting, then I can grab the other information that was retrieved via DataTables usingajax.json()
, and use that when formatting the rows, or for various other functionsThe reason I like
ajax
over just processing something via$.ajax()
then handing it todata
, is you can use theajax
namespace, which has a bunch of cool features.I wrote some JS that would reload the table via
ajax.reload()
once it senses some changes were made to the data source (by either more/less records, or if any rows were updated, by looking at the updated values)This might be a little over your head (since you said you were a noob), but you might make some use out of it.
EDIT: commented out the part that uses
_.find
(LowDash JS function) and added something that would just compare the entire arays: