如何在 ExtJS TreePanel 中找到选定的节点?

发布于 2024-07-18 08:14:45 字数 72 浏览 4 评论 0原文

当用户单击按钮时,我试图检索 TreePanel 的选定节点(如果有)。 如何检索 TreePanel 中的选择节点? 谢谢。

I'm trying to retrieve the selected node, if any, of a TreePanel when the user clicks a button. How do you retrieve the select node in a TreePanel? Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

往事风中埋 2024-07-25 08:14:46
var selectednode = tree.getSelectionModel().getSelectedNode();

                //alert(selectednode);
                if(selectednode!=tree.getRootNode())
                selectednode.remove();
var selectednode = tree.getSelectionModel().getSelectedNode();

                //alert(selectednode);
                if(selectednode!=tree.getRootNode())
                selectednode.remove();
转角预定愛 2024-07-25 08:14:46
var nodesSelected = Ext.getCmp('foldersTree').getSelectionModel().selected.items;
if(nodesSelected.length > 0)
{
    var node = nodesSelected[0];
    console.log(node.internalId);
}

这是针对 ExtJS4 TreePanel 的

var nodesSelected = Ext.getCmp('foldersTree').getSelectionModel().selected.items;
if(nodesSelected.length > 0)
{
    var node = nodesSelected[0];
    console.log(node.internalId);
}

This is for ExtJS4 TreePanel

套路撩心 2024-07-25 08:14:46

对于 ExtJS 4...

我在树面板中添加了以下侦听器:

listeners: 
{
  itemclick: function(view, record, item, index, e)
  {
    selected_node = record;
  }
}

其中 selected_node 是全局变量。 诸如追加和删除之类的函数可以与此记录变量一起使用,例如:

selected_node.appendChild({text: 'New Node', leaf: true});
selected_node.remove();

我已经创建了添加节点和删除节点的按钮,它们使用上面的按钮向选定的节点追加和删除节点。 remove() 将删除选定的节点以及所有子节点!

您还可以随时使用以下方法获取选定的节点(通过单击鼠标左键和右键进行选择):
var selected_node = Ext.getCmp('tree_id').getSelectionModel().getSelection()[0];

For ExtJS 4...

I have added the following listener in my tree panel:

listeners: 
{
  itemclick: function(view, record, item, index, e)
  {
    selected_node = record;
  }
}

where selected_node is a global variable. Functions like append and remove can be used with this record variable e.g.:

selected_node.appendChild({text: 'New Node', leaf: true});
selected_node.remove();

I have created buttons for Add Node and Delete Node which use the above to append and remove nodes to the selected node. remove() will remove the selected node as well as all child nodes!

You may also get the selected node any time using (the selection occurs with left as well as right mouse click):
var selected_node = Ext.getCmp('tree_id').getSelectionModel().getSelection()[0];

你与清晨阳光 2024-07-25 08:14:46

@Steve

Ext.fly('navTree').getSelectionModel().getSelectedNode();

不起作用,请使用

Ext.getCmp('navTree').getSelectionModel().getSelectedNode ();

代替。

@Steve

Ext.fly('navTree').getSelectionModel().getSelectedNode();

is not going to work, use

Ext.getCmp('navTree').getSelectionModel().getSelectedNode();

instead.

归属感 2024-07-25 08:14:46
var myTree = Ext.getCmp('tree-panel');
var selectednode = myTree.selModel.selNode
var myTree = Ext.getCmp('tree-panel');
var selectednode = myTree.selModel.selNode
素食主义者 2024-07-25 08:14:46

在 ExtJS4 中,您可以使用 getLastSelected() 方法,该方法返回树中最后一个选定的项目。

请参阅 ExtJS: http:// /docs.sencha.com/ext-js/4-0/#!/api/Ext.selection.Model-method-getLastSelected

示例可能如下所示:

var tree = this.up("window").down("supportedcontrolcircuitmodelselector");
var selectedCircuit = tree.getSelectionModel().getLastSelected()

In ExtJS4 you can use the getLastSelected() method which returns the last selected item in a tree.

See ExtJS: http://docs.sencha.com/ext-js/4-0/#!/api/Ext.selection.Model-method-getLastSelected

An example might look like this:

var tree = this.up("window").down("supportedcontrolcircuitmodelselector");
var selectedCircuit = tree.getSelectionModel().getLastSelected()
束缚m 2024-07-25 08:14:46

简单的 ....

itemclick: function(view, record, item, index, e)
{
   alert(record.data.text);
}

Simple ....

itemclick: function(view, record, item, index, e)
{
   alert(record.data.text);
}
遗心遗梦遗幸福 2024-07-25 08:14:45

您要做的就是创建一个事件处理程序。 每个 ExtJs 对象都有许多自动与其关联的事件。 您将编写一个事件处理程序(函数),然后将其分配给事件侦听器。 因此,在这种情况下,您可能希望为 TreePanel 组件的“click”事件分配一个事件处理程序。

var tbPan = new Ext.tree.TreePanel({
    itemId:'navTree',
    autoScroll: true,
    root: new Ext.tree.TreeNode({
        id: 'root',
        text: 'My Tree Root',
        rootVisible: true
    }),
    listeners: {
        click: {
            fn:clickListener
        }
    }
});

clickListener = function (node,event){
    // The node argument represents the node that
    // was clicked on within your TreePanel
};

但是,如果您想知道已选择的节点,会发生什么情况? 此时,您需要访问 TreePanel 的选择模型。 您提到了按钮操作。 假设您想要向该按钮的单击处理程序应用一个函数来获取选定的节点:

var btn = new Ext.Button({
    text: 'Get Value',
    handler: function (thisBtn,event){
        var node = Ext.fly('navTree').getSelectionModel().getSelectedNode();
    }
});

您使用 Flyweight 元素来获取对 TreePanel 本身的快速引用,然后使用 TreePanel 的内部方法来获取它的选择模型。 之后,您使用该选择模型(在本例中为 DefaultSelectionModel)的内部方法来获取选定节点。

您将在 Ext JS 文档中找到大量信息。 在线(和离线 AIR 应用程序)API 相当广泛。 即使您不直接使用 Core,Ext Core 手册也可以让您深入了解 ExtJS 开发。

What you would do is create an event handler. Each ExtJs object has a number of events that are automatically associated with them. You would write an event handler (a function) that you could then assign to an event listener. So, in this case, you would probably want to assign an event handler to the 'click' event of your TreePanel component.

var tbPan = new Ext.tree.TreePanel({
    itemId:'navTree',
    autoScroll: true,
    root: new Ext.tree.TreeNode({
        id: 'root',
        text: 'My Tree Root',
        rootVisible: true
    }),
    listeners: {
        click: {
            fn:clickListener
        }
    }
});

clickListener = function (node,event){
    // The node argument represents the node that
    // was clicked on within your TreePanel
};

But, what happens if you want to know a node that is already selected? At that point you'll need to access the TreePanel's Selection Model. You mentioned a button action. Let's say you wanted to apply a function to that button's click handler to get the selected node:

var btn = new Ext.Button({
    text: 'Get Value',
    handler: function (thisBtn,event){
        var node = Ext.fly('navTree').getSelectionModel().getSelectedNode();
    }
});

You used the flyweight element to get a quick reference to the TreePanel itself, then used that TreePanel's internal method for getting the it's Selection Model. After that you used that Selection Model's (in this case the DefaultSelectionModel) internal method to get the Selected Node.

You will find a wealth of information within the Ext JS Documentation. The online (and offline AIR app) API is quite extensive. The Ext Core manual can also give you a great deal of insight into ExtJS development, even if you aren't using the Core directly.

会发光的星星闪亮亮i 2024-07-25 08:14:45
var tree = Ext.create('Ext.tree.Panel', {
    store: store,
    renderTo: 'tree_el',
    height: 300,
    width: 250,
    title: 'ExtJS Tree PHP MySQL',
    tbar : [{
        text: 'get selected node',
        handler: function() {
            if (tree.getSelectionModel().hasSelection()) {
                var selectedNode = tree.getSelectionModel().getSelection();

                alert(selectedNode[0].data.text + ' was selected');
            } else {
                Ext.MessageBox.alert('No node selected!');
            }

        }

    }]

});
var tree = Ext.create('Ext.tree.Panel', {
    store: store,
    renderTo: 'tree_el',
    height: 300,
    width: 250,
    title: 'ExtJS Tree PHP MySQL',
    tbar : [{
        text: 'get selected node',
        handler: function() {
            if (tree.getSelectionModel().hasSelection()) {
                var selectedNode = tree.getSelectionModel().getSelection();

                alert(selectedNode[0].data.text + ' was selected');
            } else {
                Ext.MessageBox.alert('No node selected!');
            }

        }

    }]

});
就此别过 2024-07-25 08:14:45

在 Ext JS 4 中,您可以在树面板上放置一个侦听器,如下所示:

listeners: {

    itemclick: {
        fn: function(view, record, item, index, event) {
            //the record is the data node that was clicked
            //the item is the html dom element in the tree that was clicked
            //index is the index of the node relative to its parent
            nodeId = record.data.id;
            htmlId = item.id;
        }
    }

}

In Ext JS 4 you can put a listener on the tree panel like this:

listeners: {

    itemclick: {
        fn: function(view, record, item, index, event) {
            //the record is the data node that was clicked
            //the item is the html dom element in the tree that was clicked
            //index is the index of the node relative to its parent
            nodeId = record.data.id;
            htmlId = item.id;
        }
    }

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文