user3791049
user3791049

Reputation: 95

Dojo enhanced grid pagination in java

I have a restful webservice using portal framework which gets hundreds of rows from database. I want to display on Dojo EnhancedGrid with pagination each time showing 10 rows using page numbers 10|20|30. I am able to do pagination with this example But my rest url is loading all the records from database which leading to performance issues. There should be some event, every time when i click on page number, it should call rest url and get 10 records from database. how can i achieve this?

Upvotes: 2

Views: 590

Answers (1)

cucicov
cucicov

Reputation: 198

Dojo Enhanced Grid with Pagination makes a call to the backend REST service each time it is necessary (clicking the next page/last page/prev page/specific page/x results per page and so on..) it passes the Range parameter in the Header of the request indicating how many items it requests for the current query (i.e. Range items=0-9 will return the first 10 items and so on). So this is automatically done by the pagination support.

What you have to do is to read this parameter in the backend REST service and return the specified rows from the database. But be careful, the pagination expects an array of objects from the database.

@GET
@Path("getSearchResults")
@Produces(MediaType.APPLICATION_JSON)
public Response getSearchResults(@HeaderParam("Range") String range) {
    // parse range String
    // perform search
    return Response.ok(responseList.toArray()).header("Content-Range", "items " + startItem + "-" + endItem + "/" + totalItems).build();
}

Also the response should contain the number of items returned and the total item number so that Pagination knows how many pages to display in the Grid and it also shows a total in the lower left corner of the Grid. This response is returned in the Header of the response as well in the following parameter: Content-Range items 0-9/120. For no results use Content-Range: */0

on the Dojo side:

store=new JsonRest({ handleAs: 'json', target: 
                    '{pathToServices}/rest/services/getSearchResults'});
grid = new EnhancedGrid({
        id: "gridId",
        store: new ObjectStore({ objectStore: store}),
        structure: getGridStructure(),
        plugins: {
            pagination: {
                pageSizes: ["25", "50", "100"],
                description: true,
                sizeSwitch: true,
                pageStepper: true,
                gotoButton: true,
                maxPageStep: 4,
                position: "bottom"},
        }
    });

That's all you have to do, Enhanced Grid Pagination takes care of everything else.

Upvotes: 2

Related Questions