+ Start a Discussion

problem with select options!

hi, i have this table in my visual force page. the fist column of the table is a select option and the others are text. I have a button "add row". This button adds a row to the table, however, because the table needs to be rerendered for the table to show the new row. This causes all select options to go back to the default value. How can you make it so when u click add row each row still has its original select option from before and not the default value. here is the code

<apex:form > 
        <apex:pageBlockButtons >
            <apex:commandButton value="Save" action="{!save}" rerender="error"/>
            <apex:commandButton value="Cancel" action="{!cancel}" rerender="error"/>
        <apex:pageblockTable value="{!sblogs}" var="sb" id="table">
            <apex:column headerValue="Sold Item" style="width:110px;border:1px solid #f3f3eb;">
                <apex:selectList multiselect="false"   size="1" value="{!sb.Sold_Item__c}" style="width:400px;">  
                        <apex:selectOptions value="{!GetSoldItems}"/>
            <apex:column headerValue="Assigned To">
                <apex:inputField value="{!sb.Assigned_To__c}" required="true"/>
            <apex:column headerValue="Hours Worked">
                <apex:inputField value="{!sb.Hours_Worked__c}" style="width:3em;"/>
            <apex:column headerValue="Percent Complete">
                <apex:inputField value="{!sb.Percent_Complete__c}" style="width:3em;"/>
            <apex:column headerValue="Cost">
                <apex:inputField value="{!sb.Cost__c}"/>
        <apex:pageblockButtons location="bottom">
            <div style="text-align:right;margin-right:30px;font-weight:bold;">
                <apex:commandLink value="Add Row" action="{!addRow}" rerender="table,error" immediate="true" />
                <apex:commandLink value="Remove Row" action="{!removeRow}" rerender="table,error" immediate="true" />                


public with sharing class BacklogContExt {

    public List<Backlog__c> sblogs {get; set;}
    public final print__c parprint;
    public BacklogContExt(ApexPages.StandardController myController) {
        parprint=[Select ID,Name
        from print__c 
        WHERE ID = :((print__c)myController.getrecord()).ID];
        sblogs = new List<Backlog__c>();
        Backlog__c SBacklog = new Backlog__c();
        SBacklog.print__c = parprint.Id;
        }//end of constructor

    public void addrow() {
        Backlog__c SBacklog = new Backlog__c();
        SBacklog.print__c = parprint.Id;
    public void removerow(){
        Integer i = sblogs.size();
    public PageReference save() {
        insert sblogs;
        PageReference parrec = new PageReference ('/'+parprint.id);
        return parrec; 
    public PageReference cancel() {
        PageReference parrec = new PageReference ('/'+paSprint.id);
        return parrec; 
 /* Begin Select option */
 List<SelectOption>  SelectListOfSoldItems= new List<SelectOption>();
        public  List<SelectOption> GetSoldItems{  
             	 return SelectListOfSoldItems;
             private set;        
     Private void setSoldItems(print__c print){   
            for (Sold_Item__c SoldItem:[SELECT ID,Name 
                                        FROM Sold_item__c 
                                        Completion__c <> 100.0
                                        AND  Sold_Item__c.Project__r.name=:parprint.Project__c
               SelectListOfSoldItems.ADD(new SelectOption(SoldItem.id, SoldItem.Name));                                        



Its because you have the "immediate" attribute set to "true" on your buttons - this tells the page to discard any edits the user may have made when submitting the page.  If you remove these, the user's selections will be retained.