setting default value in primefaces datatable Filter

前端 未结 4 1276
日久生厌
日久生厌 2020-12-06 03:01

I am using primefaces v3.5.On the datatable I am using filter on one column. How can I set a default value to the filter while loading the page Itself.

相关标签:
4条回答
  • 2020-12-06 03:44

    The correct solution is to use the filteredValue attribute of p:dataTable which contains the filtered collection together with filterValue attribute of p:column to show the filters configuration to the user.

    To keep your p:dataTable filters stored in your session bean, you have to keep also the filtered data. The p:dataTable wouldn't perform the initial sorting for you.

    Check this example JSF:

    <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>
    

    Backed 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;
        }
    }
    
    0 讨论(0)
  • 2020-12-06 03:50

    Ideally, obtaining a reference to the datatable (either by binding the view datatable to a backing bean representation or walking the DOM tree) and doing this

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

    Will set a default text value, but might not apply the filter.

    Alternatively, this post in the primefaces issues queue suggests a jquery based option

        <script>
            jQuery(document).ready(function() {
            jQuery('input[id*="datumCol"]').val('2012-07-17');
             });
        </script>
    
    0 讨论(0)
  • 2020-12-06 03:52

    Use the filterValue property of the column tag in primefaces, something like

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

    Then, create a simple function call in javascript for triggering the filter, when the page is ready (the widget vars are created via jQuery in PF):

    <script  type="text/javascript" target="body">
    $j = jQuery;
    $j(document).ready( function() {
    dataTableWidgetVar.filter();   
    });
    </script>    
    
    0 讨论(0)
  • 2020-12-06 03:59

    When implementing the LazyDataModel, I added a default filter to a class variable in the class constructor. In this example, the class variable is called "filters" and the filtering is done on the "isActive" field with value "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 in the XHTML file to the filtering column p:column ...:

    filterValue="true" // is the value I set for the default filter

    0 讨论(0)
提交回复
热议问题