Question

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!

Was it helpful?

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

        }
    };

OTHER TIPS

A way to do this is to override the onBrowserEvent event of your Editable Cells and consume the event if the cell is not editable.

            final EditTextCell cell = new EditTextCell(renderer)
            {
                @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);
                    }
                }
}

I had the same need; and tested out various combinations of overriding render, isEditing, resetFocus, and edit on EditTextCell (I didn't try the onBrowserEvent solution).

When I only overrode render (to show an HTML value if non-editable); I got errors resetting focus (as discussed here). This continued even if I overrode resetFocus. When I only override isEditing, the cell would flash to editing when clicked, and then flash back. What worked perfectly was overriding edit. I triggered based on adding a tag to the value passed in by Column.getValue, you can trigger however you like, but it turned out to be as simple as:

private static class LockableEditTextCell extends EditTextCell {
    @Override
    protected void edit(Context context, Element parent, java.lang.String value) {
      if (!value.startsWith(LOCKED_CELL_VALUE)) {
        super.edit(context, parent, value);
      }
    }
  }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top