如何获取 ExtJS Combobox 的选定索引

发布于 2024-11-07 07:06:44 字数 294 浏览 0 评论 0原文

确定 ExtJS 中组合框中当前所选项目索引的经过认证的方法是什么?

ExtJS 3.x 和 4 之间的执行方式有区别吗?

var combo = new Ext.form.ComboBox(config);
var selectedIndex = combo.selectedIndex; // TODO: Implement
if(selectedIndex > 2) {
    // Do something
}

关于如何将其作为属性添加到 ComboBox 对象的奖励点。

What is the certified way to determine the index of the currently selected item in a ComboBox in ExtJS?

Is there a difference on how to do this between ExtJS 3.x and 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

深府石板幽径 2024-11-14 07:06:45

如果您有一个组合,其中 valueField 是组合商店使用的 id,您可以简单地避免搜索:

var v = combobox.getValue();
var record = combobox.findRecord(combobox.valueField || combobox.displayField, v);
var index = combobox.store.indexOf(record);

使用以下命令:

var id = combobox.getValue();
var record = store_combobox.getById(id);

If you have a combo where valueField is the id used by the combo's store, you can simply avoid the search:

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);
却一份温柔 2024-11-14 07:06:44

我想你必须使用组合商店才能做到这一点。 Combos 有一个私有的 findRecord 方法,可以按属性和值对存储进行简单的搜索。您可以在源代码本身中查看示例(Combo.js 第 1119 行)。

1)基于此,您可以通过这种方式找到选定的索引:

var v = combobox.getValue();
var record = combobox.findRecord(combobox.valueField || combobox.displayField, v);
var index = combobox.store.indexOf(record);

2)或者您可以将自己绑定到“select”事件,该事件是用组合、选定的记录及其索引作为参数触发的。

3)您还可以访问视图的 getSelectedIndexes() 但我怀疑这是一个好的解决方案(因为我不确定它始终可用)

最后,如果您想扩展组合框对象,我认为这应该可行(如果您去与第一个解决方案):

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));
  }
});

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).

1) Based on this you could find the selected index this way :

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.

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)

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));
  }
});
她说她爱他 2024-11-14 07:06:44

在 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,您的 return 语句中缺少右括号...只是认为您应该知道。

In Ext 4.0.2 the same code would look like:

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文