0

EDIT:

public class ProductWrapper {

    public Product2 product {get;set;}
    public asset_line_items__c ali {get;set;}    

    public ProductWrapper(Product2 p, asset_line_items__c ali) {
        product = p;
        this.ali = ali;
    }
}

END:

Is there a way to start List index based on 1 rather than 0 ?

I'm having issue with pagination setPageSize, I'm loading the data off of custom wrapper through List.

when the very first page loads it add extra row, meaning that, if I setPageSize = 5 I expect to see 5 rows correct but I see 6 rows and when I click the next button I see 5 rows.

After looking through my code I realize that since I'm using the List and List index starts with 0.

What will be the alternate solutions to this problem? Is that best practice to use List in the pagination scenarios?

public List<ProductWrapper> Products 
     {
      get 
      {
         List<Product2> products2 = new List<Product2>(); 
         list<ProductWrapper> pws = new list<ProductWrapper>();                 
         for(Product2 p : (List<Product2>)standardSetCtrl.getRecords()) {  
           if(p.Asset_Line_items__r.isEmpty()){
               pws.add(New ProductWrapper(p,New Asset_Line_Item__c(Quantity__c = 0));
               continue;
           }
        for(asset_line_items__c b : p.asset_line_items__r) { 
            ProductWrapper pw = new ProductWrapper((Product2)p,b);
            pws.add(pw);
         }
     }    
  Products2 = pws;
  return Products2;
 }
 set;
}  

My standardSetController:

 Public Integer noOfRecords{get; set;}
    Public Integer size{get;set;}
    public ApexPages.StandardSetController standardSetCtrl {
        get{
            if(standardSetCtrl == null){
                size = 5;
                string queryString = 'Select Name....';
                standardSetCtrl = new ApexPages.StandardSetController(Database.getQueryLocator(queryString));
                standardSetCtrl.setPageSize(size);
                noOfRecords = standardSetCtrl.getResultSize();
            }
            return standardSetCtrl;
        }set;
    }
3

Your code, as written, wouldn't compile. You're trying to assign a List<ProductWrapper> to a List<Product2>. From what I can deduce, you're actually adding one line item per asset line item, which means that one of your products probably had two line items. It's kind of hard to tell exactly, because of your compilation problems. The problem definitely is not the fact that lists are zero-based. In fact, to prove I'm not crazy, I wrote some code:

public class AccountPagination {
    public ApexPages.StandardSetController c { get; set; }
    public AccountPagination() {
        c = new ApexPages.StandardSetController(Database.getQueryLocator([SELECT Name FROM Account]));
        c.setPageSize(5);
    }
}

<apex:page controller="AccountPagination">
    <apex:form >
        <apex:pageBlock >
            <apex:variable var="counter" value="{!0}" />
            <apex:pageBlockButtons >
                <apex:commandButton action="{!c.first}" value="First" />
                <apex:commandButton action="{!c.previous}" value="Prev" />
                <apex:commandButton action="{!c.next}" value="Next" />
                <apex:commandButton action="{!c.last}" value="Last" />
                {!c.pageNumber} of {!(c.resultSize+c.pageSize-1)/c.pageSize}
            </apex:pageBlockButtons>
            <apex:pageBlockTable value="{!c.records}" var="acct">
                <apex:column >
                    <apex:variable var="counter" value="{!counter+1}" />
                    {!counter}
                </apex:column>
                <apex:column value="{!acct['Name']}" />
            </apex:pageBlockTable>
        </apex:pageBlock>
    </apex:form>
</apex:page>

You can try this out in your org, too, and you'll see what proper pagination should look like. You simply have a logic bug in your code elsewhere.


Edit: added a wrapper as an example:

public class AccountPagination {
    public class AccountWrapper {
        public Account record { get; set; }
        public AccountWrapper(Account record) {
            this.record = record;
        }
    }
    public ApexPages.StandardSetController c { get; set; }
    public AccountPagination() {
        c = new ApexPages.StandardSetController(Database.getQueryLocator([SELECT Name FROM Account]));
        c.setPageSize(5);
    }
    public AccountWrapper[] getWrapperList() {
        AccountWrapper[] results = new AccountWrapper[0];
        for(Account record:(Account[])c.getRecords()) {
            results.add(new AccountWrapper(record));
        }
        return results;
    }
}

<apex:page controller="AccountPagination">
    <apex:form >
        <apex:pageBlock >
            <apex:variable var="counter" value="{!0}" />
            <apex:pageBlockButtons >
                <apex:commandButton action="{!c.first}" value="First" />
                <apex:commandButton action="{!c.previous}" value="Prev" />
                <apex:commandButton action="{!c.next}" value="Next" />
                <apex:commandButton action="{!c.last}" value="Last" />
                {!c.pageNumber} of {!(c.resultSize+c.pageSize-1)/c.pageSize}
            </apex:pageBlockButtons>
            <apex:pageBlockTable value="{!wrapperList}" var="acct">
                <apex:column >
                    <apex:variable var="counter" value="{!counter+1}" />
                    {!counter}
                </apex:column>
                <apex:column value="{!acct.record.Name}" />
            </apex:pageBlockTable>
        </apex:pageBlock>
    </apex:form>
</apex:page>
  • yes if you use any standard object or custom object that works but have yo u tried using any wrapper class and load the data into the List? as you can see I'm using the List<ProductWrapper> I will update you the wrapper code in a sec. – Nick Kahn Sep 20 '16 at 1:11
  • I have updated my question with wrapper class to give you clear picture. – Nick Kahn Sep 20 '16 at 1:12
  • @NickKahn Adding a wrapper to my code didn't change the result: it still works as expected. See my edit. – sfdcfox Sep 20 '16 at 1:22
  • the one thing i see the difference is [] vs. List, I'm using List oppose to [] – Nick Kahn Sep 20 '16 at 1:24
  • 1
    @NickKahn That's syntactic sugar. They mean the same thing as far as the compiler's concerned, but I have an aversion to typing List<> instead of just []. I did change it in my developer org, and as expected, it still works correctly. I don't deny you're having a problem somewhere, but it's not the code you think it is. The StandardSetController is reliable for pagination without any special "tricks." – sfdcfox Sep 20 '16 at 1:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.