GXT Combobox : type a value that is not in the list and keep it

谁说胖子不能爱 提交于 2020-01-15 09:16:11

问题


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) doesn't do what I want :)


回答1:


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.

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.

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.


Original answer, focusing on ComboBox<String>:

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

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.

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.

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.

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.



来源:https://stackoverflow.com/questions/17148483/gxt-combobox-type-a-value-that-is-not-in-the-list-and-keep-it

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!