how to use json data with the table
how to use json data with the table
vikceo
Posts: 7Questions: 2Answers: 0
Hi
my RESt returns data in json format like
[{"AppVersion":"v1.1","FeedbackId":188,"FeedbackType":"Error","Os":"iPad Simulator:iOS:iPad Simulator x86_64","OsVersion":"8.3","OscVersion":"11.1.10.0.0","ReportedBy":null,"Feedbackdate":"06-11-2015","ServerUrl":"http://127.0.0.1:7101/MUDRESTService/rest/v1"},{"AppVersion":"v1.1","FeedbackId":190,"FeedbackType":"Error","Os":"iPad Simulator:iOS:iPad Simulator x86_64","OsVersion":"8.3","OscVersion":"11.1.10.0.0","ReportedBy":null,"Feedbackdate":"06-11-2015","ServerUrl":"http://127.0.0.1:7101/MUDRESTService/rest/v1"}]
i was trying to use datatables as
$.ajax({ url: 'http://127.0.0.1:7101/MUDRESTService/rest/v1/mfeedbackerrors?onlyData=true&limit=99',
type: 'get',
dataType: 'json',
success: function(output) {
console.log('line first resp');
console.log(output) ;
var ddata = JSON.stringify(output.items);
console.log(ddata);
dataSet =ddata;
$(document).ready(function() {
$('#feedback-data-table').DataTable( {
data: dataSet,
columns: [
{ title: "App Version" },
{ title: "Feedback Type" },
{ title: "OS" },
{ title: "OS Version" },
{ title: "User" },
{ title: "Date" },
{ title: "Server Url" }
]
} );
} );
}
});
This wont work i know as the data it is expecting in the format:
var dataSet = [
[ "v1.1", "Error", "iOS", "9.2", "sales representative", "11-11-2015","http://localhost:7101/feedback" ],
[ "Garrett Winters", "Accountant", "Tokyo", "8422", "2011/07/25", "$170,750" ,"http://localhost:7101/feedback"]
]
Please advise how to configure either the component to accept data in json format or how to convert data into desired format
This question has an accepted answers - jump to answer
This discussion has been closed.
Answers
Format your code... Use the instructions that are in plain english right under the input box you typed the question in.
Its easier to help when the code/question is readable.
they forgot to mention how to edit your post in same plain english.
http://d.pr/i/1iJTs
ok thanks done. hope someone replies
See, that makes it easier!
So, a few things I notice...
$(document).ready()
within thesuccess
callback of your AJAX script... Thats kind of a face-palm moment. The document.ready has fired a looooong time before that.$.ajax
script, when you could just be using theajax
option, and have DataTables pull the data and load it automatically...columns.title
, which is fine, but since your AJAX data contains objects (not arrays), you need to specify thecolumns.data
as well, to tell DataTables what elements to put in which columns.The code you have above is actually more than you really need, I think this should work for you..
Couple things for you to note, I notice in line 8 of your 2nd code snippet, you refer to
output.items
, so I'm assuming that your JSON output is something like this:Since DataTables expects
data
to be the root data source object, and you're usingitems
, I set theajax.dataSrc
toitems
..Also, I see you have some data attached to the end of the URI, so I went and moved that to the
ajax.data
, and it should work fine.Your AJAX request is using GET, which is the default for DataTables AJAX, but if you ever need to change it, just use the
ajax.type
settingWell, I think thats it...
Please use http://codebeautify.org/jsonvalidate for Validating JSON data and to format http://jsonformatter.org
Uhm... what @irispanabaker?
Edit Oh... ^ spammer! Block em @allan! lol
wow this is an amazing stuff. way better than i though.
Wondering what would be the suggested way to handle a master detail scenario.
So, say my REST service returns departments and manger details of each dept (consider 1:1 parent child) then what could be used to show such data?
Not sure what that is? Master detail scenario?..
It looks like what you might need to do, is use a function as the value for
ajax
. Read through the documentation to see some examples.yes a master detail scenario however, details will always be just one row