如何在使用Vaadin 8网格过滤器时计算总数

4

我知道我需要使用grid.getDataProvider()来获取ListDataProvider(假设我已经将一个List发送到了grid.setItems())。为了计算页脚总数,我执行以下操作:

Collection myItems = ((ListDataProvider)grid.getDataProvider()).getItems();
for(MyItem myItem : myItems)
   total += myItem.getValue();
footer.getCell(footerCell).setText(format(total));

但是如果我加上页脚,这就会失败,因为它会计算我的网格中的所有项目。例如,如果我添加:
((ListDataProvider)grid.getDataProvider()).addFilter(myFilter);

由于页脚不是过滤后的总计,而是完整的网格总计,因此顶部的代码会失败。

话虽如此,有人建议我使用

grid.getDataCommunicator().fetchItemsWithRange(...);

然而,这是一种受保护的方法。假设我创建自己的子类,我甚至不知道该方法的工作原理。

但即使如此,这似乎过于复杂,而且应该是简单的事情,特别是如果有能力在网格中添加筛选功能。

因此,我的重要问题是:如果我筛选了网格,如何计算Vaadin 8 Grid的页脚总计?

1个回答

5
要重新计算总数,您可以使用DataProviderListener,它会在过滤器更改时触发。在实现中,您可以使用QueryDataProvider提取项目,因为fetch方法还考虑了您定义的过滤器。
下面的示例主要基于Vaadin grid sampler,其想法是显示每月行情清单及其总数。过滤器将允许您查看从某个月份开始的数据(这有点傻,但它能帮助您入门)。
import com.vaadin.data.provider.ListDataProvider;
import com.vaadin.data.provider.Query;
import com.vaadin.ui.ComboBox;
import com.vaadin.ui.Grid;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.components.grid.FooterRow;
import com.vaadin.ui.components.grid.HeaderRow;
import com.vaadin.ui.themes.ValoTheme;

import java.util.List;
import java.util.Random;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

public class FilteredGrid extends VerticalLayout {
    public FilteredGrid() {
        // list data provider with some random data
        Random random = new Random();
        List<Quote> quotes = IntStream.range(1, 11).mapToObj(month -> new Quote(month, random.nextInt(10))).collect(Collectors.toList());
        ListDataProvider<Quote> provider = new ListDataProvider<>(quotes);

        // month number filter combo
        ComboBox<Integer> monthFilterCombo = new ComboBox<>("Starting with", IntStream.range(1, 10).boxed().collect(Collectors.toList()));
        monthFilterCombo.setEmptySelectionCaption("All");
        monthFilterCombo.addStyleName(ValoTheme.COMBOBOX_SMALL);
        monthFilterCombo.addValueChangeListener(event -> {
            if (event.getValue() == null) {
                provider.clearFilters();
            } else {
                provider.setFilter(quote -> quote.getMonth() > event.getValue());
            }
        });

        // grid setup
        Grid<Quote> grid = new Grid<>(Quote.class);
        grid.setDataProvider(provider);

        // header and footer
        HeaderRow header = grid.appendHeaderRow();
        header.getCell("month").setComponent(monthFilterCombo);
        FooterRow footer = grid.prependFooterRow();
        footer.getCell("month").setHtml("<b>Total:</b>");
        provider.addDataProviderListener(event -> footer.getCell("value").setHtml(calculateTotal(provider)));

        // add grid to UI
        setSizeFull();
        grid.setSizeFull();
        addComponent(grid);

        // trigger initial calculation
        provider.refreshAll();
    }

    // calculate the total of the filtered data
    private String calculateTotal(ListDataProvider<Quote> provider) {
        return "<b>" + String.valueOf(provider.fetch(new Query<>()).mapToInt(Quote::getValue).sum()) + "</b>";
    }

    // basic bean for easy binding
    public class Quote {
        private int month;
        private int value;

        public Quote(int month, int value) {
            this.month = month;
            this.value = value;
        }

        public int getMonth() {
            return month;
        }

        public void setMonth(int month) {
            this.month = month;
        }

        public int getValue() {
            return value;
        }

        public void setValue(int value) {
            this.value = value;
        }
    }
}

结果:

Vaadin 过滤表格


谢谢。我遗漏的关键部分是能够执行 new Query<>()。我不知道你可以这样做。我以为如果它不是必需的,那么它就不会是一个参数,并且在 API 中会为您创建一个空查询。无论如何,谢谢,这个方法完美地解决了我的问题! - Stephane Grenier

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接