Created
January 29, 2018 11:01
-
-
Save melistik/2f8a6f5447788b78ce8daae53889843b to your computer and use it in GitHub Desktop.
Vaadin: Refresh Grid-Rows when underlying Object updated
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 com.vaadin.server.VaadinRequest; | |
import com.vaadin.spring.annotation.SpringUI; | |
import com.vaadin.ui.Grid; | |
import com.vaadin.ui.TextField; | |
import com.vaadin.ui.UI; | |
import com.vaadin.ui.themes.ValoTheme; | |
import lombok.AllArgsConstructor; | |
import lombok.Data; | |
import lombok.NoArgsConstructor; | |
import lombok.RequiredArgsConstructor; | |
import java.util.Arrays; | |
import java.util.List; | |
@SpringUI(path = "/test") | |
public class TestUI extends UI { | |
private ArticleGroup articleGroup = new ArticleGroup(Arrays.asList( | |
new Article("One", 10, 0), | |
new Article("Two", 20, 0), | |
new Article("Three", 5, 0))); | |
private Grid<Article> grid; | |
private Grid.Column quantityColumn; | |
@Override | |
protected void init(VaadinRequest request) { | |
articleGroup.updateDistributions(); | |
grid = new Grid<>("Distribution Grid"); | |
grid.setSizeFull(); | |
grid.addFooterRowAt(0); | |
grid.addColumn(Article::getName) | |
.setCaption("Name"); | |
quantityColumn = grid.addComponentColumn(article -> { | |
TextField quantity = new TextField(); | |
quantity.setValue(String.valueOf(article.getQuantity())); | |
quantity.setWidth("100%"); | |
quantity.addStyleName(ValoTheme.TEXTAREA_ALIGN_RIGHT); | |
quantity.addStyleName(ValoTheme.TEXTFIELD_BORDERLESS); | |
quantity.addValueChangeListener(e -> quantityChanged(article, Integer.parseInt(e.getValue()))); | |
return quantity; | |
}) | |
.setCaption("Quantity"); | |
grid.addColumn(Article::getDistribution) | |
.setCaption("Distribution"); | |
grid.setItems(articleGroup.getArticles()); | |
setContent(grid); | |
} | |
private void quantityChanged(Article article, Integer newValue) { | |
article.setQuantity(newValue); | |
articleGroup.updateDistributions(); | |
grid.getFooterRow(0) | |
.getCell(quantityColumn) | |
.setText(String.valueOf(articleGroup.getSum())); | |
grid.getDataProvider().refreshAll(); | |
} | |
@RequiredArgsConstructor | |
@Data | |
public static class ArticleGroup { | |
private final List<Article> articles; | |
public void updateDistributions() { | |
articles.stream() | |
.forEach(a -> a.setDistribution(a.getQuantity() / (double) getSum())); | |
} | |
public int getSum() { | |
return articles.stream() | |
.mapToInt(Article::getQuantity) | |
.sum(); | |
} | |
} | |
@AllArgsConstructor | |
@NoArgsConstructor | |
@Data | |
public static class Article { | |
private String name; | |
private int quantity; | |
private double distribution; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment