4

我正在关注 Goolge 关于如何将 ListBoxes/SelectionCells 添加到 CellTable 的示例,但我不知道如何更改行为,因此匹配不显示字符串值。

我显示@SelectionCell 的项目不是唯一的(即可以有2 个同名的元素),所以我需要使用与对象关联的其他字段来知道选择了哪个

for (IrrigationProgramDTO program: programOptions)    
 categoryNames.add(program.getName());

SelectionCell categoryCell = new SelectionCell(categoryNames);
Column<IrrigationGapDTO, String> categoryColumn = new Column<IrrigationGapDTO, String>    (categoryCell) {
      @Override
      public String getValue(IrrigationGapDTO object) {
          if (object.getProgramSelected()!=null)
              return object.getProgramSelected().getName();
          else
              return "";
      }
    };      
    categoryColumn.setFieldUpdater(new FieldUpdater<IrrigationGapDTO, String>() {
      public void update(int index, IrrigationGapDTO object, String value) {
          for (IrrigationProgramDTO program: programOptions) {
                  //not valid as there could be more than 1 program with the same name
              if (program.getName().equals(value)) { 
                  object.setProgramSelected(program);
                  break;
              }

          }  
      }
4

3 回答 3

2

这是我对解决方案 #3 的新实现(请注意,您必须将 FieldUpdater 添加到列中才能正常工作):

import java.util.Arrays;
import java.util.List;

import com.google.gwt.cell.client.AbstractEditableCell;
import com.google.gwt.cell.client.Cell;
import com.google.gwt.cell.client.ValueUpdater;
import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.BrowserEvents;
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.client.SafeHtmlTemplates.Template;
import com.google.gwt.safehtml.shared.SafeHtml;
import com.google.gwt.safehtml.shared.SafeHtmlBuilder;

/**
 * A {@link Cell} used to render a drop-down list.
 *
 * @author Gaspard van Koningsveld
 */
public class ItemSelectionCell<C> extends AbstractEditableCell<C, C> {

    interface Template extends SafeHtmlTemplates {
        @Template("<select tabindex=\"-1\" style=\"width:100%\">")
        SafeHtml beginSelect();

        @Template("<option value=\"{0}\">{1}</option>")
        SafeHtml deselected(int hash, String option);

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

        @Template("</select>")
        SafeHtml endSelect();
    }

    private static Template template;

    private List<C> items;

    public ItemSelectionCell(C itemsArray[]) {
        this(Arrays.asList(itemsArray));
    }

    public ItemSelectionCell(List<C> items) {
        super(BrowserEvents.CHANGE);
        if (template == null) {
            template = GWT.create(Template.class);
        }
        this.items = items;
    }

    @Override
    public void onBrowserEvent(Context context, Element parent, C value, NativeEvent event, ValueUpdater<C> valueUpdater) {
        super.onBrowserEvent(context, parent, value, event, valueUpdater);
        if (BrowserEvents.CHANGE.equals(event.getType())) {
            SelectElement select = parent.getFirstChild().cast();
            int newIndex = select.getSelectedIndex();
            valueUpdater.update(items.get(newIndex));
        }
    }

    @Override
    public void render(Context context, C value, SafeHtmlBuilder sb) {
        sb.append(template.beginSelect());
        for (int i = 0; i < items.size(); i++) {
            C item = items.get(i);
            if (item.equals(value)) {
                sb.append(template.selected(i, getItemDisplayString(item)));
            } else {
                sb.append(template.deselected(i, getItemDisplayString(item)));
            }
        }
        sb.append(template.endSelect());
    }

    public String getItemDisplayString(C item) {
        return item.toString();
    }

    public List<C> getItems() {
        return items;
    }

    public void setItems(List<C> items) {
        this.items = items;
    }

    @Override
    public boolean isEditing(Context context, Element parent, C value) {
        return false;
    }
}
于 2013-01-31T12:31:26.690 回答
1

3种可能的解决方案:

1.肮脏的解决方法

而不是getName()return getName()+ 一些唯一标识符:

public String getValue(IrrigationGapDTO object) {
    if (object.getProgramSelected()!=null)
        return object.getProgramSelected().getName()+"_"+object.getUniqueIdentiufier();
    else
        return "";
}

然后在FieldUpdater您可以拆分“_”字符并处理重复项

2. 使用唯一 ID 代替getName():只需为您的程序生成/分配唯一 ID 并使用它代替名称。

3.使用IrrigationProgramDTO类型而不是String

而不是String您可以IrrigationProgramDTO在列定义中使用类。但是,您可能必须使用用户定义的 SelectionCell,它采用IrrigationProgramDTO类型而不是String数据类型。

Column<IrrigationGapDTO, IrrigationProgramDTO> categoryColumn = new Column<IrrigationGapDTO, IrrigationProgramDTO>    (categoryCell) {
  @Override
  public IrrigationProgramDTO (IrrigationGapDTO object) {
      if (object.getProgramSelected()!=null)
          return object.getProgramSelected();
      else
          return null;
  }

};      
categoryColumn.setFieldUpdater(new FieldUpdater<IrrigationGapDTO, IrrigationProgramDTO>() {
  public void update(int index, IrrigationGapDTO object, IrrigationProgramDTO value) {
       object.setProgramSelected(program);
      }  
  }
于 2012-03-12T19:40:44.487 回答
0

这是我对@Ümit 的解决方案#3 的实现:

public static abstract class EditSelectColumn<E, S> {

    Map<String, S> selectionMap = new HashMap<String, S>();

    EditColumn<E> column;

    protected final String relationshipFieldName;

    public EditSelectColumn(String relationshipFieldName) {
        this.relationshipFieldName = relationshipFieldName;

        for (S option : getOptions()) {
            assert getOptionString(option) != null : "Option string cannot be null, please check your database";
            selectionMap.put(getOptionString(option), option);
        }
        SelectionCell cell = new SelectionCell(new ArrayList<String>(selectionMap.keySet()));
        column = new EditColumn<E>(cell) {
            @Override
            public String getValue(E object) {
                if (getOption(object) == null)
                    return "";
                return getOptionString(getOption(object));
            }

            @Override
            public void setValue(E object, String value) {
                setOption(object, selectionMap.get(value));
            }
        };
    }

    public EditColumn<E> getColumn() {
        return column;
    }

    public abstract List<S> getOptions();

    public abstract String getOptionString(S option);

    public abstract S getOption(E object);

    public abstract void setOption(E object, S value);
}
于 2013-01-30T15:36:29.933 回答