Skip to content

Instantly share code, notes, and snippets.

@KrishnaKotari
Created April 2, 2015 06:30
Show Gist options
  • Save KrishnaKotari/9a98318452e03783e58e to your computer and use it in GitHub Desktop.
Save KrishnaKotari/9a98318452e03783e58e to your computer and use it in GitHub Desktop.
All Code related to Part 2 of my series on Vaadin Grid
HeaderRow filterRow = grid.appendHeaderRow();
HeaderCell manufacturerFilter = filterRow.getCell("manufacturer");
TextField textField = new TextField();
textField.setImmediate(true);
//On Change of text, filter the data of the grid
textField.addTextChangeListener(getManufacturingFilterListener());
manufacturerFilter.setComponent(textField);
/**
* Returns the TextChangeListener that gets triggered
*
* @return
*/
private TextChangeListener getManufacturingFilterListener() {
return new TextChangeListener() {
/**
*
*/
private static final long serialVersionUID = -2368474286053602744L;
@Override
public void textChange(TextChangeEvent event) {
String newValue = (String) event.getText();
@SuppressWarnings("unchecked")
BeanItemContainer<VehicleInfo> container = ((BeanItemContainer<VehicleInfo>) grid
.getContainerDataSource());
// This is important, this removes the previous filter
// that was used to filter the container
container.removeContainerFilters("manufacturer");
if (null != newValue && !newValue.isEmpty()) {
container.addContainerFilter(new SimpleStringFilter(
"manufacturer", newValue, true, false));
}
grid.recalculateColumnWidths();
}
};
}
//Get the container if you have already set it to grid and
//wrap it in GeneratedPropertyContainer
GeneratedPropertyContainer container = new GeneratedPropertyContainer(
grid.getContainerDataSource());
grid.setContainerDataSource(container);
// Add a generated Column to the table
container.addGeneratedProperty("totalSalesTillDate",
new PropertyValueGenerator<double>() {
/**
*
*/
private static final long serialVersionUID = -7625273505835964597L;
@Override
public Double getValue(Item item, Object itemId,
Object propertyId) {
VehicleInfo vehicleInfo =(VehicleInfo) itemId);
Long y2012 = vehicleInfo.getSales2012().getTotalSales();
Long y2013 = vehicleInfo.getSales2013().getTotalSales();
Long y2014 = vehicleInfo.getSales2014().getTotalSales();
return new Double(y2012 + y2013 + y2014) / (1000000 * 12);
}
@Override
public Class<double> getType() {
return Double.class;
}
});
grid.getColumn("totalSalesTillDate")
.setHeaderCaption("Sales Till Date");
/*
* Remove the properties you wish to hide. Have done it already in
* native container? even then you have do it as this is a new container
*/
container.removeContainerProperty("id");
// A new getter called Total Sales was added for YearlySales
container.removeContainerProperty("sales2012.totalSales");
container.removeContainerProperty("sales2013.totalSales");
container.removeContainerProperty("sales2014.totalSales");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment