Search code examples
gwtsortingcell

GWT Cell causes sorting issue


I created a "ColorCell" which simply shows a block of color. However, if I put this in a column, sorting is "off-by-one" meaning any column following the ColorCell sorts the column before it. Removing the ColorCell fixes the problem. What is happening? Here's the code for the cell and adding to the table.

colorColumn.setSortable(false);
table.addColumn(colorColumn);
table.setColumnWidth(colorColumn, "16px");

ColumnSortEvent.ListHandler<HasMeasures> columnSortHandler = new ColumnSortEvent.ListHandler<HasMeasures>(
        resultsDataProvider.getList());
table.addColumnSortHandler(columnSortHandler);
... code that adds other columns to sortHandler

public class ColorCell<C> implements Cell<C> {

  interface Template extends SafeHtmlTemplates {
    @Template("<div></div><div>{0}</div>")
    SafeHtml noColorCell(SafeHtml cellContents);

    /**
     * The wrapper around the image vertically aligned to the bottom.
     */
    @Template("<div style=\"width:14px;height:14px;float:left;background-color:{0};\"></div>")
    SafeHtml colorBlockCell(String color);

  }

  /**
   * The default spacing between the icon and the text in pixels.
   */
  private static final int DEFAULT_SPACING = 4;

  private static Template template;



  public ColorCell() {
    this(DEFAULT_SPACING);
  }

  public ColorCell(int spacing) {
    if (template == null) {
      template = GWT.create(Template.class);
    }
  }

  public boolean dependsOnSelection() {
    return false;
  }

  public Set<String> getConsumedEvents() {
    return null;
  }

  public boolean handlesSelection() {
    return false;
  }

  public boolean isEditing(Context context, Element parent, C value) {
    return false;
  }

  public void onBrowserEvent(Context context, Element parent, C value,
      NativeEvent event, ValueUpdater<C> valueUpdater) {
  }

  public void render(Context context, C value, SafeHtmlBuilder sb) {
    String color = getColorUsed(context, value);
    if (color != null) {
      sb.append(template.colorBlockCell(color));
    }

  }

  public boolean resetFocus(Context context, Element parent, C value) {
    return true;
  }

  public void setValue(Context context, Element parent, C value) {
  }

  /**
   * Check if the icon should be used for the value. If the icon should not be
   * used, a placeholder of the same size will be used instead. The default
   * implementations returns true.
   *
   * @param value the value being rendered
   * @return true to use the icon, false to use a placeholder
   */
  protected String getColorUsed(Context context, C value) {
    return null;
  }

  /**
   * Get the parent element of the decorated cell.
   *
   * @param parent the parent of this cell
   * @return the decorated cell's parent
   */
  private Element getCellParent(Element parent) {
    return parent; //.getChild(1).cast();
  }
}

Solution

  • Do you add the ColorCell with the same header as the header next/previous? This would merge the headers of ColorCell and add a collspan. My guess is it could cause a incorrect column index for the following columns.