简体   繁体   中英

How to enable/disable editing a specific column in a Vaadin Grid?

I have a Vaadin Grid (myGrid) where I have enabled cell editing using

myGrid.setEditorEnabled(true);

However, I would like to disallow editing the cells in one particular column. How can I do this? The only workaround I can think of is to intercept the call to the model bean and throw some exception / show a message to the user. Is there a better way to do this?

Using:

myGrid.setEditorEnabled(true);

does not automatically make every cell editable. You need to specify a Binder for every editable column. As stated in Vaadin docs :

The editor is based on Binder which is used to bind the data to the editor. (...) For each column that should be editable, a binding should be created in the editor binder and then the column is configured to use that binding.

So if you want to disallow editing in a specific column, then just don't provide a binder for it.

Take a look at my solution

indexedContainer = new IndexedContainer();
indexedContainer.addContainerProperty("name",String.class,"");
//....
//in add item process do the following 
Item item = indexedContainer.getItem(indexedContainer.addItem());
item.getItemProperty("name").setValue(myModel.getName());
item.getItemProperty("name").setReadOnly(true);
//...
myGrid.setContainerDataSource(container);

that's it

I think there is another way but I did not test it

myGrid.getColumn("name").setEditable(false)

hope this helps you

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM