繁体   English   中英

在EXTjs 4中动态添加按钮单击事件

[英]adding buttons click event dynamically in EXTjs 4

我的网格布局中有一个操作列图标,单击该图标,我打开了一个窗口表单面板。 然后单击该图标,我将构建一个fieldset动态布局。 字段集包括按钮和图像。

//Below is the code
for (var j = 0; j < productPictures.length; j++) {
    var values = productPictures[j].value;
    var idPic = productPictures[j].idProductPicture;
    alert(idPic);
    fieldset.add({
        xtype: 'container',
        layout: 'hbox',
        items: [{
            xtype: 'box',
            autoEl: {
                tag: 'img',
                src: '/files/product/' + idProduct + '/picture/100x75/' + values,
                height: '50px',
                width: '50px'
            }
        }, {
            xtype: 'button',
            name: 'remove' + idPic,
            width: 200,
            text: 'Remove',
            handler: function () {
                alert(idPic);
                Ext.Ajax.request({
                    url: '/admin/productpicture?id=' + idPic + '&memberId=' + idMember + '&idProduct=' + idProduct,
                    method: 'DELETE',
                    success: function (response) {
                        Ext.Msg.alert('Status', 'Image Deleted succesfullly.');
                    },
                    failure: function () {
                        Ext.Msg.alert('Status', 'There is an error.');
                    }
                })
            }

        }]
    })

}

我遇到的问题是我希望按钮的处理程序接受每次循环时都带有的动态值。但是,在每个按钮上单击时,其给出的值是相同的(循环的第一个值)。因此如何使其动态化,这样我可以动态地为每个图像传递参数。

您遇到了范围界定问题。 您需要做的是将变量保存在按钮配置中。

您可以执行以下操作:

{
    xtype: 'button',
    name: 'remove' + idPic,
    idPic: idPic,
    width: 200,
    text: 'Remove',
    handler: function (button) {
        alert(button.idPic);
        //the rest...
    }
}

暂无
暂无

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

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