Client-only: use custom Ajax or events?

Client-only: use custom Ajax or events?

jladburyjladbury Posts: 34Questions: 9Answers: 0

I am new to DataTables. I intend to use it and Editor for a client-only application which will load and save data from a local file.

Two of the Editor examples seem relevant: https://editor.datatables.net/examples/simple/noAjax.html and https://editor.datatables.net/examples/advanced/localstorage.html

Presumably (please correct me if I am wrong) if I take the approach in the former I would need to define actions to be taken on various events. With the latter, I just need to modify the Ajax function.

What are the pros and cons of each approach?

Replies

  • allanallan Posts: 63,455Questions: 1Answers: 10,465 Site admin

    You can't have the browser read a file from the file system (file://) via Ajax as that would be a massive security hole (any page could read any file on your computer!). You'd need to have the user upload the file if you want to do local file editing. You could then export the altered data using Buttons or similar.

    Allan

  • jladburyjladbury Posts: 34Questions: 9Answers: 0

    Thanks for a super speedy response!

    I will get the data from a local file using the FileReader API (I already have an application doing this).
    Having got the data, I want to use Editor to return any changes made by the user.

    Would the Ajax routine in the localstorage example not be appropriate from that point on? I would use it to update local variables etc which I would then write back somehow (this part of my app needs further investigation which is not affected by DataTables/Editor)

  • allanallan Posts: 63,455Questions: 1Answers: 10,465 Site admin

    I wouldn't bother with the localStorage example in that case - unless you actually want it in local storage! Use the noAjax example you linked to - Editor will update the table directly. Then when you want to get the data to export, use rows().data(). Or use Buttons which can do a CSV export, or if you wanted, you could have a custom button do a JSON export.

    Allan

  • jladburyjladbury Posts: 34Questions: 9Answers: 0

    .. and use the Editor events? (Sorry if I am being a bit thick!)

  • allanallan Posts: 63,455Questions: 1Answers: 10,465 Site admin

    I'm not clear on what you would want the events for? If you can populate the table (rows.add()) and then export the data, the no Ajax example should be all that is needed, unless I'm missing something?

    Allan

  • jladburyjladbury Posts: 34Questions: 9Answers: 0

    I want to save the changes the user makes, as they make them, into a different data structure. I will be using DT to provide an editable, tabular view of some data which is also presented in other ways, e.g. charts and timelines. Those other views share the data structure I mention. Any changes made will be reflected immediately in all views.

    I hope that makes sense!

  • allanallan Posts: 63,455Questions: 1Answers: 10,465 Site admin

    into a different data structure

    That's what I was missing. Use edit (or create / remove). Editor will keep the DataTable up to date itself - use those events to know about the changes.

    Allan

This discussion has been closed.