gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 03:34:37 24 4
gpt4 key购买 nike

我们基于 Vaadin Blog 实现了延迟网格加载文章并希望为某些网格列添加过滤器。在本例中,我们通过将 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() { ... }
}

设置数据提供者:

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()
);

最佳答案

在使用 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);

关于filter - Vaadin Grid - 具有延迟加载的多个过滤器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46816726/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com