开发者

How can I disable EditTextCell and enable it with EditButton click in GWT?

I have a CellTable with EditTextCell, the EditButton is outside of the CellTable. The CellTable will be readonly until I click on the EditButton, the EditTextCell will enable for me to click and edit. Doe开发者_如何学运维s anybody know how to do this? Thank you.


My custom class:

public class ButtonEnableEditTextCell extends EditTextCell {

    private boolean click;

    @Override
    public void onBrowserEvent(Context context, Element parent, String value, NativeEvent event,
            ValueUpdater<String> valueUpdater) {
        if (click) {
            super.onBrowserEvent(context, parent, value, event, valueUpdater);
        }
    }

    public void setClick(Boolean click) {
        this.click = click;
    }
}

View with table:

CellTable<Employee> employeeTable;

@UiField
Button editButton;

ButtonEnableEditTextCell sequenceCell = new ButtonEnableEditTextCell();

@UiHandler("editButton")
public void editButtonClick(ClickEvent event) {
    sequenceCell.setClick(true);
}

private void initTableColumns() {
        Column<Employee, String> sequenceColumn = new Column<Employee, String>(sequenceCell) {
            public String getValue(Employee employee) {
                return employee.getSeqNumber().toString();
            }
        };
        sequenceColumn.setFieldUpdater(new FieldUpdater<Employee, String>() {
            public void update(int index, Employee employee, String value) {
                employee.setSeqNumber(new Integer(value));
            }
        });
        employeeTable.addColumn(sequenceColumn, "Sequence Number");
}

I am able to enable the sequenceColumn with this code. I wonder if there is better way to do it?


You will have to make your own cell. You can extend EditTextCell and override the onBrowserEvent function. If your button has been clicked and editing is enabled, then call super.onBrowserEvent. If editing is disabled, either do nothing or display an error message.


I had a similar need; except wanting to be able to change editability of each cell while running based on the row values. I 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 (ie some setting based on your edit mode), but disabling for a cell 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);
      }
    }
  }

Update: this had a bug where if you clicked an editable cell; then a locked cell; it would let you edit. You have to also override onBrowserEvent:

@Override
public void onBrowserEvent(Context context, Element parent, String value,
    NativeEvent event, ValueUpdater<String> valueUpdater) {
  if (!value.startsWith(IncentivesColumnConfigurator.LOCKED_CELL_VALUE)) {
    super.onBrowserEvent(context, parent, value, event, valueUpdater);
  }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜