Save data to JSON file

3,046 views
Skip to first unread message

Nuno Nogueira

unread,
Jul 29, 2013, 2:09:56 PM7/29/13
to handso...@googlegroups.com
Hi,

I'm using the code as in http://handsontable.com/demo/ajax.html on my server.

When saving the data, I get an OK message but nothing actually gets saved. Clicking the "Load" button brings the initial data again.

How can I save the data to the file?

Thanks for any help!

codename-

unread,
Jul 29, 2013, 5:48:40 PM7/29/13
to handso...@googlegroups.com
You need your own server for that. Demo page is only showing features that you can use.
In browser you can only display JSON data and copy it by your own to new file.

Nuno Nogueira

unread,
Jul 30, 2013, 12:39:46 PM7/30/13
to handso...@googlegroups.com
Hi,

Thanks for the feedback.

I do have my own server, but I guess I need to implement some kind of server side script to allow the import/export of the JSON files, right?

Any suggestion?

Thanks a lot, and good work!

codename-

unread,
Jul 30, 2013, 5:38:27 PM7/30/13
to handso...@googlegroups.com
Here you got demo of save and load. You need only change sql driver for json files :)
https://github.com/warpech/jquery-handsontable/tree/master/demo/php

Nuno Nogueira

unread,
Jul 30, 2013, 6:00:13 PM7/30/13
to handso...@googlegroups.com
Hey,

This is great, I'll check it!

Thanks a lot!

Nuno Nogueira

unread,
Aug 8, 2013, 4:48:41 PM8/8/13
to handso...@googlegroups.com
Hi,

I managed to insert a button that uploads a JSON file to the server and loads the data for hands on table, using a PHP script.

Now the following steps are required:
  1. Implement the auto-save function to update a JSON file with the changed data.
  2. Allow the user to download that file, so he can upload it again at a later time if he wants
  3. Delete JSON files from the server if they are older than x days.
This is how I think an app using handsontable could work. The demos you showed me seem to be posting the data to a database, but that doesn't seem to make a lot of sense to me.

Any suggestions/ideas are greatly appreciated, I'm still a newbie developer.

Thanks in advance for your help!

codename-

unread,
Aug 8, 2013, 7:37:18 PM8/8/13
to handso...@googlegroups.com
You need to write your $_POST['variable'] into file. You can find tutorial here: http://www.tizag.com/phpT/filewrite.php

step 1: you can do this save command in afterChange hook
step 2: just add link (<a></a>) to this file
step 3: you can do this by CRON jobs and some PHP script.

Nuno Nogueira

unread,
Aug 9, 2013, 7:14:54 AM8/9/13
to handso...@googlegroups.com
For the auto-save:

How do I pass all the data in the table to the PHP script that will update the JSON file?

I mean, I have this in my page:

<form action="escreve.php" method="post">
        <label><input type="checkbox" autocomplete="off" checked="checked" name="autosave">Autosave</label>
        <input type="submit" name="formSubmit" value="Submit" />
</form>

Which will require the user to click on the submit button to call escreve.php:

<?php
if(isset($_POST['autosave'])){
    $myFile = "testFile.json";
    $fh = fopen($myFile, 'w') or die("can't open file");
    $stringData = "Floppy Jalopy\n";
    fwrite($fh, $stringData);
    fclose($fh);
};
?>

I need to:
  1. be able to call escreve.php every time the data is changed without any submit button
  2. pass all handsontable data to $stringData
Any ideas?

Thanks in advance.

alfavi...@gmail.com

unread,
Sep 13, 2014, 1:06:10 PM9/13/14
to handso...@googlegroups.com
Hi,

I'm using the code as in http://handsontable.com/demo/ajax.html on my Node.js server.
How do I pass all the data in the table to the JS script that will update the JSON file?

Matt Vagnoni

unread,
Sep 15, 2014, 2:47:18 PM9/15/14
to handso...@googlegroups.com
Just call your "load" buttons event during the initialization loop before handsontable. 

$jQuery(document).ready(function ($) {
  $
.ajax({
    url
:
"json/load.json",
    dataType
:
'json',
    type
:
'GET',
    success
:
function (res) {
      handsontable
.loadData(res.data);
      $console
.text(
'Data loaded');
   
}
 
});
});

Reply all
Reply to author
Forward
0 new messages