-
-
Save zkendall/c803835064e4b682b3a2 to your computer and use it in GitHub Desktop.
This example class shows a way of editing only the selected rows in a Vaadin table. This improves rendering performance on large tables.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Collection; | |
import com.vaadin.data.Container; | |
import com.vaadin.data.util.BeanItemContainer; | |
import com.vaadin.ui.Component; | |
import com.vaadin.ui.DefaultFieldFactory; | |
import com.vaadin.ui.Field; | |
import com.vaadin.ui.Table; | |
@SuppressWarnings("serial") | |
public class EditableTable extends Table { | |
private Table table = this; | |
public EditableTable() { | |
table.setTableFieldFactory(new DefaultFieldFactory() { | |
@Override | |
public Field<?> createField(Container container, Object itemId, Object propertyId, Component uiContext) { | |
// If the itemId isn't the currently selected item in the table, don't generate a field | |
// i.e. it's not editable | |
if (!table.isMultiSelect() && !itemId.equals(table.getValue())) { | |
return null; | |
} else if (table.isMultiSelect()) { | |
if (!((Collection<?>) table.getValue()).contains(itemId)) { | |
return null; | |
} | |
} | |
// Create editable field | |
Field field = super.createField(container, itemId, propertyId, uiContext); | |
return field; | |
} | |
}); | |
// If row selected force refresh (to re-exectue field factory) | |
table.addValueChangeListener(new ValueChangeListener() { | |
@Override | |
public void valueChange(com.vaadin.data.Property.ValueChangeEvent event) { | |
forceRefreshRowCache(); | |
} | |
}); | |
table.setContainerDataSource(createContainer()); | |
table.setSelectable(true); | |
table.setMultiSelect(true); | |
// This enables the use of FieldFactories | |
table.setEditable(true); | |
table.setSizeFull(); | |
} | |
/** | |
* Required for appropriate refresh behavior. see: http://dev.vaadin.com/ticket/14684 | |
* Unfortunately this patch requires subclassing :( | |
*/ | |
public void forceRefreshRowCache() { | |
boolean wasEnabled = disableContentRefreshing(); | |
enableContentRefreshing(true); | |
refreshRowCache(); | |
enableContentRefreshing(wasEnabled); | |
} | |
/** | |
* Add fake data. | |
*/ | |
private Container createContainer() { | |
BeanItemContainer<BlackBeans> container = new BeanItemContainer<>(BlackBeans.class); | |
for (int i = 0; i < 2; i++) { | |
container.addBean(new BlackBeans("Bob " + i, 10.1 + i)); | |
container.addBean(new BlackBeans("Harry " + i, 14.8 + i)); | |
container.addBean(new BlackBeans("Margaret " + i, 0.0 + i)); | |
container.addBean(new BlackBeans("Glenda " + i, 22.1 + i)); | |
container.addBean(new BlackBeans("Boris " + i, 0.1 + i)); | |
container.addBean(new BlackBeans("Jessica " + i, 24.1 + i)); | |
} | |
return container; | |
} | |
/** | |
* Class for bean item container. | |
*/ | |
public class BlackBeans { | |
private String name; | |
private double value; | |
public BlackBeans(String string, double d) { | |
setName(string); | |
setValue(d); | |
} | |
public String getName() { | |
return name; | |
} | |
public void setName(String name) { | |
this.name = name; | |
} | |
public double getValue() { | |
return value; | |
} | |
public void setValue(double value) { | |
this.value = value; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment