How to make only some columns editable in the Vaadin grid?

Vaadin Grid allows you to define as editable using

grid.setEditorEnabled(true); 

This makes visible columns visible. However, I do not want the user to edit a specific column, but it seems that the editable is all or nothing.

The next best solution I found is to define an editor field with the editor disabled, which almost does the trick, but the user can still select the text and move the cursor (but the field is no longer being edited).

 Grid.Column nameColumn = grid.getColumn("fullName"); nameColumn.setHeaderCaption("Full Name"); nameColumn.setEditorField(getNoEditableTextField()); ... private Field<?> getNoEditableTextField() { TextField noEditableTextFiled = new TextField(); noEditableTextFiled.setEnabled(false); return noEditableTextFiled; } 

I believe the label cannot be used because it is not a field.

Is there a better option for this?

edit: as aakath said, there is a way to achieve this by not allowing the column to be edited, but the cell value disappears when editing the row, which is undesirable.

+6
source share
7 answers

my solution is below. I just finished. he did not experience too much. but it may give you some ideas.

ati

  getColumn(columnName).setEditable(true).setEditorField(getNoEditableField(columnName)); 

...

 private Field<?> getNoEditableField(final String columnName) { CustomField<Label> result = new CustomField() { @Override protected Component getContent() { Label result = (Label) super.getContent(); Object editedItemId = getEditedItemId(); String value = DEFAULT_VALUE; if (editedItemId != null) { value = CustomizableGrid.this.toString(getContainerDataSource().getItem(editedItemId).getItemProperty(columnName).getValue()); } result.setValue(value); return result; } @Override protected Component initContent() { Label result = new Label(DEFAULT_VALUE, ContentMode.HTML); result.setDescription(getColumnDescription(columnName)); result.setStyleName("immutablegridcellstyle"); return result; } @Override public Class getType() { return Label.class; } }; result.setConverter(new Converter<Label, Object>() { //converter for your data }); return result; } 
+2
source

Have you tried calling the setEditable(false) method on a column? I believe that it should make the field non-editable when the item editor is active.

 grid.getColumn("fullName").setEditable(false); 
+5
source

I had the same problem and didn't want the click on the id column to open the editor. I solved this with the addition of an ItemClickListener, as shown below. This works great for me.

 grid.addItemClickListener((ItemClickListener<GridBean>) event -> grid.getEditor().setEnabled(!event.getColumn().getCaption().equals("Id"))); 

You can also click on individual columns. The grid is no longer editable.

+2
source

There is one difficult way to do this! I just found out about it. So, first of all you need to use a grid with a container, and not add straight lines:

 BeanItemContainer<MyBean> container = new BeanItemContainer<>(MyBean.class); setContainerDataSource(container); 

Then remove the field settings from MyBean, except for the settings for the fields you must change.

+1
source

I think the same thing can be achieved by making the grid editable on grid.setEditorEnabled(true); and disabling the editing option for other columns, such as grid.getColumn(columnName).setEditable(false); . But I'm not sure of any flaws in this method. Any suggestion is always appreciated.

+1
source

I use the following approach to get a read-only field, the trick overrides the setEnabled method to get a disabled text field. If you trace the source code in the Vaadin Grid, no matter what field you switch to, it will always call field.setEnabled (true).

 myGrid.getColumn(propertyId).setEditorField(new ReadOnlyField()); 

and

 public class ReadOnlyField extends TextField { public ReadOnlyField() { super(); this.setReadOnly(true); } @Override public void setEnabled(boolean enabled) { // always set to disabled state super.setEnabled(false); } } 
0
source

Its just go to the Vaadin documentation, which of this was below: you can see here, I indicated the specified column name

 grid = new Grid<>(); lst = new ArrayList<>(); provider = new ListDataProvider<>(lst); lst.add(new Company(1, "Java")); grid.setDataProvider(provider); grid.addColumn(Company::getSerialNo).setCaption("Sr.no"); TextField tf = new TextField(); grid.getEditor().setEnabled(true); HorizontalLayout hlyt = new HorizontalLayout(); grid.addColumn(Company::getName).setEditorComponent(tf, Company::setName).setCaption("Name").setExpandRatio(2); hlyt.addComponent(grid); 
0
source

Source: https://habr.com/ru/post/989268/


All Articles