Javascript 如何获取 ExtJS Combobox 的选定索引
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6014593/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I get the selected index of an ExtJS Combobox
提问by Seb Nilsson
What is the certified way to determine the index of the currently selected item in a ComboBox in ExtJS?
在 ExtJS 的 ComboBox 中确定当前所选项目的索引的认证方法是什么?
Is there a difference on how to do this between ExtJS 3.x and 4?
在 ExtJS 3.x 和 4 之间如何做到这一点有区别吗?
var combo = new Ext.form.ComboBox(config);
var selectedIndex = combo.selectedIndex; // TODO: Implement
if(selectedIndex > 2) {
// Do something
}
Bonus-points for how to add it as a property to the ComboBox-object.
关于如何将其作为属性添加到 ComboBox 对象的加分项。
回答by Jad
I think you'll have to use the combo's store for that. Combos have a private findRecord
method that'll do a simple search over the store by property and value. You can see an example in the sourcecode itself (Combo.js line 1119).
我认为您必须为此使用组合商店。Combos 有一个私有findRecord
方法,可以通过属性和值对存储进行简单的搜索。您可以在源代码中看到一个示例(Combo.js 第 1119 行)。
1) Based on this you could find the selected index this way :
1)基于此,您可以通过以下方式找到所选索引:
var v = combobox.getValue();
var record = combobox.findRecord(combobox.valueField || combobox.displayField, v);
var index = combobox.store.indexOf(record);
2) Or you could bind yourself to the "select" event which is fired with the combo, the record selected and its index as a parameter.
2)或者您可以将自己绑定到组合触发的“选择”事件,选择的记录及其索引作为参数。
3) You could also access the view's getSelectedIndexes() but I doubt it's a good solution (in that I'm not sure it's available all the time)
3) 您也可以访问视图的 getSelectedIndexes() 但我怀疑这是一个很好的解决方案(因为我不确定它是否一直可用)
Finally if you want to extend the combobox object I think this should work (if you go with the first solution) :
最后,如果您想扩展组合框对象,我认为这应该可行(如果您使用第一个解决方案):
Ext.override(Ext.form.ComboBox({
getSelectedIndex: function() {
var v = this.getValue();
var r = this.findRecord(this.valueField || this.displayField, v);
return(this.store.indexOf(r));
}
});
回答by SerEnder
In Ext 4.0.2 the same code would look like:
在 Ext 4.0.2 中,相同的代码如下所示:
Ext.override(Ext.form.ComboBox, {
getSelectedIndex: function() {
var v = this.getValue();
var r = this.findRecord(this.valueField || this.displayField, v);
return(this.store.indexOf(r));
}
});
Jad, you're missing a closing parenthesis on your return statement... just thought you should know.
Jad,你的 return 语句缺少一个右括号……只是想你应该知道。
回答by Pierluigi Ballatore
If you have a combo where valueField is the id used by the combo's store, you can simply avoid the search:
如果您有一个组合,其中 valueField 是组合商店使用的 id,您可以简单地避免搜索:
var v = combobox.getValue();
var record = combobox.findRecord(combobox.valueField || combobox.displayField, v);
var index = combobox.store.indexOf(record);
using this:
使用这个:
var id = combobox.getValue();
var record = store_combobox.getById(id);