Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to dynamically update the choices in a SelectionCell using GWT?
    primarykey
    data
    text
    <p>I am trying to have a table that displays data that the user inputs as well as edit the data. I have figured out how to do this with text (ie, they can edit the name of something in the table), but I cannot get it to work with selection cells. </p> <p>It works correctly if the items in the selection cell are predefined, but I cannot dynamically update the items in the cell to include new things after I have created the cell. </p> <p>To explain more, i have a "type" column. The user enters items into the table with a given type, but that can also add new types later. When they click on the item in the type column, I want the dropdown box to contain all the new types that they have entered, but I don't know how to accomplish this. </p> <p>Here is the code I have so far (that doesn't update like I want it to). record.getTypeList() will contain additional entries after the user enters new types. </p> <pre><code>SelectionCell editTypeComboBox = new SelectionCell(record.getTypeList()); Column&lt;Assignment, String&gt; typeColumn = new Column&lt;Assignment, String&gt;(editTypeComboBox) { @Override public String getValue(Assignment object) { return object.getType(); } }; typeColumn.setFieldUpdater(new FieldUpdater&lt;Assignment, String&gt;() { @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"); </code></pre> <p><b>Edit:</b> Thanks to Peter Knego foe helping me figure this out. Here is the modified DynamicSelectionCell class if anyone if interested:</p> <pre><code>/* * 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&lt;String, String&gt; { interface Template extends SafeHtmlTemplates { @Template("&lt;option value=\"{0}\"&gt;{0}&lt;/option&gt;") SafeHtml deselected(String option); @Template("&lt;option value=\"{0}\" selected=\"selected\"&gt;{0}&lt;/option&gt;") SafeHtml selected(String option); } private static Template template; private HashMap&lt;String, Integer&gt; indexForOption = new HashMap&lt;String, Integer&gt;(); private final List&lt;String&gt; options; /** * Construct a new {@link SelectionCell} with the specified options. * * @param options the options in the cell */ public DynamicSelectionCell(List&lt;String&gt; options) { super("change"); if (template == null) { template = GWT.create(Template.class); } this.options = new ArrayList&lt;String&gt;(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&lt;String&gt; 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 &amp;&amp; viewData.equals(value)) { clearViewData(key); viewData = null; } int selectedIndex = getSelectedIndex(viewData == null ? value : viewData); sb.appendHtmlConstant("&lt;select tabindex=\"-1\"&gt;"); int index = 0; for (String option : options) { if (index++ == selectedIndex) { sb.append(template.selected(option)); } else { sb.append(template.deselected(option)); } } sb.appendHtmlConstant("&lt;/select&gt;"); } private int getSelectedIndex(String value) { Integer index = indexForOption.get(value); if (index == null) { return -1; } return index.intValue(); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload