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.

kolossus
  • 20,559
  • 3
  • 52
  • 104
Ssv
  • 1,059
  • 4
  • 14
  • 25

4 Answers4

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>    
acaruci
  • 879
  • 9
  • 14
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;
    }
}
Pavel Sedek
  • 519
  • 5
  • 13
  • `@SessionScoped` might be to "big", `@ViewScoped` is totally fine here and works. Please also refer to @BalusC 's post: https://stackoverflow.com/questions/7031885/how-to-choose-the-right-bean-scope – Roland Mar 28 '18 at 21:49
  • 1
    This solution does set the filter value but does not invoke filtering. – Milgo Mar 07 '19 at 08: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

Kukeltje
  • 12,223
  • 4
  • 24
  • 47
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>
kolossus
  • 20,559
  • 3
  • 52
  • 104
  • 1
    :in this way default value is setting in the filter value. But it is not working – Ssv Mar 18 '13 at 06: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 '13 at 05:53
  • @Sagar try `$('#yourDesiredColumnId\\:filter').trigger('keyup')` to manually trigger the filter keyup filter event to achieve the filtered effect – kolossus Mar 20 '13 at 03:15