Datatable: lazyLoading and ajax updates to selected rows?

UI Components for JSF
Post Reply
tomik
Posts: 32
Joined: 21 Jan 2011, 06:50

12 Jun 2011, 23:44

Hi,

I have a datatable of records that I want to add modify (e.g., add comments, etc.) once the table is loaded. When lazyloading is off, I can ajax-update the outputPanel which wraps around the datatable. However, when I have lazy loading enabled, the page on which a record is modified invokes a call to the DB to reload the datatable page.

Is this the way the datatable is supposed to behave? Is there a way to have a lazy-loaded datatable with ajax updates of individual records without having to re-load the data for a given page?

Thank you!

healeyb
Posts: 365
Joined: 07 Apr 2010, 16:05

13 Jun 2011, 02:22

Whenever you update the dataTable with ajax, lazy loaded or not, it's
going to re-read the list specified by p:dataTable value=, and whether
or not this results in a db lookup is up to you. Your getter for the value=
should only ever be a standard IDE generated getter because it gets
called a lot more than you think. I tend to reload the collection backing
the datatable on page load, and do a database refresh only if the current
user has added or removed anything from the list. As an optimisation
I may store changes made by the current user in the database, update
the backing list in memory as a sort of cache and not do a database
refresh.

But you raise an interesting point.

With ajax you're updating a component (i.e. the datatable), so imagine
rows 1-16 displayed over 2 pages with 8 rows per page. You do an ajax
update="yourTable", but what's changed? have you added a comment to
an existing row, added a new row which would give you 17 rows over 3
pages, or removed a row - it is not known.

As things stand, when you update a lazy loaded table it has to assume
the worst, that rows have been added or removed, and retieve the data
to find out how many rows there are, how many rows per page, and how
many pages the rows will be displayed over.

The next problem is remembering which page you are looking at when
viewing data in the paginator. What will happen by default is that you'll
go back to page 1 (after an ajax update), the state you started in when
the page first loaded.

One thing I do is have a lazy loaded table which shows a list of events
in chronological order in a datatable. I also have a calendar view in
which I can click on one of the events, and it will take me to the correct
page in the lazy loaded datatable. To do so I need to know which page
to show, so I need to know how many events there are, how many
events per page, and where the selected event is in the list.

To do this you use p:dataTable binding= to an org.primefaces...DataTable
property in your backing bean. You then call setFirst() & setPage() from a
listener to setup the page the show.

(note: hard coding 8 rows per page in this example)

Code: Select all

private org.primefaces.component.datatable.DataTable eventTable;

List<Event> eventList = selectedEntity.getEventList();
int rowIndex = eventList.indexOf(selectedEvent);
eventTable.setFirst(rowIndex); // row number start from 0
eventTable.setPage((rowIndex / 8) + 1); // page number start from 1
The contradiction that crossed my mind when doing this was that I'm
using a lazy loaded datatable to load only a page of data at a time, and
then I'm re-loading the entire table (or at least the data required to do
the necessary calculations) just to get on the right page. I quickly realised
though that given the expected usage profile the benefits of using the
lazy loaded table for viewing data would outweigh the less common case
of "going to the right page" - or in your case when rows are being added
or removed. You should still win with a lazy loaded datatable.

It would be really great to be able to update a datatable with ajax and
indicate updateButImNotAddingOrDeletingAnyRowsSoRememberWhatPage
IWasOn="component-id", or maybe something more succinct! I've not
considered how feasible this would be but it'd be good.

Regards,
Brendan.

tomik
Posts: 32
Joined: 21 Jan 2011, 06:50

13 Jun 2011, 21:02

Thanks for the response, Brendan.

So in other words, it is not possible to have a lazy laoded datatable and not have the active page in the table reloaded upon an ajax-update of a single existing record (non-delete).

BTW, the getter/setter methods in my backingBean are structured exactly based on the Primefaces example in their showcase (http://www.primefaces.org/showcase/ui/datatableLazy.jsf). The only difference is that my load method calls a service method to retrieve the data from a DB.

T.

Post Reply

Return to “PrimeFaces”

  • Information
  • Who is online

    Users browsing this forum: No registered users and 53 guests