2011-06-09 75 views
1

我有具有连接到远程存储多个组合框字段的表单:使用Ext.form.Basic.loadRecord将数据加载到组合框的字段远程商店

Ext.define('app.ux.form.MyCombo', { 
    extend: 'Ext.form.field.ComboBox', 
    alias: 'widget.mycombo', 
    store: this.store, 
    displayField: 'displayField', 
    valueField: 'valueField', 
    forceSelection: true, 
    autoSelect: true, 
    initComponent: function() { 
     this.addEvents('selectitem'); 
     this.enableBubble('selectitem'); 
     this.callParent(arguments); 
     this.listeners = { 
      change: function(field, value) { 
       this.fireEvent('selectitem', field, value); 
      } 
     } 
    } 
}) 


       fieldLabel: 'DisabilityType', 
       name: 'f_disability_type', 
       xtype: 'combo', 
       valueField: 'valueField', 
       displayField: 'displayField', 
       forceSelection: true, 
       autoSelect: true, 
       store: 'DisabilityTypes' 

DisabilityTypes是一个基本的分机将autoLoad设置为false并将autoSync设置为true的.data.store。当您点击与商店关联的下拉菜单时,商店会加载并显示值列表。

当我在包含此下拉列表的BasicForm对象上调用loadRecord并将其传递给模型时,它会填充使用本地存储的组合框,但不会加载使用远程存储的组合框。这是因为组合框存储未加载(autoLoad:false),或者在窗体加载后加载组合框(autoLoad:true)。

我知道,这是在内线3.3.x一个问题,有提出解决它的一个插件:

/** 
* When combo box is used on a form with dynamic store (remote mode) 
* then sometimes the combobox store would load after the form data. 
* And in that case the setValue method of combobox will not 
* set the combobox value properly. This override makes sure that the 
* combobox store is completely loaded before calling the setValue method. 
*/ 
Ext.override(Ext.form.ComboBox, { 
    setValue : function(v){ 
     var text = v; 
     if(this.valueField){ 
      if(!Ext.isDefined(this.store.totalLength)){ 
       this.store.on('load', this.setValue.createDelegate(this, arguments), null, {single: true}); 
       if(this.store.lastOptions === null){ 
        var params; 
        if(this.valueParam){ 
         params = {}; 
         params[this.valueParam] = v; 
        }else{ 
         var q = this.allQuery; 
         this.lastQuery = q; 
         this.store.setBaseParam(this.queryParam, q); 
         params = this.getParams(q); 
        } 
        this.store.load({params: params}); 
       } 
       return; 
      } 
      var r = this.findRecord(this.valueField, v); 
      if(r){ 
       text = r.data[this.displayField]; 
      }else if(this.valueNotFoundText !== undefined){ 
       text = this.valueNotFoundText; 
      } 
     } 
     this.lastSelectionText = text; 
     if(this.hiddenField){ 
      this.hiddenField.value = v; 
     } 
     Ext.form.ComboBox.superclass.setValue.call(this, text); 
     this.value = v; 
    } 
}); 

有这个问题被固定在内线4?或者我需要找到另一个与Ext 4兼容的插件?

+0

它工作正常,至少在4.0.7中“低于2”的比较,我不,你知道使用至极的版本,但如果我叫的setValue()的组合如果商店尚未加载,并且商店具有autoLoad:true,则会先加载商店,然后再设置该值。 – VoidMain 2011-11-18 18:57:55

回答

1

我的解决办法:

Ext.form.field.ComboBox.override({ 
    setValue: function(v) { 
     v = (v && v.toString) ? v.toString() : v; 
     if(!this.store.isLoaded && this.queryMode == 'remote') { 
      this.store.addListener('load', function() { 
       this.store.isLoaded = true; 
       this.setValue(v); 
      }, this); 
      this.store.load(); 
     } else { 
      this.callOverridden(arguments); 
     } 
    } 
}); 
0
Ext.form.field.ComboBox.override({ 
    setValue: function(value) { 
     if(typeof value != 'object' && !Ext.isEmpty(value) && !this.store.isLoaded && this.queryMode == 'remote') { 
      value = (value && value.toString) ? value.toString() : value; 
      this.store.addListener('load', function() { 
       this.store.isLoaded = true; 
       this.setValue(value); 
      }, this); 
      this.store.load(); 
     } else { 
      this.callOverridden(arguments); 
     } 
    } 
}); 
+0

这比接受的答案好得多吗?对代码的评论总是受欢迎的。 – 2012-10-30 03:54:09

0

只是一个覆盖 - 为我工作,使用[表] .loadRecord([模型])方法。

要小心:如果使用相反的方式[form] .updateReocrd([model]),则选项的值不会使用默认分隔符,而只会使用','。

所以 - 如果你有一个loadRecord,做一些事情,稍后再调用updateRecord一个载入记录,由于分隔符错误,选择将会丢失。 taht就是为什么正在这里进行

Ext.form.field.ComboBox.override({ 
    setValue: function(v) { 
     if (this.multiSelect && typeof v != 'undefined' && typeof v.split == 'function'){ 
      if (this.value.length < 2){ 
       this.setValue(v.split(this.delimiter)); 
      } 
     } else { 
      this.callOverridden(arguments); 
     } 
    } 
});