1

This is my implementation of EditTextCell:

public class MyEditTextCell extends EditTextCell {

    @Override
    protected void edit(Context context, Element parent, String value) {
        if (value.equals("")) {
            super.edit(context, parent, value);
        } else {
            clearViewData(context.getKey());
        }
    }
}

I would like to text input to be shown for only for empty cells. IT is why I've override the edit() method. The rest behaviour of oryginal EditTextCell is ok, so I've not changed it.

This unfortunatelly doesn't work. Please help.

masterdany88
  • 5,041
  • 11
  • 58
  • 132

1 Answers1

1

The editor in EditTextCell is shown in onBrowserEvent method, so you just need:

public class MyEditTextCell extends EditTextCell {
    @Override
    public void onBrowserEvent(Context context, Element parent, String value, NativeEvent event, ValueUpdater<String> valueUpdater) {
        if(value == null || value.isEmpty())
            super.onBrowserEvent(context, parent, value, event, valueUpdater);
    }
}

Remember, to add the FieldUpdater to the column to save the edited value.

Here you have a full working example with simple table type containing only one String:

public class MyTableType {

    private String value;

    public MyTableType(String value) {
        super();
        this.value = value;
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }
}

CellTable<MyTableType> table = new CellTable<MyTableType>();

MyEditTextCell cell = new MyEditTextCell();

Column<MyTableType, String> column = new Column<MyTableType, String>(cell) {
    @Override
    public String getValue(MyTableType object) {
        if(object.getValue() == null)
            return "";
        else
            return object.getValue();
    }
};
column.setFieldUpdater(new FieldUpdater<MyTableType, String>() {
    @Override
    public void update(int index, MyTableType object, String value) {
        object.setValue(value);
    }
});

table.addColumn(column, "Value");

ArrayList<MyTableType> values = new ArrayList<MyTableType>();
values.add(new MyTableType("one"));
values.add(new MyTableType("two"));
values.add(new MyTableType("three"));
values.add(new MyTableType(null));
values.add(new MyTableType(""));
table.setRowData(values);

Please, notice that once you edit the cell value to be non-empty, the editor will not be shown after that.

Adam
  • 5,403
  • 6
  • 31
  • 38
  • So I should not override `edit()` method? And instead I should override `onBrowserEvent()`? – masterdany88 Oct 31 '16 at 08:19
  • Overriding `edit()` method should also work (actually now I think it's even better solution). So your approach should be OK. Maybe you should check if the value is not null. Also see the `getValue()` method - it should not return null. – Adam Oct 31 '16 at 08:28
  • Maybe it is better solution, but it doesn't work on production!!! Maybe it is the case of some kind a bug in 2.6.1 version. – masterdany88 Oct 31 '16 at 08:31
  • Have you tried with `onBrowserEvent` on production? – Adam Oct 31 '16 at 08:37
  • I am testing it. I have problem with that when the value is not acceptable I would like to restore empty value, For example: I have column for a number. When user enter text I would like to clear this text in `FieldUpdater - update` method. How can I achieve this? – masterdany88 Oct 31 '16 at 08:44