GXT Combobox:键入一个不在列表中的值并保留它 [英] GXT Combobox : type a value that is not in the list and keep it

查看:177
本文介绍了GXT Combobox:键入一个不在列表中的值并保留它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试让一个GXT组合框(3.0.1)接受我输入的文本,但它会一直在模糊处删除它。是不是可以告诉组合框接受一个不是它的liststore的一部分的值?

I try to have a GXT combobox (3.0.1) accepting text I type in it but it keeps deleting it on blur. Isn't it possible to tell the combobox to accept a value that's not a part of its liststore ?

PS:setForceSelection(false)不会做我想要的: )

PS: setForceSelection(false) doesn't do what I want :)

推荐答案

评论中提到的错误集中在 ComboBox< String>

The bug mentioned in the comments is focused on ComboBox<String>, since there is a natural mapping from the data the user typed in (aka a String) to the data in the combo (also a String). If you don't have such a natural mapping, the latest release doesn't help you a lot - that said, you can still do this.

组合框使用PropertyEditor用于在解析数据时呈现数据,查找并返回存储区中的值,还用于将任意数据转换为T值。它委托给 ComboBoxCell.selectByValue(String)来查找匹配值。

The ComboBox uses the PropertyEditor to render data, find and return a value in the store when parsing data, but also for turning arbitrary data into a T value. It delegates to ComboBoxCell.selectByValue(String) to find a matching value.

应该可以覆盖该方法并提供新的行为,或基于ComboBox中的内置方法创建自定义的 PropertyEditor< T> ,如果没有任何方法可以根据您的规则创建新对象找到。也许是这样的:

It should be possible to either override that method and provide new behavior, or make a custom PropertyEditor<T> based on the built-in one in ComboBox that will create new objects based on your rules if none can be found. Something like this perhaps:

ComboBox<MyObject> combo = new ComboBox<MyObject>(new ComboBoxCell<MyObject>(store, labelProvider){
  @Override
  protected MyObject selectByValue(String value) {
    MyObject val = super.selectByValue(value);
    if (val == null) {
      // custom logic for creating new objects goes here
      val = new MyObject();
      val.setDisplayName(value);
    }
    return val;
  }
});

如果您最终这么做了,请考虑以这种方式考虑它,以便您可以把一个委托交给组合框,它负责将一个自定义对象从字符串中提取出来。

If you end up doing this a lot, consider factoring it out in such a way so that you can hand off a delegate to the combobox that is responsible for making a custom object out of your string.

原始答案,重点on ComboBox< String>

Original answer, focusing on ComboBox<String>:

除了评论中提到的错误报告之外,同样的问题(也许也应该关闭) - http://www.sencha.com/forum/showthread.php?196281-GXT-3-rc2-ComboBox-setForSeSelection%28false%29-does-not-work / 2页。这不仅讨论错误本身,以及它为什么是一个问题,而且还讨论了使用PropertyEditor获取其他可能不存在的值的实现 - 您应该能够调整它以使其更具体您的情况。

Besides the bug report mentioned in the comments, there is another bug that touches on the same issue (that should perhaps also be closed) - http://www.sencha.com/forum/showthread.php?196281-GXT-3-rc2-ComboBox-setForceSelection%28false%29-does-not-work/page2. This discusses not only the bug itself, and why it is an issue, but then also a possible implementation to use of PropertyEditor to grab on to other values that may not be in there - you should be able to tweak it to make it more specific to your case.

我从该线索的笔记(复制到此处以避免链接死亡):

My notes from that thread (copied here to avoid link death):


使它成为 ComboBox< String> ,而不是试图告诉 ComboBox 字符串不是一个错误,而是给它一个自定义的 PropertyEditor< String> PropertyEditor 的用途(名称是2.x之前的保留,在编辑框架之类的事情发生之前)是将用户输入的文本转换为值,可以从代码中使用,反之亦然 - 如何将模型对象中的值打印到屏幕上的文本中。

Make it a ComboBox<String>, and instead of trying to tell the ComboBox that the new string is merely not an error, give it a custom PropertyEditor<String> instead. The purpose of a PropertyEditor (the name is a holdover from 2.x, before there was such a thing as the Editor framework) is to turn the text the user enters into a value that can be used from code, and vice versa - how to print the values from model objects into text on the screen.

默认 PropertyEditor 对于 ComboBox (实际上对于 ComboBoxCell )是 ComboPropertyEditor - 它使用受保护的方法 T selectByValue(String)来试图找出商店中什么值与当前字符串匹配。

The default PropertyEditor for ComboBox (actually for the ComboBoxCell) is ComboPropertyEditor - it uses the protected method T selectByValue(String) to try to figure out what value in the store matches the current string.

如我所说,如果您想要支持任何字符串值,那么这是一种向Store添加值的好方法,如果它适合,或者只是说'是的,那个字符串是一个字符串和字符串都是值' - 不需要针对商店中的每个值进行测试。

If, as I said, you want to support any string value, this is a great way to add a value to the Store if it fits, or to just say 'yep, that string is a string, and strings are values' - no need to test against every value in the store.

这就是说,我们当前的行为clearValueOnParseError = false显然没有'没有意义,它是曲ite可能forceSelection没有意义给出了2.x和3之间的差异。我目前正在关注如何让clearValueOnParseError工作 - 并试图确保它足以满足forceSelection行为。

That said, yes, our current behavior on clearValueOnParseError=false clearly doesn't make sense, and it is quite possible that forceSelection doesn't make sense given the differences between 2.x and 3. I'm currently focusing on how we can make clearValueOnParseError work - and trying to ensure that it will be enough to satisfy the forceSelection behavior.

这篇关于GXT Combobox:键入一个不在列表中的值并保留它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆