2011-05-22 8 views
14

Tengo un GWT CellTable<MyType>. Esta tabla tiene filas que deben mostrar un CheckBox. Este CheckBox debe marcarse o desmarcarse dependiendo de un getter en MyType, pero debe deshabilitarse para que el usuario no pueda hacer clic en él. ¿Alguien sabe cómo esto podría implementarse?Deshabilitar CheckboxCell en una Celda

Algunos fragmentos de código:

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");

Respuesta

13

CheckboxCell no soporta esta funcionalidad, pero usted puede hacer su propia celda que lo haga. El código no comprobado (copiado en gran parte del código CheckboxCell, Copyright Google (ver CheckboxCell fuente de la licencia)) sigue! La parte importante está en el código de renderizado. Su nueva celda iría en un 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); 
    } 
} 
0

He encontrado mi propia solución simple para esto. No es muy elegante, pero es muy fácil de implementar. Su casilla de verificación no se desactivará, pero simplemente no estará allí en absoluto. Esto es lo que hice:

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); 
    } 
    } 
}; 

simplemente he anulado el método render con el fin de hacer que la casilla de verificación sólo en una determinada condición definida por el valor de algún atributo.

Cuestiones relacionadas