如何在GXT ComboBox中添加通用的“All”值

时间:2013-05-14 09:03:22

标签: gwt gxt

我有一个通用的GXT3 ComboBox,它显示了枚举的所有可用值:

public static <T extends Enum<T>> ComboBox<T> buildEnumCombo(Class<T> t){

  ListStore<T> listStore=new ListStore<T>(new EnumModelKeyProvider<T>());

  for(T e:t.getEnumConstants()){
    listStore.add(e);
  }

  ComboBox<T> combo= new ComboBox<T>(listStore, new EnumLabelProvider<T>());
  combo.setTriggerAction(ComboBoxCell.TriggerAction.ALL);

  return combo;
}

这个组合工作正常。

我需要什么:我希望能够添加“全部”值。

我尝试在商店中添加“null”并自定义LabelProvider以显示此特定情况的“全部”,但它无法按预期工作:组合包含预期的行,但它显示空文本而不是“全部” “而且这条线的尺寸不正确。

这是我用于枚举的通用ModelKeyProvider

public class EnumModelKeyProvider<T extends Enum> implements ModelKeyProvider<T> {

@Override
public String getKey(T item) {
  if(item==null){
    return null;
  }else{
    return item.name();
  }
}

我的通用LabelProvider:

public class EnumLabelProvider<T extends Enum<T>> implements LabelProvider<T> {

  @Override
  public String getLabel(T item) {
    if(item==null){
      return "All";
    }else{
      return I18nEnum.i18nEnum(item);
    }
  }
}

2 个答案:

答案 0 :(得分:0)

也许不是您正在寻找的解决方案,但我解决了这个问题,只需将ComboBox的emptyText设置为“All”即可。

答案 1 :(得分:0)

尝试使用SimpleComboBox(在gxt 2.2.5上测试)

private SimpleComboBox<String> createSimpleComboBox(){      
        SimpleComboBox<String> combo = new SimpleComboBox<String>();

        combo.setTypeAhead(true);
        combo.setTriggerAction(TriggerAction.ALL);
        combo.setEditable(editable);
        combo.setForceSelection(true);
        combo.setTemplate(getComboTemplate());
        return combo;
}

private native String getComboTemplate() /*-{
return  [
'<tpl for=".">',
'<tpl if="value == \'\'">',
'<div class="x-combo-list-item" qtip="N/A" qtitle=""></BR></div>',
'</tpl>',
'<tpl if="value != \'\'">',
'<div class="x-combo-list-item" qtip="{value}" qtitle="">{value}</div>',
  '</tpl>',
'</tpl>'
].join("");
}-*/;

public SimpleComboBox<String> buildComboBox(){
            SimpleComboBox<String> combo = createSimpleComboBox();

            combo.add("");
            List<String> list = new ArrayList<String>();
            for(T e:t.getEnumConstants()){
                list.add(e.name());
            }
            combo.add(list);
            return combo;
}