When implementing LazyDataModel, I added a default filter to the class variable in the class constructor. In this example, the class variable is called "filters", and filtering is performed in the "isActive" field with a value of "true":
public class ExtendedLazyListModel<T> extends LazyDataModel<T> { private final List<T> datasource; private Map<String, Object> filters; public ExtendedLazyListModel(List<T> datasource) { this.filters = new HashMap<>(); filters.put("isActive", "true"); this.datasource = datasource; this.setRowCount(datasource.size()); }
Then, in the Load method, I added this code to set the default filter (only for the first call):
public List<T> load(int first, int pageSize, String sortField, SortOrder sortOrder, Map<String, Object> filters) { //set default filter if (filters.isEmpty()){ for (Field f : datasource.get(0).getClass().getDeclaredFields() ){ if (this.filters.containsKey(f.getName())) { filters.put(f.getName(), this.filters.get(f.getName())); this.filters.remove(f.getName()); } } } .....
In this example, I added the p: column ... filter column to the XHTML file:
filterValue = "true" // - the value set for the default filter
source share