+ Start a Discussion
designdesign 

SelectOptions in PageBlockTable

When creating entries in the table - no problems, but when viewing the already existing entries - SelectOption reset to default value (-- None --).

How to display the stored value (Campaign Name) in SelectOption for each row?


Controller:

 

public class VisitReportController{

	private Visit__c visit;
	public List<PosMaterial__c> posMaterials;
	ID campaignID;
	
	public VisitReportController(ApexPages.StandardController controller){
		this.visit = (Visit__c)controller.getRecord();
	}
	
	public ID getCampaigns(){
		return campaignID;
	}
	
	public void setCampaigns(ID campaignID){
		this.campaignID = campaignID;
	}
	
	public List<PosMaterial__c> getPosMaterials(){
		if (posMaterials == null){
			getExistPosm();
		}
		return posMaterials;
	}
	
	public VisitProduct__c [] getExistPosm(){
		posMaterials = [select id, VisitID__c, CampaignID__c, POSType__c, POSQuantity__c
						from PosMaterial__c where VisitId__c = :visit.Id];
		return posMaterials;
	}
	
	public void setPosMaterials(List<PosMaterial__c> posms){
		posMaterials = posms;
	}
	
	public List<SelectOption> getActiveCamps(){
		List<Campaign> cams = [select id, Name from Campaign where isActive =: true];
		
		List<SelectOption> options = new List<SelectOption>();
			options.add(new SelectOption('','-- None --'));
			for (Campaign cam : cams){
				options.add(new SelectOption(cam.id,cam.Name));
			}
		return options;
	}
	
	public PageReference save(){
		try {
			for (PosMaterial__c posm : posMaterials){
				if (posm.VisitID__c == null){
					posm.VisitId__c = visit.id;
				}
				posm.CampaignID__c = campaignID;
			}
			upsert posMaterials;
			
			PageReference visitPage = new PageReference('/' + ApexPages.currentPage().getParameters().get('id'));
			visitPage.setRedirect(true);
			return visitPage;
		} catch (Exception e){
			ApexPages.addMessages(e);
		}
		return null;
	}
	
	public void addPosm(){
		posMaterials.add(new PosMaterial__c());
	}
}

 

 

Visualforce:

 

<apex:page standardController="Visit__c" extensions="VisitReportController">
<apex:form>
<apex:pageBlock>
	<apex:pageBlockTable value="{!posMaterials}" var="prod" rendered="{!NOT(ISNULL(posMaterials))}" id="posm">
		<apex:column headerValue="{!$ObjectType.POSMaterial__c.fields.POSType__c.label}" >
			<apex:inputField value="{!prod.POSType__c}" required="true"/>
		</apex:column>
		<apex:column headerValue="{!$ObjectType.POSMaterial__c.fields.CampaignId__c.label}">
			<apex:selectList value="{!campaigns}" multiselect="false" size="1">
				<apex:selectOptions value="{!ActiveCamps}"/>
			</apex:selectList>
		</apex:column>
		<apex:column headerValue="{!$ObjectType.POSMaterial__c.fields.POSQuantity__c.label}">
			<apex:inputField value="{!prod.POSQuantity__c}" required="true"/>
		</apex:column>
	</apex:pageBlockTable>
	<apex:outputPanel ><apex:commandLink value="Add row" action="{!addPosm}" reRender="posm"/></apex:outputPanel>
</apex:pageBlock>
</apex:form>
</apex:page>

 

 

Edwin VijayEdwin Vijay

If  you would want your selected values to be displayed then that is controlled by the function below

 

 

public ID getCampaigns(){
		return campaignID;
	}
	
	public void setCampaigns(ID campaignID){
		this.campaignID = campaignID;
	}

 

I would suggest using a Wrapper class when you want to associate a custom list with a Sobject type. Search in the developer forum and you will find a sample code using Wrapper class.

 

Pradeep_NavatarPradeep_Navatar

You can call Page acton attribute to show the stored values in org which you have selected through select option :

 

           Public string campaigns{get;set;}

           Public campaign comp {get; set;}

           Public public void initApplication()

            {

                       getContact();

                       showWrapForm();

            }

            Public void getContact()

            {

                      usr = [Select ContactId, UserName, FirstName,LastName, TimeZoneSidKey from user where Id=: UserInfo.getUserId()];

                      String usrContId = usr.contactid;

                      comp = (usrContId == null) ? new campaign() : [SELECT id, POSMaterial__c FROM campaign where isActive =: true];

           }

           Public void showWrapForm()

           {

                  campaigns = comp.POSMaterial__c;

           }