How to save tree and check boxes?

I have several trees, loaded by XML, that allow users to make choices for querying a database. After they view their results, I want them to be able to return to the query page and see their trees in the same state as they left them (ones that were open, to be open; leaves that were checked, to be checked). Does saveState and loadState do this?


Yes, saveState() and loadState() will do. These methods use “Serialization”, and by default “checked” and open attributes are not serialized. You should use setSerialization method:


But note that cookie size is limited. So if you tree isn’t small, it would be better to not save the whole tree in the xml. You may use saveOpenStates/loadOpenStates and use own functionality that saves checked items to cookie and restore them (there is getAllChecked and setCheck methods).

I am having trouble implementing this. I have a sample at

When I create the trees, I call:

'.$treeName.'_tree.setSerializationLevel(true,true); '.$treeName.'_tree.loadXML("./includes/'.$treeName.'.xml"); '.$treeName.'_tree.loadState("treeStatex'.$treeName.'"); (Is the “treeStatex” needed here? I noticed that the treeStatex is prefixed to the cookie.)

When I submit the form, I save the state in a saveDomainsStates():

function saveDomainsStates(){ var arDomains = new Array('rx', 'otc', 'bio', 'ani', 'blk'); for (x in arDomains){ var elmTree = window[arDomains[x] + "_tree"]; if (elmTree != null){ elmTree.saveState(arDomains[x] + "_tree"); } } }

Then when I click return and go back to the first page, my trees are not open or checked as I left them.

Any idea what I am doing wrong?

Try to use the following approach

if(!tree.loadState(cookie_name)) tree.loadXML(url);

instead of

You should pass the same cookie name to loadState as used in saveState method

I noticed the function “enableAutoSavingSelected”. What does this do, and how does it differ from using saveState/loadState?

I am having trouble with my trees saving/loading their states properly in IE. Works just fine in FF.

i had similar problem but got superb solutions. can you send me your code so that i can have a look and give you efficient solution.

send me on "". put subject as DhtmlxTree Issues :slight_smile:



as you can see from the name of the methods:
saveState - saves the tree to cookie
loadState - load tree from cookie
enableAutoSavingSelected - turns the mode where the selected node will be saved to cookie.

If you have any issues with your tree please provide any kind of sample to reconstruct it.