在CellTable中禁用CheckboxCell

Lon*_*ick 15 gwt

我有一个GWT CellTable<MyType>.此表包含应显示的行CheckBox.这CheckBox应根据getter进行检查或取消选中MyType,但应禁用,以便用户无法单击它.有人知道如何实施吗?

一些代码片段:

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");
Run Code Online (Sandbox Code Playgroud)

Ril*_*ark 13

CheckboxCell不支持此功能,但您可以创建自己的单元格.未经测试的代码(主要从代码中复制CheckboxCell,版权所有Google(请参阅CheckboxCell许可证来源))如下!重要的部分是渲染代码.你的新细胞会进入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);
  }
}
Run Code Online (Sandbox Code Playgroud)