Extjs Combo - 如何使用GetForm()加载组合值

时间:2013-07-09 02:26:54

标签: extjs combobox extjs4 extjs4.1

我在一个表单中有一个组合框:

        xtype: 'combo',
        id: 'example',
        name: 'ax',
        triggerAction:  'all',
        forceSelection: true,
        editable:       false,
        allowBlank: false,
        fieldLabel:     'example',
        mode: 'remote',
        displayField:'name',
        valueField: 'id',
        store: Ext.create('Ext.data.Store', {
                        fields: [
                            {name: 'id'},
                            {name: 'name'}
                        ],
                        //autoLoad: false,
                        proxy: {
                            type: 'ajax',
                            url: 'example.php',
                            reader: {
                                type: 'json',
                                root: 'rows'
                            }
                        }
            }
        })

我不想要自动加载,因为当我开始时它很慢。

但是当我点击编辑按钮并将值加载到组合

时,我想为组合框设置一个值
this.down('form').getForm().load({            
       url: 'load.php',
       success:function(){
       }
    });
来自load.php的

数据(combe的名字是ax)

{ success:true , data : { ax: '{"id":"0","name":"defaults"}' } }

但那不起作用。我该怎么办呢。

p / s:如果我有autoLoad : true且数据为{ success:true , data : { ax: '0' } }则效果很好。但是当我开始时这很慢。

1 个答案:

答案 0 :(得分:0)

您要做的是确保在尝试设置组合值之前加载组合。

您可以检查组合商店中是否包含任何数据:

if(combo.getStore().getCount() > 0)
{
   //the store has data so it must be loaded, you can set the combo's value
   //doing a form.load will have the desired effect
}
else
{
  //the store isn't loaded yet! You can't set the combo's value
  //form.load will not set the value of the combo
}

如果是,您只需设置值即可。但更有可能的是它不会被加载。

你也可以这样做

//in the controller's init block
this.control({
  "#myEditButton" : {click: this.loadForm}
});

//a function in your controller
loadForm: function(button)
{
   var combo; //get your combo somehow, either via references or via the button 
   combo.getStore().load({
   scope: this,
   callback: 
     function(records, operation, success)
     {
        if(success)
        {
           //load your form here
        }
     }
   });
}

我知道这可能看起来像很多代码,但这是确定组合是否已加载的唯一方法。如果不是,则无法设置其值。

第三种选择只是在打开视图之前显式加载商店。