Is there any library that can be used to implement paging for a list?
Let' assume I have a space of 10 lines, and the user can select if he wants to scroll forward or backward by page (thus +- 10 items). This might eg be controlled by -1, 0, +1
.
This is probably much work to build a class that prevents scrolling backward/forward if there are not enough items to display, and to self-save the state on which page the user is currently.
So is there anything?
For example, you can implement pagination using links to new pages on your ecommerce site, or using JavaScript to update the current page. Load more and infinite scroll are generally implemented using JavaScript.
You should use a instead of button , as the pagination (as the name implies) leads to a different page/URL. Then you could use the rel values next and prev for the corresponding pagination links. If you insist on using button but you still have separate pages, you could use the link element to provide these rel values.
Same as left but from right. Template for the pagination items. The number of items at each page are decided by the List.js own property page. To set this just add page: Number to the option object sent into the List.js constructor (as been done in both of the examples at this page).
How To Create a Pagination Step 1) Add HTML: Example <div class="pagination"> <a href="#"> « </a> <a href="#"> 1 </a> <a class="active" href="#"> 2... Step 2) Add CSS: Example /* Pagination links */ .pagination a { color: black; float: left; padding: 8px... W3.CSS Tutorial
Search engines like Google are also a great example of using pagination in search engine results. This makes it easier for users to find what they are looking for in other pages if they cannot find it on page 1.
The end result of the pagination algorithm is almost always the same with some minor front-end differences, such as CSS, inclusion of the last/first page link if there are more page links then visible pages window (pages window is the number of visible links—for example, 10), or logic behind of the “next/prev” link.
Minor optimization, if you don't really want to create any new list at all.
/**
* returns a view (not a new list) of the sourceList for the
* range based on page and pageSize
* @param sourceList
* @param page, page number should start from 1
* @param pageSize
* @return
* custom error can be given instead of returning emptyList
*/
public static <T> List<T> getPage(List<T> sourceList, int page, int pageSize) {
if(pageSize <= 0 || page <= 0) {
throw new IllegalArgumentException("invalid page size: " + pageSize);
}
int fromIndex = (page - 1) * pageSize;
if(sourceList == null || sourceList.size() <= fromIndex){
return Collections.emptyList();
}
// toIndex exclusive
return sourceList.subList(fromIndex, Math.min(fromIndex + pageSize, sourceList.size()));
}
I've solved this before. I made a static getPages method that breaks a generic collection into a list of pages (which are also lists). I've provided the code below.
public static <T> List<List<T>> getPages(Collection<T> c, Integer pageSize) {
if (c == null)
return Collections.emptyList();
List<T> list = new ArrayList<T>(c);
if (pageSize == null || pageSize <= 0 || pageSize > list.size())
pageSize = list.size();
int numPages = (int) Math.ceil((double)list.size() / (double)pageSize);
List<List<T>> pages = new ArrayList<List<T>>(numPages);
for (int pageNum = 0; pageNum < numPages;)
pages.add(list.subList(pageNum * pageSize, Math.min(++pageNum * pageSize, list.size())));
return pages;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With