Vaadin Grid - 具有延迟加载的多个过滤器

时间:2017-10-18 18:17:43

标签: filter grid vaadin loading lazy-evaluation

我们已根据Vaadin Blog文章实现了延迟网格加载,并希望为某些Grid列添加过滤器。在这种情况下,我们通过将lambda函数设置为CallbackDataProvider来进行延迟加载。请参阅下面的代码。

我们如何将CallbackDataProvider与过滤结合起来?根据这个StackOverflow post,作者可以通过使用ConfigurableFilterDataProvider作为CallbackDataProvider的包装来解决这个问题。可悲的是,他没有分享进一步的代码。 能否请您提供如何实现这一目标的提示?

我们的代码完全基于博客文章,如下所示:

public class Person {
  private Long id;
  private String firstName;
  private String lastName;
  private String email;
  ... getter and setters ...
}

public class PersonService {
  public List<Person> findAll(int offset, int limit) { ... }
  public int count() { ... }
}

设置DataProvider:

grid.setDataProvider(
  (sortOrders, offset, limit) -> {
    Map<String, Boolean> sortOrder = sortOrders.stream().collect(
       Collectors.toMap(
         sort -> sort.getSorted(),
         sort -> SortDirection.ASCENDING.equals(sort.getDirection())
       )
    );
    return service.findAll(offset, limit, sortOrder).stream();
  },
  () -> service.count()
);

1 个答案:

答案 0 :(得分:0)

在示例中使用Map<String, Object>类型的网格。跳过getCount()实施。

public ConfigurableFilterDataProvider<Map<String, Object>, Void, MyFilter> getFilteredDataProvider(String table)
    {
        DataProvider<Map<String, Object>, MyFilter> dataProvider = DataProvider.fromFilteringCallbacks( query -> {
            List<Map<String, Object>> list = fetchFilteredItems(table, query.getOffset(), query.getLimit(), query.getFilter());
            return list.stream();
        }, query -> getCount(table, query.getFilter()) );


        return dataProvider.withConfigurableFilter();
    }


private List<Map<String, Object>> fetchFilteredItems(String table, int offset, int limit, Optional<MyFilter> filter)
    {
        long tStart = System.currentTimeMillis();
        String sql;

        if (filter.isPresent() && !filter.get().getSql(true).isEmpty())
        {
            sql = "SELECT * FROM ( "
                    + "SELECT * FROM("
                        + "SELECT rownum rn, t.* FROM " + table + " t "
                        + filter.get().getSql() + ") "
                    + "WHERE rownum <= " + (limit+offset) + ") "
                + "WHERE rn > " + offset;
        }
        else
        {
            sql = "SELECT * FROM ("
                    + "SELECT rownum rn, t.* FROM " + table + " t"
                    + " WHERE rownum <= " + (limit+offset) + ") WHERE rn > " + offset;
        }

        List<Map<String, Object>> result = jdbcTemplate.queryForList( sql, new Object[]{});
        return result;
    }

设置数据提供者并添加过滤器:

ConfigurableFilterDataProvider<Map<String, Object>, Void, MyFilter> dataProvider = getFilteredDataProvider("myTable");
dataProvider.setFilter(myFilter);
grid.setDataProvider(dataProvider);