稍后再回答,但无论如何。几天前,一个人也面临着这个任务——文本或组合框必须在一列中用于单元格编辑。这是我的实现:
final GridInlineEditingTextOrCombo editing = new GridInlineEditingTextOrCombo(attributeTableGrid);
editing.addEditor(valueCol);
自定义 GridInlineEditing 实现如下:
/**
* Class intended to create GridInlineEditing functionality,
* but with two type of editors in one column - TextField or SimpleComboBox,
* depending of SnmpParameterDefDTO.getAllowedValues().
*/
class GridInlineEditingTextOrCombo extends GridInlineEditing<SnmpParameterDefDTO> {
IsField<String> textField = new TextField();
SimpleComboBox<String> simpleComboBox = new SimpleComboBox<String>(new StringLabelProvider<String>());
Grid.GridCell currentCell = null;
private boolean currentCellChanged = false;
IsField<String> currentCellEditor;
//ComboBox<String> comboBox = new ComboBox<String>();
public GridInlineEditingTextOrCombo(Grid<SnmpParameterDefDTO> editableGrid) {
super(editableGrid);
simpleComboBox.setEditable(false);
simpleComboBox.setAllowTextSelection(false);
simpleComboBox.setTriggerAction(ComboBoxCell.TriggerAction.ALL);
}
@Override
@SuppressWarnings("unchecked")
public <O> IsField<O> getEditor(ColumnConfig<SnmpParameterDefDTO, ?> columnConfig) {
IsField<O> field = super.getEditor(columnConfig);
if(field!=null ){
if(!currentCellChanged){
return (IsField<O>)currentCellEditor;
}else{
currentCellChanged = false;
SnmpParameterDefDTO param = this.editableGrid.getStore().get(this.currentCell.getRow());
if(param.getAllowedValues() == null || param.getAllowedValues().size() == 0){
currentCellEditor = (IsField<String>)field;
}else{
simpleComboBox.getStore().clear();
simpleComboBox.add(param.getAllowedValues());
currentCellEditor = simpleComboBox;
}
return (IsField<O>)currentCellEditor;
}
}
return null;
}
@Override
public <T> void addEditor(ColumnConfig<SnmpParameterDefDTO, T> columnConfig, IsField<T> field) {
throw new RuntimeException("You can not call this method. Please use addEditor(ColumnConfig<SnmpParameterDefDTO, T> columnConfig) instead");
}
public <T> void addEditor(ColumnConfig<SnmpParameterDefDTO, T> columnConfig) {
super.addEditor(columnConfig, (IsField<T>)textField);
}
@Override
public void startEditing(Grid.GridCell cell){
currentCell = cell;
currentCellChanged = true;
super.startEditing(cell);
}
这是一种解决方法,不是优雅的实现,但无论如何,它都可以正常工作