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

我试图有一个表格,显示用户输入的数据以及编辑数据。 我已经弄清楚如何用文本来做这个(也就是说,他们可以编辑表格中某些东西的名字),但是我不能让它与选择单元格一起工作。

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

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

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

SelectionCell editTypeComboBox = new SelectionCell(record.getTypeList()); Column typeColumn = new Column(editTypeComboBox) { @Override public String getValue(Assignment object) { return object.getType(); } }; typeColumn.setFieldUpdater(new FieldUpdater() { @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"); 

编辑:感谢Peter Knego敌人帮助我解决这个问题。 这是修改后的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 { interface Template extends SafeHtmlTemplates { @Template("{0}") SafeHtml deselected(String option); @Template("{0}") SafeHtml selected(String option); } private static Template template; private HashMap indexForOption = new HashMap(); private final List options; /** * Construct a new {@link SelectionCell} with the specified options. * * @param options the options in the cell */ public DynamicSelectionCell(List options) { super("change"); if (template == null) { template = GWT.create(Template.class); } this.options = new ArrayList(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 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(""); int index = 0; for (String option : options) { if (index++ == selectedIndex) { sb.append(template.selected(option)); } else { sb.append(template.deselected(option)); } } sb.appendHtmlConstant(""); } private int getSelectedIndex(String value) { Integer index = indexForOption.get(value); if (index == null) { return -1; } return index.intValue(); } } 

不幸的是,SelectionCell将选项存储在一个私有List中,并且在构造函数中设置它们之后没有方法可以操作它。

幸运的是, SelectionCell是一个相当简单的类。 只需创建自己的(重命名)副本并添加addOption(..) / removeOption(..)方法来操作List options