Sync Tree to Server

474 views
Skip to first unread message

writeto...@gmail.com

unread,
Dec 9, 2008, 6:13:27 AM12/9/08
to jsTree
Hi,

I am working on application that allows users to build a tree using
JSTree on the browser and then save it to the server.

What is best method to sync the local tree to the server?

The documentation contains a function 'getJSON' and it returns a
object if run the following command:
tree1.getJSON ($('#0'));

This object can be sent to the server and synced with the copy of tree
on server

Does this approach work ? Or is there another method to do this?

Regards,
Deepak

vakata

unread,
Dec 9, 2008, 8:51:52 AM12/9/08
to jsTree
That is the simplest approach for sending the whole tree.
Just convert the javascript object returned by getJSON to a string and
then send it to the server.
(or you could save it as HTML too)

If you maintain a structure (adjacency, nested sets) then you could
think of using the callbacks to have tree update "live". (but as far
as I understand this is not the case).

Regards,
Ivan

On Dec 9, 1:13 pm, "writeto.deep...@gmail.com"

writeto...@gmail.com

unread,
Dec 9, 2008, 10:27:15 AM12/9/08
to jsTree
Hi Ivan,

Thanks for the response. I will start with a simple approach first.

Regards,
Deepak

dantan

unread,
Jan 8, 2009, 6:23:14 AM1/8/09
to jsTree
I am trying to get the new tree data and submit it to php.

kinda like:

onmove : function(NODE,REF_NODE,TYPE,TREE_OBJ) {
var data = tree1.getJSON ($('#0'));
$.post('/test.php', data, function(data){
process(data);
})
}

That does not work of course.

Could you give me a hint how to get the data (id's, positions,
parents, childs)?
Or another approach to store/process data after any action?

vakata

unread,
Jan 8, 2009, 6:44:17 AM1/8/09
to jsTree
Hi,
What you did is almost the right thing.

Currently you are passing the actual JSON object to the server and it
is a mess.
You need to pass the JSON object of the tree as a string.
You could use http://code.google.com/p/jquery-json/ to convert it to a
string and pass it to the server.

onmove : function(NODE,REF_NODE,TYPE,TREE_OBJ) {
$.post('/test.php', { "data" : $.toJSON( tree1.getJSON ($
('#0')) ) }, function(data){
process(data);
})
}

Now on the server you can access the JSON string as $_POST["data"] (if
using PHP).

I hope this helps!

Best regards,
Ivan

dantan

unread,
Jan 8, 2009, 7:46:39 AM1/8/09
to jsTree
Thank you for your quick advice.

Since my last post I was trying this, an it seems to work:

onmove : function(NODE,REF_NODE,TYPE,TREE_OBJ) {

var data = $.toJSON(TREE_OBJ.getJSON());
console.log( data );
$.post('/url/to/process',{ data: data });
}

What is the difference between
TREE_OBJ.getJSON() and
tree1.getJSON ($('#0'))


I now get an json encoded array that I can work with php:
[2] => Array
(
[attributes] => Array
(
[id] => li_4
[class] => open last
)

[data] => Array
(
[de] => impressum
[en] => imprint
)

[children] => Array
(
[0] => Array
(
[attributes] => Array
(
[id] => li_5
[class] => open last
)
...

JVDH

unread,
Feb 11, 2009, 12:35:31 PM2/11/09
to jsTree
I've been trying this for the past few hours, but I can't get the
treedata into PHP as an array.

jsTree and jquery-json are running without problems. The data gets
posted but I can't decode the string. PHP's json_decode keeps
returning NULL. Any suggestions...I'm going crazy here.

On 8 jan, 13:46, dantan <hattin...@gmail.com> wrote:
> Thank you for your quick advice.
>
> Since my lastpostI was trying this, an it seems to work:

jcaspes

unread,
Feb 26, 2009, 5:18:29 AM2/26/09
to jsTree
Hi !

I like to sync the tree on server but in my case, the tree is based on
XML, so it will be a good think to sync the tree with xml.
so what the best method to get the tree structure as xml ?

thanks

JC

vakata

unread,
Mar 10, 2009, 10:07:43 AM3/10/09
to jsTree
the getXML method will be available in the next version :)
Regards,
Ivan

On Feb 26, 12:18 pm, jcaspes <jcas...@club-internet.fr> wrote:
> Hi !
>
> I like to sync the tree on server but in my case, the tree is based onXML, so it will be a good think to sync the tree withxml.
> so what the best method togetthe tree structure asxml?
>
> thanks
>
> JC
>
> On 11 fév, 18:35, JVDH <m...@jvdh.net> wrote:
>
> > I've been trying this for the past few hours, but I can'tgetthe
> > treedata into PHP as an array.
>
> > jsTree and jquery-json are running without problems. The data gets
> > posted but I can't decode the string. PHP's json_decode keeps
> > returning NULL. Any suggestions...I'm going crazy here.
>
> > On 8 jan, 13:46, dantan <hattin...@gmail.com> wrote:
>
> > > Thank you for your quick advice.
>
> > > Since my lastpostI was trying this, an it seems to work:
>
> > > onmove : function(NODE,REF_NODE,TYPE,TREE_OBJ) {
>
> > >         var data = $.toJSON(TREE_OBJ.getJSON());
> > >         console.log( data );
> > >                 $.post('/url/to/process',{ data: data });
>
> > > }
>
> > > What is the difference between
> > > TREE_OBJ.getJSON() and
> > > tree1.getJSON ($('#0'))
>
> > > I nowgetan json encoded array that I can work with php:

jcaspes

unread,
Mar 17, 2009, 6:15:42 AM3/17/09
to jsTree
Hi !
I will like to know when do you think the next version will be
available ?
I realy need the getXml method...

regards,

JC

jcaspes

unread,
Mar 19, 2009, 6:14:39 AM3/19/09
to jsTree
Hi !
sorry for the previous question,
i juste comme to go to the google code page and get the getXml
code :-)

thank a lot !

regards

JC
Reply all
Reply to author
Forward
0 new messages