简体   繁体   English

ExtJS 5-动态添加项目以从商店拆分按钮菜单

[英]ExtJS 5 - Dynamically add items to split button menu from store

I want to associate a store to split button menu. 我想将商店与拆分按钮菜单关联。 I am using extjs version 5. I have searched on the web and even went through sencha documentation but I couldn't figure out a way to achieve this. 我使用的是extjs版本5。我在网上搜索甚至浏览了sencha文档,但我想不出一种方法来实现此目的。

Currently i'm holding the menu details in variable and assigning it to the splitbutton xtyoe menu property.I want to achieve the same using store, your help is much appreciated! 目前,我将菜单详细信息保存在变量中并将其分配给splitbutton xtyoe菜单属性。我想使用store实现相同的功能,非常感谢您的帮助!

Program Code:- 程序代码:

        var menuJSON = [{
            text:'Menu1',
            menu:[{text:'Submenu1'},{text:'Submenu2'}]
        },{
            text:'Menu2',
            menu:[{text:'Submenu1'},{text:'Submenu2'}]
        },{
            text:'Menu3',
            menu:[{text:'Submenu1'},{text:'Submenu2'}]
        },{
            text:'Menu4'
        },{
            text:'Menu5',
            menu:[{text:'Submenu1'},{text:'Submenu2'}]
        }];
        {
        region: 'south',
        fbar: [{
            xtype:'splitbutton',
            id: 'app-starterMenu',
            text:'Start',
            scale:'small',
            iconCls:'icon-start',
            menuAlign: 'bl-tl',
            menu: menuJSON
        }]
        }   

Thanks in advance! 提前致谢!

It would be helpful if you can provide example code for the store or relative controller, however this is still easier than you might think. 如果可以为商店或相关控制器提供示例代码,这将很有帮助,但是这仍然比您想象的要容易。 Of course, it's nothing that comes out of the box, but it's a perfect case of using listeners. 当然,没有什么是开箱即用的,但这是使用侦听器的完美案例。

Whenever the store changes data, you want to update your button configuration. 每当商店更改数据时,您都希望更新按钮配置。 Therefore just add the relevant listener to your store - maybe using the refresh or update event (depending on your specific use case). 因此,只需将相关的侦听器添加到您的商店中即可-可能使用refresh或update事件(取决于您的特定用例)。 Then whenever the store data changes, you need to grab a reference to your button (or associated menu) and update the items. 然后,只要商店数据发生变化,就需要获取对按钮(或关联菜单)的引用并更新项目。 In it's most simplistic form, an example might be as follows: 以最简单的形式,示例如下:

store: {
    listeners: {
        refresh: function(store) {
            // Get all the raw data from records and use it to set items on the menu
            var rawData = Ext.Array.pluck(store.getRange(),'data');
            Ext.getCmp('app-starterMenu').setMenu({
                items: rawData
            });
            // *OR* Loop through the store data conditionally and include what you need
            Ext.getCmp('app-starterMenu').getMenu().removeAll();
            store.each(function(record){
                Ext.getCmp('app-starterMenu').getMenu().add(record.getData());
            }); 
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM