在 ExtJS 菜单项中添加按钮

Add buttons in ExtJS menu item

是否可以使用 ExtJS 4.2menuitem 中添加 buttons

我需要实现以下目标:

(在每个 menuitemcheckbox 旁边有一个 editdelete 按钮。它们将有自己的处理程序并将单击它们打开新面板)


我试图通过为每个 employee 创建一个 container 并在其中添加所需的 buttonsmenuitem 来实现它,但它无法正常工作因为我不能再 click 定制 menuitem 了,而且设计看起来一点也不好。


不过,我在下面的Whosebug答案中找到了一个可能的"hint"

(但这并不完全是我所期望的,因为我不只需要一个 icon,我需要一个 button 和它自己的 handler 加上我不知道与 DOM 交互是否是一个好的方法)


这是我到目前为止得到的结果以及我在哪里进行测试:

现场演示:

http://jsfiddle.net/oscarj24/hmqjqtqs/

ExtJS代码:

Ext.define('Namespace.view.Panel', {
    extend: 'Ext.panel.Panel',

    title: 'Panel',

    frame: true,
    floating: true,
    draggable: true,
    resizable: false,
    closable: true, 

    employees: null, 

    layout: {
        type: 'vbox',
        align: 'stretch'
    },

    constructor: function(cfg) {
        Ext.apply(this, cfg || {});

        this.items = [{
            xtype: 'container', 
            border: false,
            items: this.createItems()
        }];

        this.callParent(arguments);
    }, 

    createItems: function() {
        var items = [];

        items.push({
            xtype: 'button', 
            text: 'Employees', 
            menu: {
                xtype: 'menu', 
                items: this.createMenuItems()
            }
        });

        return items;
    }, 

    createMenuItems: function() {
        var employees = this.employees || [], 
            items = [];

        for (var i = 0; i < employees.length; ++i) {
            var employee = employees[i];
            if (employee) {
                items.push({
                    xtype: 'menucheckitem', 
                    text: Ext.String.format('{0} {1}', employee.name, employee.lastname), 
                    employeeId: employee.id, 
                    listeners: {
                        scope: this, 
                        checkchange: this.onMenuItemCheckChange
                    }
                });
            }
        }

        items.push({
            xtype: 'menuitem', 
            iconCls: 'add', 
            text: 'Add'
        });

        return items;
    }, 

    onMenuItemCheckChange: function(item, checked, eOpts) {
        console.log('Employee Id: %o was checked: %o', item.employeeId, checked);
    }, 

    destroy: function() {
        delete this.employees;

        this.callParent(arguments);
    }

});

Ext.onReady(function() {
    Ext.create('Namespace.view.Panel', {
        employees: [
            {id: 1, name: 'Oscar', lastname: 'Jara'}, 
            {id: 2, name: 'Foo', lastname: 'Bar'}
        ]
    }).show();
});

CSS:

.add {
    background-image: url('http://icons.iconarchive.com/icons/awicons/vista-artistic/16/add-icon.png') !important; width: 16px; height: 16px; display: block;
}

.edit {
    background-image: url('http://icons.iconarchive.com/icons/designcontest/outline/16/Pencil-icon.png') !important; width: 16px; height: 16px; display: block;
}

.delete {
    background-image: url('http://icons.iconarchive.com/icons/oxygen-icons.org/oxygen/16/Actions-edit-delete-icon.png') !important; width: 16px; height: 16px; display: block;
}

如果有人知道 "proper" 实现此目的的方法,请告诉我,因为我不确定 configsAPI 中为此组件指定可能会有所帮助,但我无法在互联网上找到一个好的示例:

http://docs.sencha.com/extjs/4.2.2/#!/api/Ext.menu.CheckItem


提前致谢。

我不确定您是否有兴趣在菜单项中使用 tpl:如果您有兴趣,我可以改进下面的代码。并且 Fiddle:https://fiddle.sencha.com/#fiddle/tt3

        Ext.define('Image', {
        extend: 'Ext.data.Model',
        fields: [
            { name:'buttonText1', type:'string' },
            { name:'buttonText2', type:'string' },
            { name:'menuText',    type:'string' }
        ]
    });

    Ext.create('Ext.data.Store', {
        id:'imagesStore',
        model: 'Image',
        data: [
            { buttonText1:'edit', buttonText2: 'add', menuText:'Drawing', id: '1' },
            { buttonText1:'edit', buttonText2: 'add', menuText:'Advanced', id: '2' },
        ]
    });

    var imageTpl = new Ext.XTemplate(
            '<tpl for="."><table>',
            '<div class="menu-row">',
            '<input type="checkbox" id="check{id}">',
            '<button class="{buttonText1}">{buttonText1}</button>',
            '<button class="{buttonText2}" >{buttonText2}</button> {menuText}',
            '</div>',
            '<table></tpl>'
    );

    var dataview = Ext.create('Ext.view.View', {
            itemId: 'idDataView',
            store: Ext.data.StoreManager.lookup('imagesStore'),
            tpl: imageTpl,
            itemSelector: 'div.menu-row',
            listeners: {
                itemclick: function(dataview, record,items) {
                    Ext.each(items.children, function(item) {
                        if (item.id == 'check'+ record.get('id')) {
                            item.checked == false ? item.checked = true : item.checked = false;
                        }
                    });

                }
            }
    });

    var button = Ext.create('Ext.button.Split', {
                text: 'menuButton',
                margin: '100 100 100 100',
                menu: {
                    //plain: true,
                    items: [dataview],

                    listeners: {
                        afterrender: function(menu) {
                            var task = new Ext.util.DelayedTask(function(){
                                Ext.each(menu.down('#idDataView').getEl().dom.children, function(nodes) {
                                    Ext.each(nodes.children, function(node) {
                                        if (node.className == 'edit') {
                                            node.addEventListener('click', function() {
                                               alert('edited'); 
                                            });
                                        } else if (node.className == 'add') {
                                            node.addEventListener('click', function() {
                                               alert('added'); 
                                            });
                                        }
                                    })

                                });
                             });
                            task.delay(100);  
                        }
                    },
                },
        renderTo: Ext.getBody()

    });

我可能会选择使用 container 的方式,并尝试让它发挥作用。我意识到样式可能不如框架提供的那么好,但是通过足够的微调,你可以得到它......只需使用 Sencha CMD、scss 文件和它们的预定义 CSS变量

在这个例子中,我快速取消了按钮的样式,这样它们就可以点击了 icons/areas,然后我将员工记录放在按钮本身上......这并不是最好的方法,但它作品。如果您不喜欢每个单独组件上的监听器,您可以让父容器具有监听器,并且在该函数中您将检查目标的 CSS class... 但除此之外观点。工作 example

Ext.application({
  name: 'Fiddle',

  launch: function() {
    Ext.define('MyPanel', {
      extend: 'Ext.panel.Panel',
      title: 'My Panel',
      renderTo: Ext.getBody(),
      employees: [{
        id: 1,
        name: 'Oscar',
        lastname: 'Jara'
      }, {
        id: 2,
        name: 'Foo',
        lastname: 'Bar'
      }],
      initComponent: function() {
        this.createMenuItems();
        this.callParent();
      },
      createMenuItems: function() {
        var items = [];
        var employees = this.employees;
        if (employees) {
          for (var i = 0; i < employees.length; i++) {
            var employee = employees[i];
            var containerItems = [];
            var checkboxCmp = Ext.create('Ext.form.field.Checkbox', {
              width: 20
            });
            containerItems.push(checkboxCmp);
            containerItems.push({
              xtype: 'button',
              cls: 'my-custom-button',
              employee: employee,
              width: 22,
              text: '',
              iconCls: 'edit',
              listeners: {
                click: this.onClickEditButton
              }
            });
            containerItems.push({
              xtype: 'button',
              cls: 'my-custom-button',
              employee: employee,
              width: 22,
              text: '',
              iconCls: 'delete',
              listeners: {
                click: this.onClickDeleteButton
              }
            });
            containerItems.push({
              xtype: 'component',
              html: '<div style="border-left:1px solid #000;height:100%; display: inline-block;"></div>'
            });
            containerItems.push({
              xtype: 'button',
              cls: 'my-custom-button',
              textAlign: 'left',
              checkboxCmp: checkboxCmp,
              employee: employee,
              flex: 1,
              text: employee.name + ' ' + employee.lastname,
              listeners: {
                click: this.onClickEmployee
              }
            });
            items.push({
              xtype: 'container',
              layout: {
                type: 'hbox',
                align: 'stretch'
              },
              overCls: 'over-item-cls',
              items: containerItems
            });
          }
        }
        this.tools = [{
          xtype: 'button',
          text: 'Employees',
          menu: {
            xtype: 'menu',
            items: items,
            plain: true
          }
        }];
      },
      onClickDeleteButton: function(button, event, eOpts) {
        alert('clicked delete, check console for employee');
        console.log('delete', button.employee);
      },
      onClickEditButton: function(button, event, eOpts) {
        alert('clicked edit, check console for employee');
        console.log('edit', button.employee);
      },
      onClickEmployee: function(button, event, eOpts) {
        alert('employee checkbox changed, check console for employee');
        console.log('employee', button.employee);
        var checkboxCmp = button.checkboxCmp;
        if (checkboxCmp) {
          checkboxCmp.setValue(!checkboxCmp.getValue());
        }

      }
    });
    Ext.create('MyPanel');
  }
});

CSS

.add {
    background-image: url('http://icons.iconarchive.com/icons/awicons/vista-artistic/16/add-icon.png') !important; width: 16px; height: 16px; display: block;
}

.edit {
    opacity: 0.4;
    background-image: url('http://icons.iconarchive.com/icons/designcontest/outline/16/Pencil-icon.png') !important; width: 16px; height: 16px; display: block;
}

.edit:hover,
.delete:hover {
    opacity: 1.0;
}

.delete {
    opacity: 0.4;
    background-image: url('http://icons.iconarchive.com/icons/oxygen-icons.org/oxygen/16/Actions-edit-delete-icon.png') !important; width: 16px; height: 16px; display: block;
}

.over-item-cls {
    background-color: lightblue;
}

a.my-custom-button.x-btn-default-small {
    background: none;
    border: none;
}

a.my-custom-button.x-btn-default-small span {
    color: #ababab;
}

a.my-custom-button.x-btn-default-small:hover span {
    color: black;
}