Java 如何使用GWT动态更新SelectionCell中的选项?

Java 如何使用GWT动态更新SelectionCell中的选项?,java,gwt,Java,Gwt,我试图有一个表,显示用户输入的数据以及编辑数据。我已经知道了如何使用文本(即,他们可以编辑表格中某个内容的名称),但我无法让它使用选择单元格 如果选择单元格中的项目是预定义的,则它可以正常工作,但在创建单元格后,我无法动态更新单元格中的项目以包含新内容 为了解释更多,我有一个“类型”栏。用户在表中输入具有给定类型的项,但这也可以在以后添加新类型。当他们单击类型列中的项目时,我希望下拉框包含他们输入的所有新类型,但我不知道如何实现这一点 这是我到目前为止的代码(它没有像我希望的那样更新)。在用户输

我试图有一个表,显示用户输入的数据以及编辑数据。我已经知道了如何使用文本(即,他们可以编辑表格中某个内容的名称),但我无法让它使用选择单元格

如果选择单元格中的项目是预定义的,则它可以正常工作,但在创建单元格后,我无法动态更新单元格中的项目以包含新内容

为了解释更多,我有一个“类型”栏。用户在表中输入具有给定类型的项,但这也可以在以后添加新类型。当他们单击类型列中的项目时,我希望下拉框包含他们输入的所有新类型,但我不知道如何实现这一点

这是我到目前为止的代码(它没有像我希望的那样更新)。在用户输入新类型后,record.getTypeList()将包含其他条目

SelectionCell editTypeComboBox = new SelectionCell(record.getTypeList());

    Column<Assignment, String> typeColumn = new Column<Assignment, String>(editTypeComboBox) {
        @Override
        public String getValue(Assignment object) {
            return object.getType();
        }
    };
    typeColumn.setFieldUpdater(new FieldUpdater<Assignment, String>() {

        @Override
        public void update(int index, Assignment object, String value) {
            int row = index;
            String newType = value;
            record.editAssignType(row, newType);
            updateClassGradeLabel();
            log.info("Set type to "
                    + value);
            cellTable.redraw();
        }
    });

    cellTable.addColumn(typeColumn, "Type");
SelectionCell编辑类型组合框=新建SelectionCell(record.getTypeList());
列类型Column=新列(editTypeComboBox){
@凌驾
公共字符串getValue(赋值对象){
返回object.getType();
}
};
setFieldUpdater(新的FieldUpdater(){
@凌驾
公共void更新(int索引、赋值对象、字符串值){
int行=索引;
字符串newType=值;
record.editAssignType(行,新类型);
updateClassGradeLabel();
log.info(“将类型设置为”
+价值);
cellTable.redraw();
}
});
cellTable.addColumn(typeColumn,“Type”);
编辑: 多亏Peter Knego foe帮我解决了这个问题。以下是修改后的DynamicSelectionCell类(如果有兴趣):

/*
 * Copyright 2010 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.google.gwt.cell.client;

import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Element;
import com.google.gwt.dom.client.NativeEvent;
import com.google.gwt.dom.client.SelectElement;
import com.google.gwt.safehtml.client.SafeHtmlTemplates;
import com.google.gwt.safehtml.shared.SafeHtml;
import com.google.gwt.safehtml.shared.SafeHtmlBuilder;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * A {@link Cell} used to render a drop-down list.
 */
public class DynamicSelectionCell extends AbstractInputCell<String, String> {

  interface Template extends SafeHtmlTemplates {
    @Template("<option value=\"{0}\">{0}</option>")
    SafeHtml deselected(String option);

    @Template("<option value=\"{0}\" selected=\"selected\">{0}</option>")
    SafeHtml selected(String option);
  }

  private static Template template;

  private HashMap<String, Integer> indexForOption = new HashMap<String, Integer>();

  private final List<String> options;

  /**
   * Construct a new {@link SelectionCell} with the specified options.
   *
   * @param options the options in the cell
   */
  public DynamicSelectionCell(List<String> options) {
    super("change");
    if (template == null) {
      template = GWT.create(Template.class);
    }
    this.options = new ArrayList<String>(options);
    int index = 0;
    for (String option : options) {
      indexForOption.put(option, index++);
    }
  }

  public void addOption(String newOp){
      String option = new String(newOp);
      options.add(option);
      refreshIndexes();
  }

  public void removeOption(String op){
      String option = new String(op);
      options.remove(indexForOption.get(option));
      refreshIndexes();
  }

  private void refreshIndexes(){
        int index = 0;
        for (String option : options) {
          indexForOption.put(option, index++);
        }
  }

  @Override
  public void onBrowserEvent(Context context, Element parent, String value,
      NativeEvent event, ValueUpdater<String> valueUpdater) {
    super.onBrowserEvent(context, parent, value, event, valueUpdater);
    String type = event.getType();
    if ("change".equals(type)) {
      Object key = context.getKey();
      SelectElement select = parent.getFirstChild().cast();
      String newValue = options.get(select.getSelectedIndex());
      setViewData(key, newValue);
      finishEditing(parent, newValue, key, valueUpdater);
      if (valueUpdater != null) {
        valueUpdater.update(newValue);
      }
    }
  }

  @Override
  public void render(Context context, String value, SafeHtmlBuilder sb) {
    // Get the view data.
    Object key = context.getKey();
    String viewData = getViewData(key);
    if (viewData != null && viewData.equals(value)) {
      clearViewData(key);
      viewData = null;
    }

    int selectedIndex = getSelectedIndex(viewData == null ? value : viewData);
    sb.appendHtmlConstant("<select tabindex=\"-1\">");
    int index = 0;
    for (String option : options) {
      if (index++ == selectedIndex) {
        sb.append(template.selected(option));
      } else {
        sb.append(template.deselected(option));
      }
    }
    sb.appendHtmlConstant("</select>");
  }

  private int getSelectedIndex(String value) {
    Integer index = indexForOption.get(value);
    if (index == null) {
      return -1;
    }
    return index.intValue();
  }
}
/*
*版权所有2010谷歌公司。
*
*根据Apache许可证2.0版(以下简称“许可证”)获得许可;你不可以
*除非符合许可证,否则请使用此文件。您可以获得一份
*许可证在
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*除非适用法律要求或书面同意,软件
*根据许可证进行的分发是按“原样”分发的,没有
*明示或暗示的任何种类的保证或条件。见
*管理下权限和限制的特定语言的许可证
*执照。
*/
包com.google.gwt.cell.client;
导入com.google.gwt.core.client.gwt;
导入com.google.gwt.dom.client.Element;
导入com.google.gwt.dom.client.NativeEvent;
导入com.google.gwt.dom.client.SelectElement;
导入com.google.gwt.safehtml.client.SafeHtmlTemplates;
导入com.google.gwt.safehtml.shared.safehtml;
导入com.google.gwt.safehtml.shared.SafeHtmlBuilder;
导入java.util.ArrayList;
导入java.util.HashMap;
导入java.util.List;
/**
*用于呈现下拉列表的{@link Cell}。
*/
公共类DynamicSelectionCell扩展了AbstractInputCell{
接口模板扩展了安全HtmlTemplates{
@模板(“{0}”)
取消选择安全HTML(字符串选项);
@模板(“{0}”)
选择安全HTML(字符串选项);
}
私有静态模板;
private HashMap indexForOption=new HashMap();
私人最终名单选择;
/**
*使用指定的选项构造一个新的{@link SelectionCell}。
*
*@param options单元格中的选项
*/
公共动态选择单元(列表选项){
超级(“变更”);
如果(模板==null){
template=GWT.create(template.class);
}
this.options=新的ArrayList(选项);
int指数=0;
用于(字符串选项:选项){
看跌期权(期权,索引++);
}
}
公共void addOption(字符串newOp){
字符串选项=新字符串(newOp);
选项。添加(选项);
刷新索引();
}
公共无效删除选项(字符串op){
字符串选项=新字符串(op);
options.remove(indexForOption.get(option));
刷新索引();
}
私有索引(){
int指数=0;
用于(字符串选项:选项){
看跌期权(期权,索引++);
}
}
@凌驾
public void onBrowserEvent(上下文上下文、元素父级、字符串值、,
NativeEvent事件,ValueUpdater(ValueUpdater){
onBrowserEvent(上下文、父级、值、事件、值更新程序);
字符串类型=event.getType();
如果(“变更”。等于(类型)){
Object key=context.getKey();
SelectElement select=parent.getFirstChild().cast();
字符串newValue=options.get(select.getSelectedIndex());
setViewData(键,新值);
完成编辑(父项、新值、键、值更新程序);
if(valueUpdater!=null){
valueUpdater.update(newValue);
}
}
}
@凌驾
公共void呈现(上下文上下文、字符串值、安全HtmlBuilder sb){
//获取视图数据。
Object key=context.getKey();
字符串viewData=getViewData(键);
if(viewData!=null&&viewData.equals(value)){
clearViewData(键);
viewData=null;
}
int-selectedIndex=getSelectedIndex(viewData==null?值:viewData);
某人以““””号结尾;
int指数=0;
用于(字符串选项:选项){
如果(索引+==selectedIndex){
sb追加(模板选择(选项));
}否则{
sb追加(模板取消选择(选项));
}
}
某人以““””号结尾;
}
私有int getSelectedIndex(字符串值){
整数索引=indexForOption.get(值);
如果(索引==null){
返回-1;
}
返回index.intValue();
}
}

不幸的是,SelectionCell将选项存储在私有列表中,并且在构造函数中设置选项后,没有任何方法可以处理这些选项

幸运的是,它是一个相当简单的类。只需制作您自己的(重命名)副本并添加
addOption(..)
/
removepoption(..)
方法来操作
列表选项

我喜欢我的工作