我更喜歡使用select()而不是setValue()。下面的代碼選擇下一個組合框項目:
function comboSelectNextItem(combo, suppressEvent) {
var store = combo.getStore();
var value = combo.getValue();
var index = store.find(combo.valueField, value);
var next_index = index + 1;
var next_record = store.getAt(next_index);
if(next_record) {
combo.select(next_record);
if(! suppressEvent) {
combo.fireEvent('select', combo, [ next_record ]);
}
}
}
代碼,選擇前一個組合框項目:
function comboSelectPrevItem(combo, suppressEvent) {
var store = combo.getStore();
var value = combo.getValue();
var index = store.find(combo.valueField, value);
var prev_index = index - 1;
var prev_record = store.getAt(prev_index);
if(prev_record) {
combo.select(prev_record);
if(! suppressEvent) {
combo.fireEvent('select', combo, [ prev_record ]);
}
}
}
您還可以擴展Ext.form.field.ComboBox包括那些功能(含有少量變化)。例如,您可以將此代碼放在應用程序的推出()函數,因此任何Ext.form.field.Combobox繼承了這兩種方法:
Ext.define("Ext.form.field.ComboBoxOverride", {
"override": "Ext.form.field.ComboBox",
"selectNextItem": function(suppressEvent) {
var store = this.getStore();
var value = this.getValue();
var index = store.find(this.valueField, value);
var next_index = index + 1;
var next_record = store.getAt(next_index);
if(next_record) {
this.select(next_record);
if(! suppressEvent) {
this.fireEvent('select', this, [ next_record ]);
}
}
},
"selectPrevItem": function(suppressEvent) {
var store = this.getStore();
var value = this.getValue();
var index = store.find(this.valueField, value);
var prev_index = index - 1;
var prev_record = store.getAt(prev_index);
if(prev_record) {
this.select(prev_record);
if(! suppressEvent) {
this.fireEvent('select', this, [ prev_record ]);
}
}
}
});
然後,你可以在你的代碼的任何地方使用這些方法:
var combo = ... // the combo you are working on
combo.selectNextItem(); // select the next item
combo.selectPrevItem(); // select the previous item
這是一個甜蜜的小部件! –
謝謝!一旦我發現一些怪癖,我會嘗試將它製作成一個自定義組件。 – dmackerman