Search code examples
ajaxpaginationnavigationwicket-6

Ajax Pagination Wicket. Retrieve new data from db when next page is clicked


I want to paginate my results. First I've tried the classic way and it worked, my dataView retrieves a list with results from database and displays the number of results I want per page. (Looks like this << <1 2 3 4 5> >> )

final DataView<RequestEntity> dataView = new MyDataView();
dataView.setItemsPerPage(10);

linksContainer.add(new PagingNavigator("pageNavigator", dataView));

<a wicket:id="pageNavigator"></a>

Now I want to retrieve data from database only when the next page is clicked (kind of lazy loading/ lazy pagination). So I modified my DAOObject like this: query.setMaxResults(entriesPerPage);

It is the same query like before but this time it will take the amount of results I want per page. And it works, it retrieves as much entries as I want per page. The problem is that I don't know how to display another page. It appears just one page with the first entries from the query. (Looks like this << 1 >>)

My idea is to use links instead of AjaxPagingNavigator to display pages from 1 to 5 and when the link is clicked the query is executed. I don't think my idea is good. Can you help me? I hope my question isn't too stupid. Thanks


Solution

  • Done! All I needed to do is to create IDataProvider that knows everything. If you create it you don't need to worry about the size( about tricking it to show more pages).

    IDataProvider dataProvider = new IDataProvider<RequestEntity>() {
        RequestEntityDAOExtra requestEntityDAOExtra =
                ((MyApp) getApplication()).getMyRequestDAO();
        @Override
        public Iterator<? extends RequestEntity> iterator(long first, long count) {
            List<RequestEntity> entitiesByDomainList = requestEntityDAOExtra.getEntityByDomain(
                    domainInput.getModelObject(), (int) count, (int) first);
            return entitiesByDomainList.iterator();
        }
    
        @Override
        public long size() {
            return requestEntityDAOExtra.getEntityByDomainCount(domainInput.getModelObject());
        }
    
        @Override
        public IModel<RequestEntity> model(final RequestEntity requestEntity) {
            return new LoadableDetachableModel() {
                @Override
                protected RequestEntity load() {
                    return requestEntity;
                }
            };
        }
    
        @Override
        public void detach() {
    
        }
    };
    
    
    final DataView<RequestEntity> dataView = new MyDataView(dataProvider, 10);
    
    private class MyDataView extends DataView<RequestEntity> {
        public MyDataView(IDataProvider dataProvider, int i) {
            super("linksList", dataProvider, i);
        }
    
        @Override
        protected void populateItem(final Item<RequestEntity> item) {
            .....
        }
    }