简体   繁体   English

如何将单击事件处理程序添加到 Ext JS 选择器子元素?

[英]How to add a click event handler to Ext JS picker child element?

I want to be able to add a link into the combo box drop down list that does something.我希望能够在组合框下拉列表中添加一个链接来执行某些操作。

I did this by changing the template.我通过更改模板来做到这一点。 However adding a click event listener doesn't work, does anyone know why?但是添加单击事件侦听器不起作用,有人知道为什么吗?

 items: [{ xtype: 'combobox', fieldLabel: 'Combo with extra option', store: { fields: ['value', 'display'], data: [ { value: 1, display: 'First' }, { value: 2, display: 'Second' } ] }, valueField: 'value', displayField: 'display', tpl: Ext.create('Ext.XTemplate', '<div style="background-color: lightblue;" class="searchItem">Search</div>', '<ul class="x-list-plain">', '<tpl for=".">', '<li role="option" class="x-boundlist-item">{display} - {value}</li>', '</tpl></ul>' ), listeners: { boxready: function(field) { var picker = field.getPicker(); picker.on('boxready', function() { var searchItem = Ext.get(this.getEl().query('.searchItem')[0]); searchItem.on('click', function() { alert('test'); });//this doesn't do anything }) } }

you need to add a listener configuration to your listConfig instead of the combobox.您需要将侦听器配置添加到listConfig而不是组合框。 That way you access the actual list instead of the entire combobox.这样您就可以访问实际列表而不是整个组合框。

Secondly, have a look at the general configuration for an event listener .其次,看看 事件侦听器的一般 配置 You need to make use of the options element and delegate .您需要使用 options elementdelegate

The element option requires an Ext.dom.Element to add a listener to. element选项需要一个 Ext.dom.Element 来添加一个监听器。 In your case, you can use el as value to add the entire element.在您的情况下,您可以使用el作为值来添加整个元素。

The delegate option requires a selector like div.searchItem . delegate选项需要像div.searchItem这样的选择器。 Putting this all together would create the following listConfig :将所有这些放在一起将创建以下listConfig

            listConfig: {
                listeners: {
                    click: {
                        element: 'el',
                        delegate: 'div.searchItem',
                        fn: function (event, target) {
                            alert('test');
                        }
                    }
                }
            }

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

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