Extjs4 使用Ext.form.Basic.loadRecord将数据加载到具有远程存储的组合框字段中
我有一个表单,其中包含多个附加到远程存储的组合框字段:Extjs4 使用Ext.form.Basic.loadRecord将数据加载到具有远程存储的组合框字段中,extjs4,Extjs4,我有一个表单,其中包含多个附加到远程存储的组合框字段: Ext.define('app.ux.form.MyCombo', { extend: 'Ext.form.field.ComboBox', alias: 'widget.mycombo', store: this.store, displayField: 'displayField', valueField: 'valueField', forceSelection: true, a
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是一个基本的Ext.data.store,autoLoad设置为false,autoSync设置为true。当您单击绑定到存储的下拉列表时,存储将加载并显示值列表
当我在包含此下拉列表的BasicForm对象上调用loadRecord并向其传递模型时,它会填充使用本地存储的组合框,但不会加载使用远程存储的组合框。这是因为组合框存储没有加载(autoLoad:false),或者组合框是在表单加载后加载的(autoLoad:true)
我知道这是Ext 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;
}
});
此问题是否已在Ext 4中修复?或者我需要找到另一个兼容Ext 4的插件吗?我的解决方案:
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);
}
}
});
我的解决方案:
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);
}
}
});
这只是另一个覆盖-使用[form].loadRecord([model])方法为我工作 注意:如果使用相反的方式[form].UpdateRocld([model]),则选项的值将不会使用默认分隔符,而只使用“,” 因此-如果您有一个loadRecord,请执行一些操作,然后稍后调用updateRecord记录一个loadRecord,由于分隔符错误,选择将丢失。这就是为什么在这里进行“低于2”的比较
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);
}
}
});
Ext.form.field.ComboBox.override({
设置值:函数(v){
if(this.multiSelect&&typeof v!=“undefined”&&typeof v.split==“function”){
如果(此.value.length<2){
this.setValue(v.split(this.delimiter));
}
}否则{
此.calloverrided(参数);
}
}
});
只是另一个覆盖-对我来说,使用[form].loadRecord([model])方法
注意:如果使用相反的方式[form].UpdateRocld([model]),则选项的值将不会使用默认分隔符,而只使用“,”
因此-如果您有一个loadRecord,请执行一些操作,然后稍后调用updateRecord记录一个loadRecord,由于分隔符错误,选择将丢失。这就是为什么在这里进行“低于2”的比较
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);
}
}
});
Ext.form.field.ComboBox.override({
设置值:函数(v){
if(this.multiSelect&&typeof v!=“undefined”&&typeof v.split==“function”){
如果(此.value.length<2){
this.setValue(v.split(this.delimiter));
}
}否则{
此.calloverrided(参数);
}
}
});
它工作正常,至少在4.0.7中,我不知道您使用的是哪个版本,但如果我在一个尚未加载存储的组合上调用setvalue(),并且存储已自动加载:true,则它会首先加载存储,然后设置值。它工作正常,至少在4.0.7中,我不知道您使用的是哪个版本,但如果我调用setvalue())对于一个尚未加载存储且存储已自动加载的组合:true,则首先加载存储,然后设置值。这比接受的答案好多少?对代码的评论总是受欢迎的。这比公认的答案更好吗?欢迎对代码进行评论。