Setting default value in dftat format

I am using primers v3.5. In datatable, I use a filter in one column. How to set the default value for the filter when loading the page itself.

+4
source share
4 answers

Use the filterValue property of a column tag in simple elements, something like

<p:datatable ... widgetVar="dataTableWidgetVar"> <p:column ... filterValue="#{BackingBean.defaultValue}"> 

Then create a simple function call in javascript to start the filter when the page is ready (widget widgets are created via jQuery in PF):

 <script type="text/javascript" target="body"> $j = jQuery; $j(document).ready( function() { dataTableWidgetVar.filter(); }); </script> 
+9
source

The correct solution is to use the filteredValue p:dataTable , which contains the filtered collection along with the filterValue p:column attribute to show the filter configuration for the user.

To save your p:dataTable filters saved in your bean session, you must also save the filtered data. p:dataTable will not do the initial sort for you.

Check out this JSF example:

 <p:dataTable value="#{usersBean.employees}" var="e" filteredValue="#{userListState.filteredValue}"> <p:ajax event="filter" listener="#{userListState.onFilterChange}"/> <p:column headerText="user" filterBy="#{e.user.id}" filterValue="#{userListState.filterState('user.id')}"> #{e.user.id} </p:column> </p:dataTable> 

With this managed bean:

 @Named(value = "userListState") @SessionScoped public class UserListState implements Serializable{ private Map<String, String> filterState = new HashMap<String, String>(); private List<Employee> filteredValue; public UserListState() { } public void onFilterChange(FilterEvent filterEvent) { filterState = filterEvent.getFilters(); filteredValue =(List<Employee>) filterEvent.getData(); } public String filterState(String column) { return filterState.get(column); } public List<Employee> getFilteredValue() { return filteredValue; } public void setFilteredValue(List<Employee> filteredValue) { this.filteredValue = filteredValue; } } 
+8
source

Ideally, getting a datatable reference (either by binding the view to be mapped to the bean or walking the DOM tree), and doing so

  Map<String,String> theFilterValues = new HashMap<String,String>(); theFilterValues.put("filterColumn","fooValue"); myDataTable.setFilters(theFilterValues); 

The default text value will be set, but the filter may not be applied.

Alternatively, this post in the feather issues queue implies a jquery option

  <script> jQuery(document).ready(function() { jQuery('input[id*="datumCol"]').val('2012-07-17'); }); </script> 
+1
source

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

+1
source

All Articles