7

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

14

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

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;
    }
}
2
  • @SessionScoped might be to "big", @ViewScoped is totally fine here and works. Please also refer to @BalusC 's post: stackoverflow.com/questions/7031885/…
    – Roland
    Mar 28, 2018 at 21:49
  • 1
    This solution does set the filter value but does not invoke filtering.
    – Milgo
    Mar 7, 2019 at 8:28
2

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

1

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>
3
  • 1
    :in this way default value is setting in the filter value. But it is not working
    – Ssv
    Mar 18, 2013 at 6:47
  • @kolussus:by using script function I am trying to get the default value. but the load method of datatable is not getting the result
    – Ssv
    Mar 19, 2013 at 5:53
  • @Sagar try $('#yourDesiredColumnId\\:filter').trigger('keyup') to manually trigger the filter keyup filter event to achieve the filtered effect
    – kolossus
    Mar 20, 2013 at 3:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.