Pagination

Discussion in 'Resources' started by gomeow, Mar 16, 2013.

Thread Status:
Not open for further replies.
  1. Offline

    gomeow

    I couldn't find any resource on how to paginate a list and then showing it to the user, so I wrote a method myself.

    Code:java
    1. public void paginate(CommandSender sender, SortedMap<Integer, String> map,
    2. int page, int pageLength) {
    3. sender.sendMessage(ChatColor.YELLOW + "List: Page (" + String.valueOf(page) + " of " + (((map.size() % pageLength) == 0) ? map.size() / pageLength : (map.size() / pageLength) + 1));
    4. int i = 0, k = 0;
    5. page--;
    6. for (final Entry<Integer, String> e : map.entrySet()) {
    7. k++;
    8. if ((((page * pageLength) + i + 1) == k) && (k != ((page * pageLength) + pageLength + 1))) {
    9. i++;
    10. sender.sendMessage(ChatColor.YELLOW + " - " + e.getValue());
    11. }
    12. }
    13. }

    How to use it:
    Code:java
    1. /*
    2. * Collections.reverseOrder() is optional.
    3. * It makes it so the highest numbers get shown first.
    4. * Otherwise the lowest number will be shown first.
    5. */
    6. SortedMap<Integer, String> map = new TreeMap<Integer, String>(Collections.reverseOrder());
    7. map.put(1, "Thing");
    8. /*
    9. * The first parameter is the rank, and the second parameter is the text to be shown.
    10. * The SortedMap will automatically sort it.
    11. * Add to that map for as many values there are in the list you want to show.
    12. */
    13. paginate(sender, map, 1, 5);
    14. /*
    15. * Pass the first parameter as the sender.
    16. * The second parameter as the map.
    17. * The third as the page number.
    18. * The fourth as how long each page should be.
    19. */

    I hope this helps you, reply if you have any questions
     
  2. Offline

    chasechocolate

    Oohh nice! Will be helpful when you have extensive list of plugin commands.
     
  3. Offline

    gomeow

    chasechocolate
    I needed it to show a bunch of results from a MySQL table, but it is pretty versatile
     
  4. Offline

    chasechocolate

    Yeah, there's gotta be tons of things you can do this with.
     
  5. Offline

    davidtoledo

    Excellent! tks!
     
  6. Offline

    TheE

    I actually prefer @zml2008's version as it can be used for multiple listings.

    Usage is as simple as this:
    Code:java
    1.  
    2. Collection<Object> collection; //collection of the data that should be idsplayed
    3. int page = 4;
    4.  
    5. new PaginatedResult<Object obj>(" Command - Description") {
    6. @Override
    7. public String format(Object obj) {
    8. return obj.toString(); //match each points formating depending on the data that is displayed
    9. }
    10. }.display(sender, collection, page);
    11.  
     
Thread Status:
Not open for further replies.

Share This Page