-
FEATURED COMPONENTS
First time here? Check out the FAQ!
Hey everyone,
I got a major architectual problem with the listbox paging feature and really could need some help.
The situation
I am using a listbox with paging enabled in order to display a list of emails which are fetched live from an email server like this:
Client <- ZK server <- email server
The problem
If there are many emails to fetch this can take quite a while
Thus, I want to use listbox's paging feature in order to only fetch the emails which are currently needed.
Unfortunately, the paging feature only works correctly if all the data for the listbox is already available on the server.
Does anyone have an idea how to solve this?
My idea:
I could create a list of n dummy objects (empty instances of class Object), where "n" is the number of emails available on the server and set these as the data source for listbox.
The listbox would then automatically create the right amount of pages (with empty objects).
Then, I need to somehow check, how many rows can be displayed (per page) and which page (in the listbox) the user currently views.
Then, I could simply fetch only the emails which are required for this page.
This idea requires 2 things though:
1. How can I check how many rows fit into the currently displayed page?
2. How can I catch the event which is fired when the user changes a page?
This is just an idea though. If you have a better suggestion, any help would be really appreciated :-)
hi valmar,
About the 1 though . you can take a look this demo i created several weeks ago.
following ListModel was the suggestion we provided for ZK users for handling huge data model,
public class HugeDataListModel extends AbstractListModel { private final int pageSize; private final PageDataModel innerModel; //Assign data model only need size public HugeDataListModel(int pageSize) { this.pageSize = pageSize; if (pageSize <= 0) throw new IllegalArgumentException( "page size should be positive integer"); innerModel = new PageDataModel(); } //while getting specific item, it will call innerModel to do so public Object getElementAt(int index) { int residual = index % pageSize; return innerModel.toPage(index / pageSize).get(residual); } public int getSize() { return innerModel.getTotalSize(); } //The innder model private class PageDataModel { private int currentPageNumber; private List<Person> temporaryPage; private int totalSize; PageDataModel() { SomeThingDAO dao = new SomeThingDAO(); totalSize = dao.countAll(); temporaryPage = dao.getPersons(pageSize, 0); } public int getTotalSize() { return totalSize; } public List<Person> toPage(int pageIdx) { if (temporaryPage != null && currentPageNumber == pageIdx) return temporaryPage; SomeThingDAO dao = new SomeThingDAO(); temporaryPage = dao.getPersons(pageSize, pageIdx); currentPageNumber = pageIdx; return temporaryPage; } } }
i tried this with ZK 5.0.5 and rod technology, it works very well.
any feedback is welcome :)
Ryan
Hm, I am not sure if that helps with my problem.
I see in the constructor method that I need to provide a fixed size for each page:
public HugeDataListModel(int pageSize) { /* ... */ }
But what I need is something that actually returns me (dynamically) the number of rows that would potentially fit into the current page.
Same should happen when the user resizes the window. => An event should occur which gives me the new page size.
I found a solution for my problem:
1st, I create an array of n "dummy" objects, where n is the maximum number of emails available and set these as the data model for my listbox component. That creates n rows divided into m pages automatically.
Then, I use listbox's getPageSize() method which returns me the number of rows that currently fit into the listbox.
By calling listbox's getActivePage() function, I get the page id which is currently displayed.
With this data, I can calculate which and how many emails should be displayed in the listbox.
In order to update the listbox when the user switches to a different page, I listen to listbox' onAfterRender event.
Whenever the event gets fired, I update the model behind the listbox and voilá, it's done.
@Ryan Thanks for you code! It didn't actually solve my problem but it bumped me into the right direction ;-)
Asked: 2011-01-22 11:47:59 +0800
Seen: 747 times
Last updated: Jan 23 '11