0

EDIT 2:

I looped through the Products collection and no updated values found, i have updated my question again to show you how i'm doing

In the next action method I have the following:

for(ProductWrapper p : Products) 
{
      for(Asset_Line_items__r ali : p.ali) 
      { 
        system.debug('after next  ?  '+ ali ); 
      }   
       system.debug('qtyValues p: ' +  p);     
}

Here is my wrapper class looks like:

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 EDIT 2:

Edit:

private Map<Id, Integer> qValues = new Map<Id, Integer>();

public List<ProductWrapper> Products 
     {
      get 
      {
         List<Product2> products2 = new List<Product2>(); 
         list<ProductWrapper> pws = new list<ProductWrapper>();                 
         for(Product2 p : (List<Product2>)standardSetCtrl.getRecords()) {  
            for(asset_line_items__c b : p.asset_line_items__r) { 
              ProductWrapper pw = new ProductWrapper((Product2)p,b);
              pws.add(pw);
              //adding into map:
              qvalues.put(b.id, integer.valueOf(Quantity__c);
           }
         }    
  Products2 = pws;
  return Products2;
 }
 set;
}  

in the pagination:

 public void next() {
     system.debug('map data: '+qValues);
     standardSetCtrl.next();
 } 

END:

How do I persist data when I do pagination? so in my pageBlockTable I have a apex:inputField and when I enter the value and click next button to get to the next page the value i have entered is lost, how do I maintain the value while pagination (next, previous, last, first)?

Visualforce page mock-up:

<apex:pageBlock id="pb">
  <apex:pageBlockTable value="{!products}" var="a">                     
          <apex:column headerValue="quantity" >                            
                <apex:inputField value="{!a.ali.qty}" required="true" />                    
          </apex:column>
          <apex:column value="{!a.Name}" headerValue="Name"/> 
          <apex:column value="{!a.ProductName}" headerValue="P Name"/> 
  </apex:pageBlockTable>
</apex:pageBlock> 

<apex:outputPanel rendered="{!products.size>0}" >
                <apex:commandButton value="|< First" action="{!first}" rerender="properties" rendered="{!HasPrevious}" />
                <apex:commandButton value="Previous" action="{!previous}" rendered="{!HasPrevious}"  rerender="properties"/>
                Page {!pageNumber} of {!totalPageNumber}
                <apex:commandButton value="Next" action="{!next}" rendered="{!HasNext}"  rerender="properties"/>
                <apex:commandButton value="Last>|" action="{!last}" rendered="{!HasNext}" rerender="properties" />

Here is apex code:

 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));                   
           }
        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;
    }

Pagination code:

    public void first() {
         standardSetCtrl.first();
     } 
     public void last() {
         standardSetCtrl.last();
     } 
     public void previous() {
         standardSetCtrl.previous();
     }

     public void next() {
         standardSetCtrl.next();
     } 

    public Boolean hasNext {
        get {
            return standardSetCtrl.getHasNext();
        }
        set;
    }

    public Boolean hasPrevious {
        get {
            return standardSetCtrl.getHasPrevious();
        }
        set;
    }

    public Integer pageNumber {
        get {
            return standardSetCtrl.getPageNumber();
        }
        set;
    }

    public Integer totalPageNumber {
        get {
            Decimal totalSize = standardSetCtrl.getResultSize();
            Decimal pageSize = standardSetCtrl.getPageSize();
            Decimal pages = totalSize/pageSize;
            return (Integer)pages.round(System.RoundingMode.CEILING);
        }
        set;
    }
2

If you are only editing one value I suggest you keep a map in your controller (carried from page to page in the view state):

private Map<Id, Integer> values = new Map<Id, Integer>();

and add the values from the current page to that map (keyed by the record Id) as you move to the next page. Then whenever you query new pages, check if you already have a value for each record Id and set the value in the SObject if you do (so it is re-presented).

The final save then becomes (not sure what object it is in your code):

public PageReference save() {
    Asset_Line_Item__c[] updates = new Asset_Line_Item__c[] {};
    for (Id id : values.keySet()) {
        updates.add(new Asset_Line_Item__c(Id = id, Quantity__c = values.get(id));
    }
    update updates;
    return ...;
}
  • One question though, where do you put the map? is it in the next() action method? or Products action method, I have tried on both places but i'm not getting what I have entered, unless I misunderstood you. – Nick Kahn Sep 20 '16 at 14:42
  • You have the map as a field of your class so it is carried in the view state. You populate it with the values from the current page just before you move to the next page, and immediately after the new page data has been queried check the map for edited values (as the user may be paging backwards and forwards). – Keith C Sep 20 '16 at 14:47
  • I have updated my question with the changes but still I do not see the values being updated to the MAP field, please take a look. – Nick Kahn Sep 20 '16 at 15:24
  • Immediately before the debug output in your next method loop over the Products collection and grab the values. (At that point in time the updates from the UI should have been applied to the collection in the controller.) – Keith C Sep 20 '16 at 16:11
  • I looped through the Products collection and no updated values found, i have updated my question again to show you how i'm doing. – Nick Kahn Sep 20 '16 at 16:21

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.