简体   繁体   English

将侦听器添加到Sencha Touch 1.1的Ext.List中的自定义元素

[英]add listeners to custom elements in Ext.List in Sencha Touch 1.1

I'm trying to make a list with -/+ buttons. 我正在尝试使用-/ +按钮列出列表。 Each item on a list will have this pair of buttons. 列表上的每个项目都会有这对按钮。 I'm trying to add listeners to the button elements (actually they are Divs) but with minimum luck so far. 我正在尝试将侦听器添加到按钮元素(实际上它们是Divs),但到目前为止运气不佳。

What I've done so far is catch the ' beforerender ' event of the list, loop through all the list elements and add a click listener for each item on the list. 到目前为止,我所做的是捕获列表的“ beforerender ”事件,遍历所有列表元素,并为列表中的每个项目添加一个点击侦听器。 But the problem is that when I navigate out, to another screen/panel and the back in the list, the beforerender event is not thrown. 但是问题在于,当我导航到另一个屏幕/面板以及列表的后部时,不会引发beforerender事件。 So the listeners for the new list items are not registered. 因此,未注册新列表项的侦听器。

I've tried to make a workaround and force the list to be refreshed on each ' beforeactivate ' event of the panel. 我尝试过一种解决方法,并在面板的每个“ beforeactivate ”事件上强制刷新列表。 But this causes the list to refresh for 4-5 times (don't know why). 但这会使列表刷新4-5次(不知道为什么)。

I must be going the wrong way on this one... Maybe the solution is a custom component but the documentation on custom components is quite poor. 我肯定在这方面走错了路...也许解决方案是自定义组件,但是有关自定义组件的文档却很差。

Ok, I've found the solution, for anyone having the same problem: 好的,对于任何有相同问题的人,我都找到了解决方案:

I caught the activate and beforeactivate event of the Panel and I retrieve a reference to the list by using an id on the list, (eg: lstProducts ) and then getting it by this.down('#productsList'); 我捕获了Panel的activatebeforeactivate事件,并通过使用列表上的ID(例如lstProducts ),然后通过this.down('#productsList');获取该列表,来检索对该列表的引用this.down('#productsList'); .

UPDATE UPDATE

Here is the template for the list, along with the list and the method that setups the listeners. 这是列表的模板,以及列表和设置侦听器的方法。

The template for the list 列表模板

    productsTemplate = new Ext.XTemplate(
            '<tpl for=".">' +
                '<table width="100%" cellpadding="0" cellspacing="0"><tr>' +
                '<td>' +
                '<div class="product">{Name}</div>' +
                '</td>' +
                '<td rowspan="2" width="10%" align="right">' +
                '<table cellpadding="0" cellspacing="0">' +
                '<tr><td><div class="btn btnOrder minus" id="btnRemove{#}" > - </div></td> ' +
                '<td><input type="text" disabled id="pr-{Id}" value="0" class="productQuantity"/> </td>' +
                '<td><div class="btn btnOrder plus" id="btnAdd{#}"> + </div></td></tr></table> ' +
                '</td></tr><tr><td>' +
                '<table class="orderExtra"><tr>' +
                '<td class="sideOrderIcon" id="prSideOrderIcon-{Id}">&nbsp;</td>' +
                '<td valign="middle"><input type="text" disabled id="prSideOrder-{Id}" value="0" class="extraQuantity"/></td>' +
                '<td class="extraIcon" id="prExtraIcon-{Id}">&nbsp;</td>' +
                '<td><input type="text" disabled id="prExtra-{Id}" value="0" class="extraQuantity"/></td>' +
                '<td class="price">{Price} €</td>' +
                '</tr></table>' +
                '</td></tr>' +
                '</table>' +
                '</tpl>'
        );

The list itself and the action listeners setup at activate of the view. 列表本身和动作侦听器在视图激活时设置。

productsList = new Ext.List({
            store:this.store,
            scope:this,
            refreshed:false,
            id:'productsList',
            disableSelection:true,
            itemTpl:productsTemplate })

Ext.apply(this, {
            layout:'fit',
            scroll:'vertical',
            items:[productsList],
            listeners:{
                activate:this.setupQuantityActionListeners
            }

At some other part of the code put the below method. 在代码的其他部分放置以下方法。

setupQuantityActionListeners:function () {
    var panel = this.down('#productsList');
    // loop all the list items to add listeners to the buttons
    panel.all.elements.forEach(function (item, index) {
        //get the button references
        var btnAdd = Ext.get('btnAdd' + (index + 1));
        var btnRemove = Ext.get('btnRemove' + (index + 1));
        //get the product model
        var product = app.stores.Products.getAt(index);
        var tbxQuantity = Ext.get('pr-' + product.data.Id);
        //get the running quantity
        if (tbxQuantity)
            var quantity = tbxQuantity.getValue();
        //add - remove quantity
        if (btnAdd) {
            btnAdd.on('click', function () {
                tbxQuantity.dom.value = ++quantity;
                Ext.dispatch({
                    controller:'Orders',
                    action:'addOrderItem',
                    product:product,
                    quantity:quantity
                })
            })
        }
        if (btnRemove) {
            btnRemove.on('click', function () {
                if (quantity > 0) {
                    tbxQuantity.dom.value = --quantity;
                    Ext.dispatch({
                        controller:'Orders',
                        action:'addOrderItem',
                        product:product,
                        quantity:quantity
                    })
                }
            })
        }
    });
}

Be very careful with the scoping in general. 总体上要非常小心。 If you want to get something from the view and you try to get it at a component listener method, you must set scope:this to that component, so as to get a reference of the view when inside the listener method of the component. 如果要从视图中获取某些内容并尝试通过组件侦听器方法获取它,则必须将scope:this设置为该组件,以便在该组件的侦听器方法内部获取视图的引用。

Hope it helps 希望能帮助到你

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

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