Find your content:

Search form

You are here

getting only the last value in for loop

 
Share

my Vf page is

<apex:dataTable value="{!templook}" var="item" style="margin-left:60px;"  cellpadding="5" cellspacing="6"   >
    <apex:column headervalue="Payment For">
        {!payfor}
    </apex:column> 
    <apex:column headerValue="Unit Price">
        {!price}
    </apex:column>
</apex:datatable>

and in my constructor class

public class sitepage2 {
    public list<temp_invoice_lookup__c> templook{ get; set; }
    public sitepage2(){
        tempinvoiceid = apexpages.currentpage().getparameters().get('tempinvoiceid');
        templook = [select items_name__c,item_price__c,temp_invoice1__c from temp_invoice_lookup__c where Temp_Invoice1__c=:tempInvoiceid];
        for(integer i =0; i<templook.size();i++){
            payfor = templook[i].items_Name__c;
            price = templook[i].item_price__c;
        }
    }
    public void save(){
        for(integer i =0; i<templook.size();i++){
          itemlst.itemname = payfor;
          itemlst.Unitprice = price;
          system.debug('itemssss'+payfor +itemlst.itemname );
        } 
        itemslist.add(itemlst);
    }
}

getting only the last value in for loop....How should I overcome this issue


Attribution to: Eagerin Sf

Possible Suggestion/Solution #1

You're looping through your data in VF but never actually displaying items from the SELECT!

You display the variables payfor and price (and earlier in Apex you've carefully overwritten their values in the loop so indeed they contain only last item's data).

Change the VF to

<apex:dataTable value="{!templook}" var="item" style="margin-left:60px;"  cellpadding="5" cellspacing="6"   >
    <apex:column headervalue="Payment For">{!item.items_Name__c}</apex:column> 
    <apex:column value="{!item.item_price__c}" /> <!-- even better -->
</apex:datatable>

Attribution to: eyescream
This content is remixed from stackoverflow or stackexchange. Please visit https://salesforce.stackexchange.com/questions/4593

My Block Status

My Block Content