+ Start a Discussion
jdeveloperjdeveloper 

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:page>
<apex:form > 
    <apex:pageblock>
        <apex:pageBlockButtons >
            <apex:commandButton value="Save" action="{!save}" rerender="error"/>
            <apex:commandButton value="Cancel" action="{!cancel}" rerender="error"/>
        </apex:pageBlockButtons>
  
        <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:selectList>
            </apex:column>
            
          
            
                          
            <apex:column headerValue="Assigned To">
                <apex:inputField value="{!sb.Assigned_To__c}" required="true"/>
            </apex:column>
            <apex:column headerValue="Hours Worked">
                <apex:inputField value="{!sb.Hours_Worked__c}" style="width:3em;"/>
            </apex:column>
            <apex:column headerValue="Percent Complete">
                <apex:inputField value="{!sb.Percent_Complete__c}" style="width:3em;"/>
            </apex:column>
            <apex:column headerValue="Cost">
                <apex:inputField value="{!sb.Cost__c}"/>
            </apex:column>
        </apex:pageblockTable>
        
        <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" />
                &nbsp;|&nbsp;&nbsp;
                <apex:commandLink value="Remove Row" action="{!removeRow}" rerender="table,error" immediate="true" />                
            </div>
        </apex:pageblockButtons>
    </apex:pageblock>  
</apex:form>
</apex:page>

 

public with sharing class BacklogContExt {

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

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

 

bob_buzzardbob_buzzard

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.