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.
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>
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;
}
}
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With