p:Datatable: LazyDataModel creation

UI Components for JSF
Post Reply
nirvanbd
Posts: 45
Joined: 16 Oct 2010, 16:17

07 Nov 2010, 01:59

I hava a facelet page which displays some data using p:dataTable. When the page is initially loaded, p:dataTable should be empty. Once user enters the search criteria, the dataTable should show the results. This means that I don't know about the rowCount and also the related query till the user enters something on page and submits the page. My question is where should I put the LazyDataModel subclass instantiation code in my ManagedBean which is ViewScoped. I can't put it in constructor as I don't know the rowCount and the query to be executed. If I instantiate the model in the action method, I get a ClassCastException when the page is initially loaded.

Code: Select all

javax.faces.FacesException: javax.faces.model.ListDataModel cannot be cast to org.primefaces.model.LazyDataModel
Nirvan.

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

07 Nov 2010, 07:48

If the code is similar to your previous post, you can do the following. The LazyDataModel can be created in the constructor, just don't call it's setRowCount method. In the action method when the rowCount is known, the LazyDataModels setRowCount can be called. The datatables loadLazyData method should also be called in the action method:

Code: Select all

    int rowCount = determine number of rows here;
    lazyModel.setRowCount(rowCount);
    DataTable dataTable = (DataTable)  FacesContext.getCurrentInstance().getViewRoot().findComponent("formId:dataTableId");
    dataTable.loadLazyData();

nirvanbd
Posts: 45
Joined: 16 Oct 2010, 16:17

07 Nov 2010, 16:34

Callahan,
Thank you for your guidance. I have the Lazy dataTable working now.

regards,
Nirvan.

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

07 Nov 2010, 18:02

No problem ;).

User avatar
jmvalenzuela
Posts: 26
Joined: 19 Sep 2010, 07:54

20 Nov 2010, 00:31

nirvanbd:

could you show us the managed bean full code

please

kiler
Posts: 4
Joined: 17 Nov 2010, 13:56
Location: Warsaw

23 Nov 2010, 11:57

Hi All,

It would be grate if someone show all Bean with LazyDataModel.
I have a problem use it with data from database. I have entity bean, with facade which I use in web application.
I don't know exacly how to use with LazyDataModel it's mean I wrote something like below:


private LazyDataModel<Events> eventsLazyDataModel;

public LazyDataModel<Events> getEventsLazyDataModel() {
eventsLazyDataModel = New LazyDataModel<Events>() {
@Override
public List<Events> load(int i, int il, String string, boolean bln, Map<String, String> filters) {
return List<Events> eventsList = new ArrayList(getEventFacade().findAll());
}
};
eventsLazyDataModel.setPageRowCount(10);
eventsLazyDataModel.setRowCount(getEventFacade().recordCount());
return eventsLazyDataModel;
}

On page, there are displayed first 10 records and after go to second page there are still the same record.
Any filters and sort doesn't work.

Thanks for any sugestion.
Kiler

lobz
Posts: 21
Joined: 17 Nov 2010, 13:49
Location: Hungary

24 Nov 2010, 10:48

getEventFacade().findAll() should take into account the parameters of the load function. The list returned by load should contain only the elements on the current page but your function returns all the elements.

If you solve this, you'll probably get to a next problem which I did not manage to solve yet. See http://primefaces.prime.com.tr/forum/vi ... f=3&t=6163

Post Reply

Return to “PrimeFaces”

  • Information
  • Who is online

    Users browsing this forum: No registered users and 32 guests