In short:
Is there a better way to restore the state of the tree when the page loads?
Some details:
I have an interesting problem.
I’m using a treetable and would like to save the state when someone opens or closes nodes.
When the page reloads I would also like for the tree to restore itself to the state it was in before they left the page.
What I’m doing below works (yay). However… you have to click the plus sign two times instead of one time in order for the node to open. If I remove the onAfterLoad event, it only requires one click - not two - but the state doesn’t restore when the page reloads.
I see that the onAfterLoad event is being triggered with each node that gets opened (thankfully only on the first click).
Is there a better way to restore the state of the tree when the page loads? (I’ve also tried to restore the state within on a webix.ready event but the state of the tree doesn’t update.)
Here is the code. Any thoughts?
{
id:"requirementsContent",
view:"treetable",
url:"/requirements.json",
columns:[
{
id:"name",
header:"Component",
template:"{common.treetable()} #name#",
fillspace:true
},
{
id:"kind",
header:"Type",
fillspace:true
},
{
id:"state",
header:"Status",
fillspace:true
},
{
id:"actions",
header:"Actions",
fillspace:true,
template:" Edit | Add"
}
],
on:{
onBeforeOpen: function(id){
// If children have not been loaded yet, load them.
if (this.getItem(id).$count === -1){
this.loadBranch(id, null); // null means no callback
webix.message({
type:"error",
text:"Please click the plus sign a second time.
This is a bug we are working to resolve."
})
}
},
onAfterOpen: function(id){
//webix.message("After Open " + id);
// Save the state of the tree after an element been opened.
var state = this.getState();
webix.storage.local.put("requirements_content_treetable_state", state);
},
onAfterClose: function(id){
//webix.message("Closing " + id);
// Save the state of the tree after an element been opened.
var state = this.getState();
webix.storage.local.put("requirements_content_treetable_state", state);
},
// TODO: This causes the user to need to click the +
// symbol 2 times. Need to understand why.
// For now we'll leave this.
onAfterLoad: function(id){
var state = webix.storage.local.get("requirements_content_treetable_state");
if (state){
this.setState(state);
}
}
}
}