[Hierarchical Tree] Saving TreeList changes into local variable

Forge Component
(10)
Published on 13 Feb (7 days ago) by Ângelo Sousa
10 votes
Published on 13 Feb (7 days ago) by Ângelo Sousa

Hi

I'm trying to figure out the best way to save all checked child element.


Cheers!


Solution

Hi Jeffrey,

I've try this once with Ztree. I dont know with Hierarchical Tree, but what i see in the description, they have same functionality. Maybe you can try this method.


This how i do it in Ztree :
1. Make sure chkDisabled in ztree is set to False. (you can check another feature in this https://github.com/zTree/zTree_v3/tree/master/demo)
2. Create an hidden input (id: SelectedNode_textbox) that reference to Text Variable, for example "Selected"
3. In preparation, set SettingCallback.onCheck of ztree settings with function. This function will update every time node is check or unchecked. The update method means it clear the values in SelectedNode_textbox, then re-write the value with node id according to selected node.
The code will be something like this:

SyntaxEditor Code Snippet

//treeId : refer to ztree id
//tb_selected: refer to SelectedNode_textbox id
function treeNodeChecked(treeId, tb_selected)
{           
    var treeObj = $.fn.zTree.getZTreeObj(treeId); //not sure if this is correct
    var checkedNode = treeObj.getCheckedNodes();        
    var checkedId = document.getElementById(tb_selected); 
    var countChecked = 0;
    for(c = 0; c < checkedNode.length; c++)
    {   //if you only need to save the child node
        if(!checkedNode[c].isParent)
        {
            if(countChecked == 0)
                document.getElementById(tb_selected).value = checkedNode[c].id;
            else
                document.getElementById(tb_selected).value +=  "," + checkedNode[c].id;

            countChecked = countChecked + 1;
        }      
     }       
}

3. By the code, you'll see the value inside SelectedNode_textbox will be change everytime you select the node.
4. To save, use String_split to split the value in "Selected" parameter (refer to SelectedNode_textbox) by using "," delimiter, then iterate the String_split result. If the id is custom type, use TextToInteger(IntegerToIdentifier(resultText))

Good luck!




Solution

Hendra wrote:

Hi Jeffrey,

I've try this once with Ztree. I dont know with Hierarchical Tree, but what i see in the description, they have same functionality. Maybe you can try this method.


This how i do it in Ztree :
1. Make sure chkDisabled in ztree is set to False. (you can check another feature in this https://github.com/zTree/zTree_v3/tree/master/demo)
2. Create an hidden input (id: SelectedNode_textbox) that reference to Text Variable, for example "Selected"
3. In preparation, set SettingCallback.onCheck of ztree settings with function. This function will update every time node is check or unchecked. The update method means it clear the values in SelectedNode_textbox, then re-write the value with node id according to selected node.
The code will be something like this:

SyntaxEditor Code Snippet

//treeId : refer to ztree id
//tb_selected: refer to SelectedNode_textbox id
function treeNodeChecked(treeId, tb_selected)
{           
    var treeObj = $.fn.zTree.getZTreeObj(treeId); //not sure if this is correct
    var checkedNode = treeObj.getCheckedNodes();        
    var checkedId = document.getElementById(tb_selected); 
    var countChecked = 0;
    for(c = 0; c < checkedNode.length; c++)
    {   //if you only need to save the child node
        if(!checkedNode[c].isParent)
        {
            if(countChecked == 0)
                document.getElementById(tb_selected).value = checkedNode[c].id;
            else
                document.getElementById(tb_selected).value +=  "," + checkedNode[c].id;

            countChecked = countChecked + 1;
        }      
     }       
}

3. By the code, you'll see the value inside SelectedNode_textbox will be change everytime you select the node.
4. To save, use String_split to split the value in "Selected" parameter (refer to SelectedNode_textbox) by using "," delimiter, then iterate the String_split result. If the id is custom type, use TextToInteger(IntegerToIdentifier(resultText))

Good luck!




It works! thank you so much!