PDA

View Full Version : RowEditor with a conditionally enabled cell



AdamSas
16 Jan 2012, 12:30 PM
Hi All,

I need a Grid with several columns and a RowEditor, where some columns are conditionally enabled for editing and sometimes not. It depends on the other values in that row. E.g. we have a grid with columns age and personalId. When a value in age > 18, personalId should be enabled for editing (TextField). Otherwise it should be disabled for editing.
How can I achieve such behaviour?

Regards

sven
16 Jan 2012, 12:33 PM
You could listen to the BeforeStartEditEvent and remove/add the editor for the specific columns depending on your input.

sven
16 Jan 2012, 12:47 PM
You could listen to the BeforeStartEditEvent and remove/add the editor for the specific columns depending on your input.

Sorry this was for Ext GWT 3.

For Ext GWT 2 you need to listen to the BeforeEdit and remove/add the editor from the ColumnConfig

AdamSas
16 Jan 2012, 2:11 PM
Thank you for your solution. BeforeEdit event works very well - it is invoked every time I start editing a row with RowEditor. Unfortunately changing editor for ColumnConfig is not working in my code. It seems that after the first assignment of CellEditor the editor is not changed in subsequent calls of RowEditor.

I changed my requirement so that age column should be editable only when it was empty before the start of editing, otherwise the previously set value should only be displayed. This should be achieved with the following code:


final CellEditor codeTextFieldCellEditor = new CellEditor(new TextField<String>());
final CellEditor codeLabelFieldCellEditor = new CellEditor(new LabelField());

rowEditor.addListener(Events.BeforeEdit, new Listener<RowEditorEvent>() {
@Override
public void handleEvent(RowEditorEvent be) {
ModelData modelData = grid.getStore().getAt(be.getRowIndex());
if(modelData == null || modelData.get("age") == null) {
grid.getColumnModel().setEditor(0, codeTextFieldCellEditor);
} else {
grid.getColumnModel().setEditor(0, codeLabelFieldCellEditor);
}
}
});

Unfortunately the "age" column is always editable even if I start editing for the second time (in that case column age is not empty because I put some value in the first editing).

Is it OK how I try to replace CellEditor in ColumnConfig?

Thanks

AdamSas
18 Jan 2012, 2:58 AM
For some reasons the solution with changing Editor on BeforeEdit event does not work, but the aforementioned requirement can be achieved in a different way - instead replacing Editor, a TextField attached to CellEditor can be disabled/enabled depending on custom condition.



final TextField editorField = new TextField<String>();
final CellEditor codeTextFieldCellEditor = new CellEditor(editorField);

rowEditor.addListener(Events.BeforeEdit, new Listener<RowEditorEvent>() {
@Override
public void handleEvent(RowEditorEvent be) {
ModelData modelData = grid.getStore().getAt(be.getRowIndex());
if(modelData == null || modelData.get("age") == null) {
editorField.enable();
} else {
editorField.disable();
}
}
});