javascript - Extjs 使用更新的单选项目数组重新加载单选组

标签 javascript jquery extjs radio-button extjs3

我有一个包含 formPanelExt.Window。 formPanel 有一个 radiogroup 项,它加载 radioItemsArray。最初,我根据 myRadioStore 中的数据创建每个 radioItem ,并映射到表单面板中的 radiogroup 。这工作正常,请参阅下面的代码:

this.radioItemsArr = [];
    Ext.each(myRadioStore.data.items, function(itm, i, all) {
        var radioItem = new Ext.form.Radio({
                    id : itm.data.dataId,
                    // custom radio label with text + images
                    boxLabel: 
                        '<b>Data id:</b> ' +  itm.data.dataId + '<br/>' +
                        '<b>Data:</b> ' +  itm.data.dataDetail + '<br/>' +
                        '<p>' + '<img id style="imgStyle" src=\"' + itm.data.url + '\"/></p>',                          
                    name: 'radioName',
                    inputValue: itm.data.dataId ,
                    height: 'auto',
                    checked: false
                });
        this.radioItemsArr.push(radioItem);
    }, this);

this.myForm = new Ext.form.FormPanel({
            border: false,
            id:'my-form',
            frame : true,
            layout : 'fit',
            items: [{
                    autoScroll: true,
                    id: 'myFormId',
                    defaults: {
                        labelStyle: 'font-weight:bold;'
                    },
                    items: [{
                        title: 'Custom Title',
                        items: [{
                            // custom description text set on form load
                            id: 'descId',
                            style : { marginTop: '15px', border:'5px'}
                        }]
                    }, {
                        title: 'Select an item from below',
                        items: [{
                            anchor: '0',
                            autoHeight: true,
                            xtype: 'radiogroup',
                            hideLabels: true,
                            id: 'allRadiosID',
                            items: [
                                this.radioItemsArr
                            ],
                        }]
                    }
                ],
            }],
            buttonAlign :'center',
            buttons: [{
                // save button
            },{
                // cancel button
            }]
        });

这将在第一次正确加载所有单选按钮。但是,当使用来自服务器的新数据更新 myRadioStore 时(当用户单击按钮时会发生这种情况),我希望我的表单面板使用新的单选按钮进行更新。因此,当更新 myRadioStore 时,我会删除 radioItemsArray 中的所有项目,然后通过循环遍历存储并推送到 radioItemsArr 来创建新的 radioItem 。我可以看到 radioItemsArr 有新的单选按钮选项。但是 formpanel 中的 radiogroup 没有刷新。

调用 Ext.getCmp('my-form').doLayout() 似乎不起作用。有什么想法/评论吗?

编辑:我使用的是 extjs 3.4

谢谢!

最佳答案

没有什么可以将商店直接绑定(bind)到 radio 组。您可以向商店添加一个监听器,并使用 update 监听器以编程方式添加新的商店 radio 。

未经测试的代码,但应该已经足够了。

// Add a listener to the store, this can be defined in the `listeners` property of the store config too.

myRadioStore.addListener('update', function () {
    // get the radio group and remove all items
    var radioGroup = Ext.getCmp('allRadiosID');
    radioGroup.removeAll();
    // call the function to renew the radio array.
    getRadioArray();
    radioGroup.add(this.radioItemsArr);
    // Optionally update the container form too
    Ext.getCmp('my-form').doLayout();
}, this);


this.radioItemsArr = [];

function getRadioArray() {
    this.radioItemsArr = [];
    Ext.each(myRadioStore.data.items, function (itm, i, all) {
        var radioItem = new Ext.form.Radio({
            id: itm.data.dataId,
            // custom radio label with text + images
            boxLabel:
                '<b>Data id:</b> ' + itm.data.dataId + '<br/>' +
                '<b>Data:</b> ' + itm.data.dataDetail + '<br/>' +
                '<p>' + '<img id style="imgStyle" src=\"' + itm.data.url + '\"/></p>',
            name: 'radioName',
            inputValue: itm.data.dataId,
            height: 'auto',
            checked: false
        });
        this.radioItemsArr.push(radioItem);
    }, this);
}

getRadioArray();

this.myForm = new Ext.form.FormPanel({
    border: false,
    id: 'my-form',
    frame: true,
    layout: 'fit',
    items: [{
        autoScroll: true,
        id: 'myFormId',
        defaults: {
            labelStyle: 'font-weight:bold;'
        },
        items: [{
            title: 'Custom Title',
            items: [{
                // custom description text set on form load
                id: 'descId',
                style: {
                    marginTop: '15px',
                    border: '5px'
                }
            }]
        }, {
            title: 'Select an item from below',
            items: [{
                anchor: '0',
                autoHeight: true,
                xtype: 'radiogroup',
                hideLabels: true,
                id: 'allRadiosID',
                items: [this.radioItemsArr]
            }]
        }]
    }],
    buttonAlign: 'center',
    buttons: [{
        // save button
    }, {
        // cancel button
    }]
});

关于javascript - Extjs 使用更新的单选项目数组重新加载单选组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25569242/

相关文章:

javascript - 使用 Javascript 将数据添加到 Firebase 的最简单方法

javascript - 表单提交后保留数据库查询结果

php - 使用php检查javascript文件是否已加载?

javascript - 当最后一个元素到达时隐藏下一个按钮

extjs - 如何在 Sencha Touch 应用程序中制作静态标题栏?

javascript - Redactor 没有这样的方法 "destroy"

JavaScript 和 LiveView(TIBCO 实时数据集市)

javascript - jQuery.html() 结果,浏览器不解释为 HTML

javascript - Extjs 6 网格 + JSON

javascript - Jquery Fadein/FadeOut 同时进行