[英]How to dynamically update the choices in a SelectionCell using 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");
編輯:感謝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<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();
}
}
不幸的是,SelectionCell將選項存儲在私有List中,並且在構造函數中設置它們之后沒有方法可以操作它。
幸運的是, SelectionCell是一個相當簡單的類。 只需創建自己的(重命名)副本並添加addOption(..)
/ removeOption(..)
方法來操作List<String> options
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.