Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

setting default value in primefaces datatable Filter

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.

like image 726
Ssv Avatar asked Mar 16 '13 14:03

Ssv


3 Answers

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>    
like image 132
acaruci Avatar answered Oct 30 '22 12:10

acaruci


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;
    }
}
like image 35
Pavel Sedek Avatar answered Oct 30 '22 13:10

Pavel Sedek


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>
like image 23
kolossus Avatar answered Oct 30 '22 13:10

kolossus