【问题标题】:How can I change the rootnode of any treestore at runtime?如何在运行时更改任何树存储的根节点?
【发布时间】:2011-06-02 10:21:50
【问题描述】:

如何在运行时更改树存储的 rootNode?​​p>

以下是我目前的树店:

Ext.define('rt.store.userinproject', {
    extend: 'Ext.data.TreeStore',
    folderSort: true,
    sorters: [{
        property: 'text',
        direction: 'ASC'
    }],
    root: {
        expanded: true,
        text: "",
        user: "",
        status: "",
        children: [{
            text: "Project 1",
            pid: 1,
            expanded: true,
            children: [{
                text: "Department 1",
                did: 1,
                cls: "folder",
                children: [{
                    text: "User 1",
                    uid: 1,
                    leaf: true
                }, {
                    text: "User 2",
                    uid: 2,
                    leaf: true
                }, {
                    text: "User 3",
                    uid: 3,
                    leaf: true
                }, {
                    text: "User 4",
                    uid: 4,
                    leaf: true
                }, {
                    text: "User 5",
                    uid: 5,
                    leaf: true
                }]
            }, {
                text: "Department 2",
                did: 2,
                cls: "folder",
                children: [{
                    text: "User 6",
                    uid: 6,
                    leaf: true
                }, {
                    text: "User 7",
                    uid: 7,
                    leaf: true
                }, {
                    text: "User 8",
                    uid: 8,
                    leaf: true
                }, {
                    text: "User 9",
                    uid: 9,
                    leaf: true
                }, {
                    text: "User 10",
                    uid: 10,
                    leaf: true
                }]
            }, {
                text: "Department 3",
                did: 3,
                cls: "folder",
                children: [{
                    text: "User 11",
                    uid: 11,
                    leaf: true
                }, {
                    text: "User 12",
                    uid: 12,
                    leaf: true
                }, {
                    text: "User 13",
                    uid: 13,
                    leaf: true
                }, {
                    text: "User 14",
                    uid: 14,
                    leaf: true
                }, {
                    text: "User 15",
                    uid: 15,
                    leaf: true
                }]
            }]
        }, {
            text: "Project 2",
            pid: 2,
            expanded: true,
            children: [{
                text: "Department 1",
                did: 1,
                cls: "folder",
                children: [{
                    text: "User 1",
                    uid: 1,
                    leaf: true
                }, {
                    text: "User 2",
                    uid: 2,
                    leaf: true
                }, {
                    text: "User 3",
                    uid: 3,
                    leaf: true
                }, {
                    text: "User 4",
                    uid: 4,
                    leaf: true
                }, {
                    text: "User 5",
                    uid: 5,
                    leaf: true
                }]
            }, {
                text: "Department 2",
                did: 2,
                cls: "folder",
                children: [{
                    text: "User 16",
                    uid: 16,
                    leaf: true
                }, {
                    text: "User 17",
                    uid: 17,
                    leaf: true
                }, {
                    text: "User 18",
                    uid: 18,
                    leaf: true
                }, {
                    text: "User 19",
                    uid: 19,
                    leaf: true
                }, {
                    text: "User 20",
                    uid: 20,
                    leaf: true
                }]
            }, {
                text: "Department 3",
                did: 3,
                cls: "folder",
                children: [{
                    text: "User 21",
                    uid: 21,
                    leaf: true
                }, {
                    text: "User 22",
                    uid: 22,
                    leaf: true
                }, {
                    text: "User 23",
                    uid: 23,
                    leaf: true
                }, {
                    text: "User 24",
                    uid: 24,
                    leaf: true
                }, {
                    text: "User 25",
                    uid: 25,
                    leaf: true
                }]
            }]
        }]
    }
});

目前我可以在树面板中显示从根节点开始的整棵树。

现在在上面的商店中,当用户单击项目 1 时,我想将“项目 1”作为我的根节点 当用户单击项目 2 时,“项目 2”作为我的根节点。

这可能吗?如果是这样,怎么做?如果您需要我的任何东西,请告诉我。

【问题讨论】:

  • 改rootNode是什么意思?您要更改文本还是所有层次结构?
  • @TheHorse 感谢您的回复。我可以说我只想显示层次结构的一部分。我不知道是否可以使用 extjs。我已经编辑了我的问题。在我上面的商店中,当用户单击项目 1 和项目 2 时,我想将“项目 1”作为 rootNode。我希望这能进一步解决问题

标签: extjs tree extjs4 extjs-mvc


【解决方案1】:

1. 如果您要先尝试修改现有根的某些部分,则需要获取根节点。您可以使用TreeStore 提供的getRootNode() 获取根节点。示例:

 var node = treeObject.getStore().getRootNode();

现在,要修改数据,您可以使用data 属性。例如,如果您需要更改根节点的文本,您可以:

node.data.text = 'New Root';   // You can access any other property of your node's data model the same way

2.如果您需要替换整个节点,您可以使用setRootNode(),您的TreeStore 也可以使用它。您必须传递用于 TreeStore 的适当数据模型。

由于您正在尝试替换您的根,您肯定需要使用setRootNode()。这是一个替换根的示例代码:

//your tree click handler...
itemclick : function(view,rec,it,inx,ev) {

    var root = view.getTreeStore().getRootNode();
    var newRoot = root.getChildAt(inx).copy('xx',true);                 
    view.getTreeStore().setRootNode(newRoot);                   
}

理想情况下,这段代码应该可以工作。但是copy() 中有一个small bug 会导致破坏此代码。论坛提供了一个临时解决方案(我不建议修改库文件)(参考上面的链接)。

另一种方法是编写自己的 copy 代码,例如:

var root = view.getTreeStore().getRootNode();
var newRoot = root.getChildAt(inx);
view.getTreeStore().setRootNode(newRoot); 
// for each child in newRoot, create child for the newly assigned root.

【讨论】:

  • @Abdel Olakara 感谢您的回复。我编辑了我的问题以进一步清除它。我有项目列表,我想要做的是,当用户单击项目 1 和 root =“项目 2”以单击项目 2 时,将树显示为 root =“项目 1”,依此类推。希望这会有所帮助。
  • @amar4kintu 您可以使用第二种方法。使用 setRootNode() 将“Project2”作为您的根节点。但我的问题是,在那之后你如何使用 Project1?
  • @Abdel Olakara 感谢您的回复。目前我正在使用边框布局,其中左侧有项目列表,右侧有选项卡式面板,该面板的选项卡之一是项目中的用户。因此,当用户单击左侧的项目时,我想显示该项目中的用户列表。希望这是有道理的。从上面的商店你认为有可能在用户点击时将根更改为我需要的吗?
  • @amar4kintu.. 你试过上面解释的第二种方法了吗?如果你这样做了,你就会知道它是否有效!
  • @Abdel Olakara 感谢您的回复。是的,我尝试了第二种方法,但之后树视图没有更新,因此它显示为空树。我所做的是这个-> usertree.getStore().setRootNode(usertree.getStore().getRootNode().getChildAt(0)); usertree.view.refresh();看来这是目前treepanel的问题。如果您有任何其他想法,请告诉我。谢谢。
猜你喜欢
  • 2022-01-08
  • 1970-01-01
  • 2021-05-02
  • 1970-01-01
  • 1970-01-01
  • 2016-07-27
  • 1970-01-01
  • 2019-01-15
  • 1970-01-01
相关资源
最近更新 更多