Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

I have an PageBlockTable which has a wrapper class as its value.

Every time a command button is clicked to add a product, a new row is added to the wrapper class and the PageblockTable is rerendered to display the new row. All these work but when the rerender happens the selectedValue in the selectList is reset to the first item on the selectList.

How can i have the selectList to maintain state?

    <apex:commandButton action="{!addNewProduct}" value="Add New Product" rerender="pbtExistingInstItems,PBCopyProdToInstItems"/>
    <apex:selectList id="slprodname" value="{!itw.installedItem.Product__c}" size="1" rendered="{!itw.isManuallyCreated ==  true}" >
                <apex:selectOptions value="{!prodList}"/>
                <apex:actionSupport event="onchange" action="{!updateDefaultProductValues}"  rerender="colInstallationName">
                </apex:actionSupport>
    </apex:selectList>

Thanks

share|improve this question
    
Can you use sObjects picklist instead prodList? Is Product__c a picklist or a sting? –  mast0r Dec 17 '12 at 7:36
    
its not a picklist.. its lookup to product, but i just wanted a few products to be displayed which i am displaying from a custom setting –  Prady Dec 17 '12 at 9:40
    
I think it is not possible to hold selected values after reRendering, unless you use sObject as container or holds values in an javascript array. –  mast0r Dec 17 '12 at 9:43
    
What do you think would be the best way to handle this scenario? I need to show in few product names for users to choose from, have to capture their IDs and place it in a field called product__c. –  Prady Dec 17 '12 at 13:06
    
Do you have any sObject where products are stored as a picklist? –  mast0r Dec 17 '12 at 13:15

2 Answers 2

up vote 1 down vote accepted

Just want to explain my solution. Lets say you have some custom object "SomeObject" and a picklist field "ProductList__c" on that, where are all products collected. And you have a wrapper class. All you need is to add a copy of "SomeObject" to every row. And then at your page it could be like this:

<apex:commandButton action="{!addNewProduct}" value="Add New Product" rerender="pbtExistingInstItems,PBCopyProdToInstItems"/>

<apex:inputField value="{!itw.SomeObject.ProductList__c}" rendered="{!itw.isManuallyCreated}" id="slprodname">
    <apex:actionSupport event="onchange" action="{!updateDefaultProductValues}"  rerender="colInstallationName">
</apex:inputField>

The picklist itw.SomeObject.ProductList__c will be rendered as a normal lelect list where selected value will be stored even if you re-renders the pageblock.

share|improve this answer

Its depends on the which kind of pagereference you return from your action method addNewProduct

If you are returning full URL then its load whole page, that's why it is reloading and reset to first item in select list.

so use only partial page load, return partial URL

PageReference pr = new PageReference('/apex/PageName'); // for partial page load.

return pr; // use this

PageReference pr = new PageReference(URL.getSalesforceBaseUrl().toExternalForm()+'/apex/PageName'); // for full page load.

return pr; //don't use this.

share|improve this answer
    
i just do a rerender, no pageRef –  Prady Dec 17 '12 at 6:33
    
Its the same page which gets refreshed and its done using rerender tag. So there is no need of any pageRefs in this instance –  Prady Dec 17 '12 at 6:52

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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