LazyDataModel refresh and search criteria best practices?

UI Components for JSF
Post Reply
tmanning
Posts: 4
Joined: 27 Jul 2011, 20:14

27 Jul 2011, 21:03

I'm trying to create a page where users can specify search criteria and press "search", upon which a DataTable backed by a LazyDataModel will be displayed showing the rows that match the user's search criteria, but this is proving to be surprisingly difficult so I would appreciate any advice on how to get this working.

Example: I create the page something like this (PF 3.0M3):

Code: Select all

<h:form>
  <h:outputLabel for="col1" value="col1 search term"/><h:inputText value="#{backingBean.criteria.col1}"/>
  <p:commandButton id="criteria_find" value="SEARCH" update="@form"/>
  <p:commandButton actionListener="#{backingBean.resetCriteria}" id="criteria_reset" value="RESET" update=":find_results_table" />

<p:dataTable id="find_results_table"
	lazy="true"
	paginator="true"
	rows="5"
	rowsPerPageTemplate="5,10,15"
	paginatorTemplate="{RowsPerPageDropdown} {FirstPageLink} {PreviousPageLink} {CurrentPageReport} {NextPageLink} {LastPageLink}"
	value="#{backingBean.dataModel}" 
	var="record"
	selectionMode="multiple" 
	selection="#{backingBean.selectedItems}"
        rendered="#{!backingBean.criteria.isEmpty}"
>
	<p:column headerText="Column1"><h:outputText value="#{record.number}"/></p:column>
        <p:column headerText="Column2"><h:outputText value="#{record.name}"/></p:column>
</p:dataTable>
</h:form>
My backing bean is a ViewScoped bean. Currently I initialize the datamodel in a @PostConstruct method:

Code: Select all

private Criteria criteria;
private LazyDataModel<MyEntity> model;
@PostConstruct
	public void init() {
		model = new LazyDataModel<MyEntity>() {

			@Override
			public List<MyEntity> load(int first, int pageSize, String sortField, SortOrder sortOrder, Map<String, String> filters) {
				return findEntityService.find(pageSize, first, criteria, MyEntity.class); //filters are not used, criteria act like filters in the service. Sorting is not an option.
			}
		};
		model.setRowCount(findAccountService.getRowCount(criteria, MyEntity.class));
	}
        public Object getDataModel() {
		return model;
	}
When the user hits the SEARCH button I want the backingBean.dataModel load() method to be called, but unless there are search criteria entered I don't want that method to be called, because in this case I have search criteria - not filters (i.e. don't show the user everything and let them pick a subset; they must explicitly choose their search terms). Alternatively, I suppose the method itself could check the Criteria and return null so that at least no results would be found.
When the user hits the RESET button I clear the search criteria on the backing bean and want the data table to be re-rendered again.

* How can I code this so that hitting my FIND or REFRESH commandButton will cause the data model to refresh? The update="@form" and update=":find_results_table" don't work; I've heard mention of some client-side API on the DataTable for refreshing but I don't know how to call it.

Thanks for the help!

Post Reply

Return to “PrimeFaces”

  • Information
  • Who is online

    Users browsing this forum: No registered users and 18 guests