How will I create 2 datatables from 1 json file?
How will I create 2 datatables from 1 json file?
I'm using bootstrap framework.
Here's the json content:
[
{
"Issues(This week)":[
{
"category":"Sample Content",
"ruleName":"Sample Content",
"fileName":"Sample Content"
},
{
"category":"Sample Content",
"ruleName":"Sample Content",
"fileName":"Sample Content"
}
]
},
{
"Issues(Old)":[
{
"category":"Sample Content",
"ruleName":"Sample Content",
"fileName":"Sample Content"
}
]
}
]
Thank you.
This question has accepted answers - jump to:
Answers
Hi @JCG,
You can define a the data to use in the DataTable using
data
. Just pass in the data you want for each table.Thanks,
Sandy
Hi @sandy,
Thank you for your fast response.
Here's the script I tried but I have 2 data source in my json file; The "Issues(This week)" and "Issues("Old"). So I need to show both.
I want to create 2 datatables but I dont know how will I create it from 1 external json file I have .Thank you in advance!
Use jQuery ajax() to fetch the data then in the
success
function initialize both Datatables usingdata
to load the particular dataset. Here is a simple example:http://live.datatables.net/kibunado/1/edit
Kevin
Hi @kthorngren,
Thank you for helping me with this.
I tried your code and I got this response.
After getting this error, I tried to change the " var data = JSON.parse(json);" to
// var data = JSON.stringify(json);" and here's the output.
Here's my json file content:
[
{
"Issues(This week)":[
{
"category":"Sample Content",
"ruleName":"Sample Content",
"fileName":"Sample Content"
},
{
"category":"Sample Content",
"ruleName":"Sample Content",
"fileName":"Sample Content"
}
]
},
{
"Issues(Old)":[
{
"category":"Sample Content",
"ruleName":"Sample Content",
"fileName":"Sample Content"
}
]
}
]
Then this is the script:
Hope you can help me
The data is in an array, so you need to do something like this: http://live.datatables.net/yupevowu/1/edit
Colin
Hi @colin,
Thank you for your response, your sample code works for me. But only when I input the json data in my script. My problem now is my data source is from external json file. Can you help me get the data from an external json file?. Thanks again
The first thing to do is verify what the JSON response is. Use the browser's network inspector tool to see what the response is. This technote provides the steps.
Is this a local file or a webserver? Due to security reasons web browsers aren't allowed to open local files. You will need to send the URL to a server that returns the file.
Kevin
Hi @kthorngren ,
I am using a webserver. I combined all your suggestions and came up with this output (please find the attached screenshot). I also include the response on the side. I don't know what I'm missing to show the data.
Here's my updated script:
Thank you
Using
var data = JSON.stringify(json);
on the JSON response isn't going to work. First if the response is already a JSON stringJSON.stringify(j)
will just encapsulate that into another string. You are gettingUnexpected token....
when usingJSON.parse()
. That means that either the JSON is not formatted correctly or that you don't need to useJSON.parse()
and can use the data directly, ie, don't useJSON.parse()
just usevar data = json;
.The technote I linked to provides steps to test the JSON string using jsonlint.
Kevin
Hi @kthorngren,
I replaced the
JSON.parse()
tovar data = json;
and it worked .Thank you so much for helping me,@kthorngren and @colin . You guys are really amazing!