加载json存储后设置组合框值

时间:2013-06-20 15:58:49

标签: extjs4

我有一个Combobox,我从控制器加载它(下面的代码片段)

Ext.getCmp("combovalue2").getStore().loadRawData(value2);

value2是我从模型中获得的对象并且它成功运行(我在组合框中验证了这一点)。以下是我的表单面板中的组合框的代码段

{
                xtype:'combo',
                id:"combovalue2",
                name: 'cpa',
                cls:'extraComboBox',
                forceSelection: true,
                queryMode: 'local',
                fieldLabel: 'Alliance',
                displayField: 'label',     
                valueField: 'numAe',
                emptyText: 'select Alliance',
                store: new Ext.data.JsonStore({fields: ["numAe","label"]})
           }

这就是我的json的样子

"cpaList": [
        {
            "label": "A Communications                   ",
            "id": "USF  ",
            "numOff": 999,
            "numAe": 483,
            "pctFeeCalc": 0,
            "pctFeeBasisPoint": 0
        },
        {
            "label": "Ac Neilson, Inc                    ",
            "id": "MMV  ",
            "numOff": 999,
            "numAe": 876,
            "pctFeeCalc": 20,
            "pctFeeBasisPoint": 0
        },
        {
            "label": "Acer Worldwide Inc.                ",
            "id": "CAA  ",
            "numOff": 999,
            "numAe": 619,
            "pctFeeCalc": 0,
            "pctFeeBasisPoint": 0
        },
        {
            "label": "Advantech, Inc.                    ",
            "id": "SLE  ",
            "numOff": 999,
            "numAe": 592,
            "pctFeeCalc": 0,
            "pctFeeBasisPoint": 0
        },
        {
            "label": "Afo Ltd                            ",
            "id": "NAN  ",
            "numOff": 999,
            "numAe": 959,
            "pctFeeCalc": 25,
            "pctFeeBasisPoint": 0
        },
        {
            "label": "All Systems Go                     ",
            "id": "BCS  ",
            "numOff": 999,
            "numAe": 944,
            "pctFeeCalc": 25,
            "pctFeeBasisPoint": 0
        },
        {
            "label": "Alyssa Anne Lock                   ",
            "id": "GRE  ",
            "numOff": 999,
            "numAe": 369,
            "pctFeeCalc": 20,
            "pctFeeBasisPoint": 0
        }

模型

{name: 'cpa', mapping: 'cpaList'}

但我很难显示渲染组合框时应显示的值。我想将值设置为来自json的组合框(这意味着value2是一个对象数组,我想要设置的是一个字符串),同时还显示我正在做的组合框中的数据数组通过loadRawData(value2)

3 个答案:

答案 0 :(得分:0)

你不应该有loadRawData。从代码的外观来看,您的商店需要更改,或者您需要使用返回的部分JSON数据。

你的根是'cpaList'。商店不知道该怎么办。如果您希望保持代码的原样,请执行以下操作:

Ext.getCmp("combovalue2").getStore().loadData(value2.cpaList);

否则,您必须更改商店以匹配您的数据。如果你实际上定义了这个商店,然后将你的组合商店设置为定义的商店,那么它将在渲染时自动加载。然后,您不必激发单独的AJAX请求,并且您的代码现在都与您的商店相关。

var store = Ext.create('Ext.data.Store', {
    autoLoad: true,
    proxy: {
        type: 'ajax',
        url: '/whateveryoururlis',
        reader: {
            type: 'json',
            root: 'cpaList' // your root of your data containing the array of fields for the store
        }
    },
    fields: ["numAe","label"]
});

答案 1 :(得分:0)

我修好了。 我在模型1中使用2个不同的对象是通过loadData(modelObject)加载存储,然后另一个在combobox中保留值 - name config即cpa。

模型定义:

{name: 'cpaList', mapping: 'cpaList'},
{name: 'cpa', mapping: 'feeSetupCtg.cpaNo'}

控制器定义:

var value2 = model.get('cpaList');
Ext.getCmp("combovalue2").getStore().loadData(value2);

Combobox defn:

{
    xtype:'combo',
    id:"combovalue2",
    name: 'cpa',
    cls:'extraComboBox',
    forceSelection: true,
    queryMode: 'local',
    fieldLabel: 'Alliance',
    displayField: 'label',     
    valueField: 'numAe',
    emptyText: 'select Alliance',
    editable: false,
    store: new Ext.data.JsonStore({fields: ["numAe","label"]})
}

答案 2 :(得分:0)

在控制器中添加:

Ext.getCmp("combovalue2").getStore().on("load", this.setDefaultComboValue, this);

...

setDefaultComboValue: function(store, records, successful, eOpts)
{
   if(successful){
        Ext.getCmp("combovalue2").setValue(records[0]); //records[0] or whatever record you want to load
   }
}

请注意,在Extjs 4.2中,loadRawData方法不再触发load事件。 我建议您使用load方法加载JSON数据。