开发者

Disable CheckboxCell in a CellTable

I have a GWT CellTable<MyType>. This table have rows which should display a CheckBox. This CheckBox should checked or unchecked depending on a getter in MyType, but it should disabled so the user can't click it. Does anybody know how this could be implemented?

开发者_开发技巧

Some code snippets:

Column<MyType, Boolean> checkboxColumn = new Column<MyType, Boolean>(new CheckboxCell()) {
    @Override
    public Boolean getValue(MyType object) {
        return object.isItTrue();
    }
};
CellTable<MyType> cellTable = new CellTable<MyType>();
cellTable.addColumn(checkboxColumn, "Header title");


CheckboxCell does not support this functionality, but you can make your own cell that does. Untested code (copied largely from the CheckboxCell code, Copyright Google (see CheckboxCell source for license)) follows! The important part is in the render code. Your new cell would go in a Column<MyType, MyType>.

public class DisableableCheckboxCell extends AbstractEditableCell<MyType> {

  /**
   * An html string representation of a checked input box.
   */
  private static final SafeHtml INPUT_CHECKED = SafeHtmlUtils.fromSafeConstant("<input type=\"checkbox\" tabindex=\"-1\" checked/>");

  /**
   * An html string representation of an unchecked input box.
   */
  private static final SafeHtml INPUT_UNCHECKED = SafeHtmlUtils.fromSafeConstant("<input type=\"checkbox\" tabindex=\"-1\"/>");

  private static final SafeHtml INPUT_CHECKED_DISABLED = SafeHtmlUtils.fromSafeConstant("<input type=\"checkbox\" tabindex=\"-1\" checked disabled=\"disabled\"/>");

  private static final SafeHtml INPUT_UNCHECKED_DISABLED = SafeHtmlUtils.fromSafeConstant("<input type=\"checkbox\" tabindex=\"-1\" disabled=\"disabled\"/>");

/** You'd copy the rest of the code from CheckboxCell, or implement the other required functions yourself **/

  @Override
  public void render(Context context, MyType value, SafeHtmlBuilder sb) {
    // Get the view data.
    Object key = context.getKey();
    MyType viewData = getViewData(key);
    if (viewData != null && viewData.equals(value)) {
      clearViewData(key);
      viewData = null;
    }

    MyType relevantValue = viewData != null ? viewData : value;
    boolean checked = relevantValue.shouldBeChecked();
    boolean enabled = relevantValue.shouldBeEnabled();

    if (checked && !enabled)) {
      sb.append(INPUT_CHECKED_DISABLED);
    } else if (!checked && !enabled) {
      sb.append(INPUT_UNCHECKED_DISABLED);
    } else if (checked && enabled) {
      sb.append(INPUT_CHECKED);
    } else if (!checked && enabled) {
      sb.append(INPUT_UNCHECKED);
  }
}


I came up with my own simple solution for this. It's not too elegant but it's very easy to implement. Your checkbox won't be deactivated but it simply won't be there at all. Here is what I did :

private Column<SomeObject,Boolean> someColumn = new Column<SomeObject,Boolean>(new CheckboxCell()) {
  @Override
  public Boolean getValue(SomeObject object){
     return object.getSomeValue();
  }
  @Override
  public void render(Context context, SomeObject object, SafeHtmlBuilder sb){
     if (object.getSomeOtherValue()){
        super.render(context, object, sb);
     }
  }
};

I've simply overriden the render method in order to render the CheckBox only on a certain condition defined by the value of some attribute.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜