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);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With