Can we write the JSON data locally to a file

Can we write the JSON data locally to a file

_jthomas_jthomas Posts: 75Questions: 0Answers: 0
edited July 2011 in General
Hi Allan,
This may be one of the stupidest questions. Just as we can read from the local file as mentioned in the example( http://www.datatables.net/release-datatables/examples/ajax/ajax.html) -, I am testing it offline where HTML file and the JSON object array file resides locally, can we create and write to a file locally?

Regards
Joson

Replies

  • _jthomas_jthomas Posts: 75Questions: 0Answers: 0
    My initial thoughts are now lingering around running a light weight web server in the local machine to post the data. Do you have any suggestions which one would be a best fit? thanks

    Regards
    Joson
  • allanallan Posts: 63,277Questions: 1Answers: 10,424 Site admin
    To export JSON data to a local file, you could use TableTools and pass the Flash component the JSON data to be saved as a local file (just like it currently does for CSV). Loading would liekly require a modification of the Flash component since there isn't a cross browser way of reading local files from Javascript at the moment.

    Allan
  • _jthomas_jthomas Posts: 75Questions: 0Answers: 0
    Thanks Allan. I will check that out. Loading is already working great with following and passing chrome a command line parameter to access local files

    --allow-file-access-from-files file:///C:/offline/customer-search.html

    [code]
    "sAjaxSource": "customer/CustomerList.txt",
    "bDeferRender": true,
    [/code]

    Regards
    Joson
  • _jthomas_jthomas Posts: 75Questions: 0Answers: 0
    Also,
    It seems the example is not working in Chrome 12. Only Print button gets enabled. Thanks

    http://www.datatables.net/release-datatables/extras/TableTools/
  • _jthomas_jthomas Posts: 75Questions: 0Answers: 0
    Okay, The company network policy prevents downloading swf files. My BAD
  • _jthomas_jthomas Posts: 75Questions: 0Answers: 0
    Anyway, I have added the local folder to my flash trusted locations and it is now working great. Thanks
  • _jthomas_jthomas Posts: 75Questions: 0Answers: 0
    Hi Allan,
    Regarding your suggestion regarding passing the JSON data to the flash component, do you want me to add a new template to "tabletools.js" and pass the data?
    Here is my thought process
    a) each row has certain columns editable and when the user edits the value, he/she checks a box in the same row to indicate that he wants to save that edited values to the file ( done )
    b) I need to loop through the data and save it silently to a file when user click on the button "Save Data" in the page.
    c) I am planning to add one more button to TableTools.BUTTONS in the "tabletools.js" for saving the JSON data to the file.

    Is this a correct approach? Please advise and thanks very much.

    Regards
    Joson
  • _jthomas_jthomas Posts: 75Questions: 0Answers: 0
    Hi Allan,
    Is there a way to customize writing to the file without using the "oTableTools":aoButtons defined.
    I mean I have a button called "Save" and in its click event, I am going through some selected rows and planning to write to the file. The place where I am getting stuck is
    a) Is there a way to grab the flash movie clip instance to call fnSetText
    b)Is there a way to write to the file to a directory without popping up the file open dialog box?
    I would like to save to a file within the following code - would that be possible? Thanks

    [code]
    $('#savesomething', this).click( function () {
    $.each(oTable.fnGetNodes(), function(index, value) {
    if( $('input:checked', value).is(":checked") )
    console.log( oTable.fnGetData(index, 0) + "\t" + oTable.fnGetData(index, 1) + "\t" +value.cells[2].innerHTML + "\t" +value.cells[3].innerHTML);

    var tableTools = TableTools.fnGetInstance( 'example' );
    //can I write to a file here???
    });
    });
    [/code]
  • _jthomas_jthomas Posts: 75Questions: 0Answers: 0
    I guess if we can access the internal configuration object of TableTools, we could customize it? I think I am setting the actions at the wrong level instead of the configuration object? Let me know if I am heading in the wrong direction. Thanks

    [code]
    var tableTools = TableTools.fnGetInstance( 'example' );
    var flash = new ZeroClipboard.Client();
    flash.setAction( 'save' );
    flash.setCharSet( 'UTF8' );
    flash.setFileName( "filename.txt");
    tableTools.fnSetText(flash, "Checking");
    [/code]
  • _jthomas_jthomas Posts: 75Questions: 0Answers: 0
    Hi Allan,
    Is there a way to specify a particular directory to save the files? Currently, it is defaulting to the directory where the html page resides? Thanks very much.

    Also, it would be nice ( if it isn't already there) to save the file to a specific directory without prompting the user with File Save dialog box.

    Regards
    Joson
  • _jthomas_jthomas Posts: 75Questions: 0Answers: 0
    Hi Allan,

    When you get a chance, could you take a look at my previous question/query. Thanks very much!!!
  • fbasfbas Posts: 1,094Questions: 4Answers: 0
    On my computer, using Chrome, TableTools uses the last directory I saved to. It seems to get that from Chrome.

    I'm planning on looking into the TableTools code a little more later this week, so I'll look for anything that might help you.
  • _jthomas_jthomas Posts: 75Questions: 0Answers: 0
    Thanks fbas - You are correct. Chrome saves to the last directory something is saved to. I was looking in the TableTools code to see if there is a place where directory is specified. That way I could specify that in the TableTools config. Anyway, I was not able to locate that.

    Regarding the file save dialog box that may be controlled by the flash/swf component and I think it may not be possible to save the file without the "File open/save" dialog box.

    Regards
    Joson
  • allanallan Posts: 63,277Questions: 1Answers: 10,424 Site admin
    [quote]_jthomas said: Is there a way to specify a particular directory to save the files?[/quote]

    TableTools uses the FileReference ( http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/net/FileReference.html ) class in AS3, so whatever that can do, TableTools can (or could be modified to) do. It doesn't look like there is a way of doing either of the two things you want. Perhaps file an enhancement request against Adobe :-)

    Allan
  • _jthomas_jthomas Posts: 75Questions: 0Answers: 0
    Yep, you are correct Allan

    "The FileReference and FileReferenceList classes do not let you set the default file location for the dialog box that the browse() or download() methods generate."
This discussion has been closed.