vakata / jstree

jquery tree plugin
http://jstree.com
MIT License
5.16k stars 1.38k forks source link

End-to-End example 1) load the tree from JSON 2) user changes the tree 3) save the tree back to JSON #915

Closed YO-Tech closed 10 years ago

YO-Tech commented 10 years ago

OUF !!! after a month of jquery, javascript, AJAX, googling, reading and testing ... jstree.js development code reviews, ...

I do not understand why a simple example to perform following does not exist anywhere (Reading the API is painfull !!!)

1) to load a tree from a any source (JSON, SQL database ... keep it simple, let's say JSON) ... I am able to do so ! 2) give the user the possibility to change the tree nodes (DND, add, modify, delete are enough) ... I am able to do so ! 3) and mainly ... save the changes back to the same source (JSON, SQL database, ...) for future usage Why allowing changes if we cannot save them ? ( I have tried lots of things from the API doc).

You seem to be the GURU of this tool.

Easiest solution for me would be an updated nodes array from which I would be able to extract the node ID and PARENT and re-create my source for future usage after the user click on a SAVE button.

Ideally, I would let the user perform all updates he want and a SAVE button would scan the updated array and update back the source.

Many thanks in advance. I am building a site which will use the outcome of tree updates.

Bye.

vakata commented 10 years ago

Hi,

1) Take a look at the bundled PHP example (also available on jstree.com) - it is called sitebrowser and does exactly that, it even features lazy loading and the tree is saved on each change - the source is available in this repo too (demo folder).

2) A nice step by step guide is available on the home page of this repository, along with jsFiddle examples so that you can try out stuff on your own.

3) There is also the jstree.com site, which on its front page has a few tabs - each explaining a particular data source or a basic activity (attaching events, triggering changes, etc).

If you want to save the tree as a whole and not as changes occur you need to use the get_json method, which is described here: http://www.jstree.com/api/#/?q=get_json&f=get_json%28[obj,%20options]%29 You can call it like this:

// this is for the nested format
var your_tree_nested = $('#tree').jstree(true).get_json();
// and this is for the flat format (with ID / PARENT)
var your_tree_flat = $('#tree').jstree(true).get_json(null, { "flat" : true });

There is no "changed nodes" array, as this is a hierarchy - a simple move could invalidate the whole structure anyway, so you'd be better off either saving the tree as a whole, or reacting as each change occurs. Keep in mind you do not have to persist the changes as they occur if you do not want to - using the sitebrowser demo as a stepping stone - instead of persisting each change, you can save the changes to an object or array of your liking and sync at a later time. I will not provide such a method, because as I mentioned above, I cannot think of an universal way of storing changed nodes - I do not believe this is applicable for a tree (but it can be achieved if you want to).

For future how-to questions please use the discussion group (which also has a lot of information available): https://groups.google.com/forum/#!forum/jstree

YO-Tech commented 10 years ago

Hi,

… in 2 minutes, you have resolved my problem of the month !!

I can now write back the tree into my SQL database and reload it again.

I can now work on the main part of my web site which is using the SQL updated tree .

Thanks.

Bye.

De : Ivan Bozhanov [mailto:notifications@github.com] Envoyé : Friday, November 21, 2014 1:46 PM À : vakata/jstree Cc : YO-Tech Objet : Re: [jstree] End-to-End example 1) load the tree from JSON 2) user changes the tree 3) save the tree back to JSON (#915)

Closed #915 https://github.com/vakata/jstree/issues/915 . — Reply to this email directly or https://github.com/vakata/jstree/issues/915#event-196950874 view it on GitHub. https://github.com/notifications/beacon/AJbZixG_6oH5czG3QKiRfgKib61eZStdks5nP3_2gaJpZM4C_Jaj.gif

orionseye commented 4 years ago

Hi,

  1. Take a look at the bundled PHP example (also available on jstree.com) - it is called sitebrowser and does exactly that

Wow.. sooo beautiful :) Thanks!