gwtcelltable

GWT CellTable Cells readOnly/disabled/non-editable


I want to make that some cells of the rows can be non-editable. by now my solution is when i create the columns, if one is readOnly, y make a TextCell, if not, i go with the default Cell wich can be EditTextCell,DatePickerCell,etc.

The problem with this is that i can't make some rows readOnly and others not. Or they are ALL the fields readOnly or they are not.

How can i do to make this for example

TABLE:
Data1 | Data2 | Data3
--------------------------------------
readOnly | non-readOnly | readOnly
readOnly | readOnly | non-readOnly

when i mean "readOnly" it can be "enabled" or make it a "TextCell"

                    celda = new TextInputCell();
                    Column<ObjetoDato, String> columna = new Column<ObjetoDato, String>(celda) {
                        @Override
                        public String getValue(ObjetoDato object) {
                                if(actual.getValorDefault()!=null && object.getValor(actual.getNombreCampo()).isEmpty()){
                                    object.setValor(actual.getNombreCampo(), actual.getValorDefault());
                                    return actual.getValorDefault();
                                }

                                return object.getValor(actual.getNombreCampo());
                        }
                    };
                    tabla.agregarColumna(columna, actual.getCaption()); 
                    columna.setFieldUpdater(new FieldUpdater<ObjetoDato, String>() {
                              @Override
                            public void update(int index, ObjetoDato object, String value) {
                                object.setValor(actual.getNombreCampo(), value);
                                new Scripter(object,actual.getComportamiento(),true);
                                tabla.actualizar();
                                Sistema.get().getIG().actualizarTotales();
                              }
                    });  

I tried creating my cutom cell already and replacing the TextImputCell, but the methods never trigger

celda = new FabriCel();

and

public class FabriCel extends TextInputCell {
private String campo;

public FabriCel(String campo){
    this.campo=campo;
}
 @Override
 public void onBrowserEvent(Context context, Element parent, String value, NativeEvent event, ValueUpdater<String> valueUpdater){
     Boolean editable = false;///get it from your model

     if(editable != null && !editable){
         event.preventDefault();
     }else{
         super.onBrowserEvent(context, parent, value, event, valueUpdater);
     }
 }

Also this

@Override
public void render(com.google.gwt.cell.client.Cell.Context context, String value, SafeHtmlBuilder sb) {
    Boolean editable = false;///get it from your model
    if(editable){
        Log.log();
        sb.appendHtmlConstant("<div contentEditable='false'>"  +value+"</div>");        
    }else{
        Log.log("No entra");
        super.render(context, value, sb);
    }

}  

Thanks!


Solution

  • You have to create one custom cell. In that, you have tell runtime like it should be readonly or no-readonly. just example.

            private class CustomCell extends EditTextCell {
                public void render(com.google.gwt.cell.client.Cell.Context context, 
                String value, SafeHtmlBuilder sb) {
                Data data=context.getKey();
                if(data.isReadOnly()){
    
                sb.appendHtmlConstant("<div contentEditable='false'
                 unselectable='false' >"  +value+"</div>");         
                            }else{
                                super.render(context, value, sb);
    
    
                            }
    
                    }   
                }
    

    In given bean, there is some condition which says readonly or no-readonly. And create column like

    Column<Data, String> nameColumn = new Column<Data, String>(new CustomCell()) {
    
            @Override
            public String getValue(Data object) {
                return object.getName();
    
            }
        };