Tags in my blog

Showing posts with label paging. Show all posts
Showing posts with label paging. Show all posts

27 November 2009

Paging Calculation Note

Formula to calculate starting row from maxRows: maxRows*(page-1)
Formula to calculate numberOfPages (count + maxRows - 1) / maxRows

public List getCustomers() {
return customerManager.findAllCustomers(MAX_RESULTS * (page-1), MAX_RESULTS);
}

public long getNumOfPages() {
return (customerManager.findAllCustomersCount().longValue() + MAX_RESULTS - 1) / MAX_RESULTS;
}


import java.util.List;

import javax.faces.context.FacesContext;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.holibi.pharmabeans.db.manager.CustomerManager;
import com.holibi.pharmabeans.db.model.Customer;

@Controller("listCustomerBean")
@Scope("request")
public class ListCustomerBean {
private static final int MAX_RESULTS = 4;

@Autowired
private CustomerManager customerManager;

private int page;

public ListCustomerBean() {
FacesContext context = FacesContext.getCurrentInstance();
String strPage = context.getExternalContext().getRequestParameterMap().get("page");
if (strPage != null) {
page = Integer.parseInt(strPage);
}
if (page <= 1) {
page = 1;
}
}

public List getCustomers() {
return customerManager.findAllCustomers(MAX_RESULTS * (page-1), MAX_RESULTS);
}

public long getNumOfPages() {
return (customerManager.findAllCustomersCount().longValue() + MAX_RESULTS - 1) / MAX_RESULTS;
}

public void action() {
System.out.println("Page: " + page);
}
}