How to allow user to upload their own JSON file?
How to allow user to upload their own JSON file?
responsivelabs
Posts: 31Questions: 8Answers: 0
So I've been developing a portion of a project with datatables, and I've been trying to find a way to upload a JSON file on the client side and have it render in datatables. Preferably, im trying to do this all on the client side so I don't have to save all the JSON files that people upload. Anyone have any suggestions on this? Would greatly be appreciated.
This question has accepted answers - jump to:
This discussion has been closed.
Answers
Uploading a JSON file is not a built in feature of Datatables. This SO Thread may help you with that process. Once Uploaded you can use JSON.parse() to parse the uploaded JSON string, store it in a Javascript variable then use
rows.add()
to add to an already initialized Datatable or usedata
when initializing. Here is an example:https://datatables.net/examples/data_sources/js_array.html
Kevin
Ah gotcha - What about allowing the user to type in a file name or URL in place of uploading the json?
Thanks for the info so far, very helpful.
There is an example, in the thread, allowing the user to browse local files. Does something like that not work?
The process to obtain the JSON data is outside the scope of Datatables. You will be better served to look for a solution on Stack Overflow to meet your upload needs. Once you have that working then we can help get it into Datatables.
If you haven't done so already you will want to read the Data Manual to understand the structures Datatables supports.
Kevin
Ok, so i've managed to figure this out using the following:
So I presume now I would need to use the rows.add() function? And yes datatables will already be initialized since this is a feature I'm adding for users to essentially see their JSON file in a specifically rendered format.
Edited by Kevin: Syntax highlighting. Details on how to highlight code using markdown can be found in this guide
Yes, the
result
variable in line 12 should have the data you can add viarows.add()
. The key is to make sure the JSON data is an array containing either arrays or objects. For example:http://live.datatables.net/tahiquyi/1/edit
Kevin
Hmm, i'm having trouble trying to find out where to use the table.rows.add function in conjunction with what I've already got:
btw I'm doing it in this manner bc I'm selectively using certain data from the JSON file rather than rendering all of it.
Also I removed async: false - not sure why I had that in there
Currently when I load the page I just receive the array upon outputting the results into console:
(641) […]
[0…99]
[100…199]
[200…299]
[300…399]
[400…499]
[500…599]
[600…640]
length: 641
<prototype>: Array []
data.html:103:11
Looks like you should add it here:
This assumes the JSON data is in a format your table supports. You may need to iterate the
result
and manipulate it to load properly into the table.Kevin
Awesome, so I've definitely made some progress Kevin Looks like its loading the file now into datatables, its just that I get the following warning:
DataTables warning: table id=betMain - Requested unknown parameter '0' for row 641
I presume this is because the needs to be formatted to fit the table?
The link in the alert provides details:
https://datatables.net/manual/tech-notes/4
But yes, each row needs to match the Datatbles config. I wonder if line 641 is the end of the file with a blank line. It may be slower but you could loop through the
result
data, validate or manipulate as needed, then userow,add()
and add the rows one at a time. Like this:Kevin
Well currently, Im not using all of the data in the JSON file. Im selectively choosing certain data points and rendering just those, which I'm doing originally with:
How would I go about just extracting certain data like I'm doing for the original file its loading? Currently I see where the rows should be but all the data is blank.
Sorry about all the questions, Im so close to getting this to work - really hoping I can get it proper today!
My previous post has an example. If the rows are blank then the row data is probably not what you expect. Console.log statements should help you discover what you have. How does it differ from your initial data?
If you need help then post a few rows of your data so we can take a look.
Kevin
Hmm I tried that but its not outputting anything to the table. I've even tried removing all the extra data in the JSON and just doing one row but still nothing. Below is an example of the JSON file i'm testing:
Eventually though this is what the file would look like with a row containing all the data:
Btw, to make things easier here it is in action:
http://live.datatables.net/zeziqiyi/1/edit?html,css,js,console,output
For the JSON, I just copied the following and put it into the file example.json for upload. It seems to add the rows but no data at the moment.
Also here is a better formatted version of a row from the JSON file so you can easily see:
Thanks for formatting it This is object based but your Datatables is not configured for objects by using
columns.data
. Looks like your initial data you loop through and turn into array based data. You need to be consistent with this as Datatables doesn't support both in the same table at the same time.I would look at removing the loop for the initial data. Use
columns.data
to define the columns and usecolumns.render
to build'<a href="id='+data[i].id+'&modal" target="_blank">View</a>'
in the last column.Kevin
Ah dang, so I'm gonna most likely need to redo the initial data to use columns.data and columns.render? Was hoping I wouldn't have to redo it as I couldn't get it to work with the columns.data option for some reason
What about changing the secondary data to mimic the initial data?
The choice is yours. You can do either option. They just need to be the same structure.
Kevin
FINALLY got it working Decided to just go ahead and replace the initial data loop with the columns.data option. I haven't had a chance yet to use the columns.render for the HTML markup required for certain entries, however I can say that its rendering the data properly from the initial JSON file, and its also adding new rows successfully when uploading a new file.
I'm sure I'll have other questions along the way as this is a work in progress, but I really appreciate your help with this Kevin. Definitely would have taken me 10x longer without your assistance
Glad you got it working!
Kevin