Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

how do you calculate the last page of results in a list

Posted on 2010-08-27
2
Medium Priority
?
401 Views
Last Modified: 2012-05-10
I'm trying to calculate the first and last page of records in a list.  For example, if the returned list contained 100 records and page could display 10 records per page, I am trying to create a method to grab the first 10 records for a link that would take the user to the first 10 records in the list and then another link "last" that would take the user to the last 10 records in the list.

I have an existing method that takes the user to the next and previous set of records (see code below).  How can I get the first and last pages?

This relates to Salesforce apex programming, but I'm asking in the .net and c# zones because the logic should be similar.

Thanks for any help.
/* getlistContact returns the list of Contacts upto 10 per page implementing pagination*/    
    Public List<Contact> getlistContact()
    {
      return contactList_Next ;
    }
    
    /*  Previous method has the logic for displaying the list of Next contacts from Contact object implementing pagination*/ 
    Public void Next()
    { 
        try
        {
            showprev = true;
            contactList_Next.clear();
            Integer limit1 = 0;
  
            if(next+countPerPage < contactList.size())
                limit1 = next+countPerPage;
            else
            {
                limit1 = contactList.size();
                shownext = false;
            }
                
            for(Integer i=next; i<limit1; i++)
            contactList_Next.add(contactList[i]);
    
            Next+=countPerPage;
            
        }catch(Exception e){system.debug('Exception :'+e);}
    }
    
  /*  Previous method has the logic for displaying the list of previous contacts from Contact object implementing pagination*/    
    Public void Prev()
    {
        try
        {
            shownext = true;
            contactList_Next.clear();
            Integer limit1 = 0;        
            if(next-(countPerPage+countPerPage) > 0)
                limit1 = next-countPerPage;
            else
            {
                limit1 = next-countPerPage; 
                showprev = false;
            }
    
            for(Integer i=next-(countPerPage+countPerPage); i<limit1; i++)    
            contactList_Next.add(contactList[i]);
            Next-=countPerPage; 
        }catch(Exception e){
          system.debug('Exception :'+e);
        }               
    }

Open in new window

0
Comment
Question by:-Dman100-
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 6

Accepted Solution

by:
DalHorinek earned 1000 total points
ID: 33544909
If you want last 10 items in a list, then do this in pseudocode

limit = 10;
list_size = list.size(); // size of the list
index = list_size - limit -1 // index where to start (size of the list - limit and -1 (cuz indexing starts at zero, we know)

result = new List; // new list
for (i = index; i != list_size; i++)
      result.push(list[i]); // push last 10 items into new list

return result; // return it

But I'm not sure If I have understood correctly.
0
 
LVL 16

Assisted Solution

by:SriVaddadi
SriVaddadi earned 1000 total points
ID: 33545018

Add "using System.Linq;"

Below code gives you the last 10 records

  List<Contact> list = new List<Contact>();
            Contact[] lastPage = new Contact[10];
            list.CopyTo(list.Count-10, lastPage, 0, 10);
            List<Contact> lastPageList = lastPage.ToList<Contact>();
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

721 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question