LazyDataModel doesn't work with a search form

UI Components for JSF
Post Reply
gstasica
Posts: 3
Joined: 10 Nov 2010, 19:13

20 Nov 2010, 15:05

hi,

i'm trying to combine my LazyDataModel with a search form so that whenever search criteria changes its result gets reflected in a lazy laded data table.

my page

Code: Select all

<h:form prependId="false">
   <h:inputText value="#{searchBean.name}" /> 
      <h:commandButton value="Search" action="#{searchBean.search}"/>

   <p:dataTable  paginator="true" lazy="true" rows="10" var="item" value="#{searchBean.results}" id="resultTable">
      <p:column headerText="Name" sortBy="#{item.name}" >#{item.name} /></p:column>
    </p:dataTable>

</h:form>
my backing bean

Code: Select all

@ManagedBean
@ViewScoped
public class SearchBean{

  private LazyDataModel<Model> results;
  private String name;

  public SearchBean(){
    results = new SearchsLazyLoader ();
  }

    private final class SearchsLazyLoader extends LazyDataModel<Model> {
      public List<Model> load(int first, int pageSize, String sortField, boolean sortOrder,
		        Map<String, String> filters)
		{
                   return mydao.findModelsByName(name);  //here i simply pass name param from a bean to dao
                }
     }

     public final String search(){
       results = new SearchsLazyLoader ();
       results.setRowCount(10);  //for simplicity i always return 10 rows       
     }

     public void doAjax(){  //i used this method for p:commandButton tag in my second test
        search();
     }

   ... //getter and setters code follows
  
}
while running this code the results are as follow:
1. the first time i hit "search" button data are loaded from dao and table gets populated accordingly
2. changing search criteria (updating search name param) doesn't update table (using debugger i can see that there isn't any call to load method in SearchLazyLoader)
3. doing sort operation on a name column updates table and there is a call to SearchLazyLoader

i found this issue http://code.google.com/p/primefaces/iss ... il?id=1356 and tried to follow all suggested steps but with no luck (i have no idea why the issue's status is "Cant replicate" as one needs just a few lines of code to see that the problem really exist) with the exception that case 2 on my previous list has changed i.e. my SearchLazyLoader gets called but for some reason table doesn't get updated

i'd like to add that the same results i get when used with p:ajax tags i.e.

Code: Select all

<p:commandButton value="Search" actionListener="#{searchBean.doAjax}" process="@form" update="resultTable">
is it possible to use lazy loaded data table with a search form? Neither of the example available in a showcase covers this scenario but i would imagine that using these two is very common.


i've been using primefaces 2.2.rc1

callahan
Posts: 768
Joined: 27 May 2010, 22:52

20 Nov 2010, 19:47

The search method assigns the dataTables data-source a new value on the fly. This isn't a good idea, as the datatable caches the data-source, so assigning it a new value will cause problems.

gstasica
Posts: 3
Joined: 10 Nov 2010, 19:13

21 Nov 2010, 12:52

Thanks for your reply but could you be more specific what you meant and more importantly how could i make search form update data table.

I think that the this type of functionality where on one page one has a search form that updates data table is very common and as such should be supported out of the box. I probably miss something in my code/page that would allow me to combine these two.

Anyone using a search form with a data table component ?

callahan
Posts: 768
Joined: 27 May 2010, 22:52

21 Nov 2010, 14:24

What I ment is that the PrimeFaces DataTable component inherits form the JSF UIData component and that the UIData component internally caches a reference to its value. The value for your DataTable is stored in the SearchBean results property. If you assign a new value to results, the DataTable will not know about this as it still holds a cached reference to the old value.

DataTable has a method called resetValue which can be called to force the DataTable to throw away its cached value and access the new value when required. Something similar to the following should work.

Code: Select all

     public final String search(){
       results = new SearchsLazyLoader ();
       results.setRowCount(10);  //for simplicity i always return 10 rows       

       DataTable dataTable = (DataTable)  FacesContext.getCurrentInstance().getViewRoot().findComponent("theDataTablesId");
       dataTable.resetPagination(); // jump to first page
       dataTable.resetValue();
     }
Note that this is supported out of the box when lazy loading is not used. See http://www.primefaces.org/showcase/ui/d ... tering.jsf. When lazy loading is used, the DataTable can't know how to access data. It can't automatically generate SQL statements to extract the required information from the database. Don't forget that PrimeFaces is a JSF component suite and not a full blown Web application stack.

Post Reply

Return to “PrimeFaces”

  • Information
  • Who is online

    Users browsing this forum: No registered users and 35 guests