How to update dojo tree data dynamically

后端 未结 7 888
隐瞒了意图╮
隐瞒了意图╮ 2021-02-04 14:39

I would like to know how ot update the data of the dojo.dijit.tree component dynamically. At the moment I\'m creating the tree using dojo.data.ItemFileReadStore and dijit.tree.F

7条回答
  •  悲&欢浪女
    2021-02-04 15:08

    Here's a problem with Layke's solution (which otherwise does work) found by pre-production testing for a commercial website.

    Case 1:

    • Create & populate a tree.
    • Click on a node to select.
    • Execute refreshTree as in Layke's solution.
    • Click on a node, get error "this.labelNode is undefined".

    Now start again, case 2:

    • Create & populate a tree.
    • Click on a node to select.
    • Ctrl-click on the previously selected node.
    • Execute refreshTree as in Layke's solution.
    • Click on a node, no error.

    The stored selection references to the first selection are being used to undo the selection attributes (background color, etc.) when the second selection is made. Unfortunately, the referred-to objects are now in the bit-bucket. The modified code appears to be production-ready, i.e. hasn't failed any pre-production tests.

    The solution is to put:

    Tree.dndController.selectNone();
    

    prior to first line of Layke's refreshTree solution above.

    In response to meta suggestions, here it is:

    refreshTree : function() {
        // Destruct the references to any selected nodes so that 
        // the refreshed tree will not attempt to unselect destructed nodes
        // when a new selection is made.
        // These references are contained in Tree.selectedItem,
        // Tree.selectedItems, Tree.selectedNode, and Tree.selectedNodes.
        Tree.dndController.selectNone();
    
        Tree.model.store.clearOnClose = true;
        Tree.model.store.close();
    
        // Completely delete every node from the dijit.Tree     
        Tree._itemNodesMap = {};
        Tree.rootNode.state = "UNCHECKED";
        Tree.model.root.children = null;
    
        // Destroy the widget
        Tree.rootNode.destroyRecursive();
    
        // Recreate the model, (with the model again)
        Tree.model.constructor(dijit.byId("myTree").model)
    
        // Rebuild the tree
        Tree.postMixInProperties();
        Tree._load();
    
    }
    

提交回复
热议问题