在組合有一個叫assertValue
魔術方法,它往往會導致一些問題:
assertValue: function() {
var me = this,
value = me.getRawValue(),
rec, currentValue;
if (me.forceSelection) {
if (me.multiSelect) {
// For multiselect, check that the current displayed value matches the current
// selection, if it does not then revert to the most recent selection.
if (value !== me.getDisplayValue()) {
me.setValue(me.lastSelection);
}
} else {
// For single-select, match the displayed value to a record and select it,
// if it does not match a record then revert to the most recent selection.
rec = me.findRecordByDisplay(value);
if (rec) {
currentValue = me.value;
// Prevent an issue where we have duplicate display values with
// different underlying values.
if (!me.findRecordByValue(currentValue)) {
me.select(rec, true);
}
} else {
me.setValue(me.lastSelection);
}
}
}
me.collapse();
}
基本上當你不使用multiSelect
然後lastSelection
總是使用,當您嘗試在商店找不到的設定值。 我通常會添加虛擬記錄來存儲,這對應於空值。如果您需要將allowBlank
設置爲false,問題將會回來。
作爲一種替代添加僞記錄可以覆蓋assertValue
方法是這樣的:
assertValue: function() {
var me = this,
value = me.getRawValue();
if (me.forceSelection && me.allowBlank && Ext.isEmpty(value)) {
me.setValue(value);
me.collapse();
return;
}
this.callParent(arguments);
}