function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
logontokartiklogontokartik 

How to pass parameters from a Visualforce Pageblock table to Controller

Here is my situation, 

 

I am writing a VF page which is very much similar to NEW EDIT Views.  Here a user can select a Field, and if field is a Picklist, a button shows up. When user clicks on that button, a picklist is displayed as checkboxes, which user can select.

 

I figured out how to display check boxes as popup and other stuff (thanks to TehNrD). I am now stuck figuring out how to find which row user is clicking, since I am using pageblock table to generate the search fields. 

 

IS there a way I can pass the parameter user selects from VF back to controller?? 

 

Here is my VF Code:

 

 

 <apex:PageBlockSection id="addfieldsBlock" title="Search Fields" columns="1" rendered="{!NOT(isReadonly)}" showheader="{!isAdvanced}">     
          <apex:outputPanel rendered="{!isAdvanced}">                         
          <apex:pageBlockTable id="fieldtable" value="{!wsavedsearchfields}" var="mf">
           <apex:facet name="footer">
               <apex:outputPanel >               
                    <apex:commandLink style="color:#015BA7;padding:3px 3px 3px 3px;" styleClass="listActionLink" value="Add Row" action="{!addrow}">
                    </apex:commandLink>|
                    <apex:commandLink style="color:#015BA7;padding:3px 3px 3px 3px;" styleClass="listActionLink" value="Remove Row"   action="{!removeRow}" >
                    </apex:commandLink>
             </apex:outputPanel>
             </apex:facet>
                 <apex:column width="20%" >
                    <apex:facet name="header" >Field</apex:facet>
                      <apex:selectList value="{!mf.savedsearchfield.Label__c}" size="1" id="fieldslist" style="width:250px;border:{!savedfieldErr};">
                             <apex:selectOptions value="{!items}" ></apex:selectOptions>                           
                        <apex:actionSupport event="onchange" action="{!getchkLookup}" rerender="addfieldsBlock">
                      		<apex:param name="fieldLabel" value="{!mf.savedsearchfield.Label__c}" assignTo="{!fieldLabel}"/>
                      	</apex:actionSupport>                       
                      </apex:selectList>
                                             
                    <apex:outputPanel rendered="{!LEN(mf.errMessageFld)!=0}">                      
                     <div class="errorMsg"><strong>Error:</strong>&nbsp;{!mf.errMessageFld}</div>
                     </apex:outputPanel>
                  </apex:column> 
                 
                 <apex:column width="12%" >                
                    <apex:facet name="header">Operator</apex:facet>
                    <apex:inputField value="{!mf.savedsearchfield.Operator__c}" style="width:100px;"/>
                    <apex:outputPanel rendered="{!LEN(mf.errMessageOp)!=0}">                      
                     <div class="errorMsg"><strong>Error:</strong>&nbsp;{!mf.errMessageOp}</div>
                     </apex:outputPanel>
                 </apex:column>                 
                 
                 <apex:column width="20%" id="valuecolumn">                 	                
                    <apex:facet name="header">Value</apex:facet>
                    <apex:inputField value="{!mf.savedsearchfield.DefaultValue__c}"  rendered="{!LEN(mf.errMessageVal)==0}"/>                    
                    <apex:commandButton value=".." action="{!getisLookup}" rendered="{!mf.isLookup}" id="lookupButton">
                    	<apex:actionSupport event="onclick" rerender="popup">
                    		<apex:param name="fieldLabel" value="{!mf.savedsearchfield.Label__c}" assignTo="{!fieldLabel}"/>
                    	</apex:actionSupport>	 
                   </apex:commandButton>
                    <apex:outputPanel rendered="{!LEN(mf.errMessageVal)!=0}">                      
                     <div class="errorMsg"><strong>Error:</strong>&nbsp;{!mf.errMessageVal}</div>
                     </apex:outputPanel>                                                  
                 </apex:column>                                                                  
              </apex:pageBlockTable>  
              </apex:outputPanel>           
       </apex:PageBlocksection> 
       
       <apex:pageblockSection showheader="false">
       <apex:outputPanel rendered="{!isLookupfld}" id="popup" styleClass="customPopup" layout="block">
            <apex:outputPanel >            
            	<div class="heading"><strong>Please select a value:</strong></div>
                <apex:selectCheckboxes value="{!selPicklist}" layout="pageDirection">
					<apex:selectOptions value="{!picklistoptions}"/>									        	
        		</apex:selectCheckboxes>  
        		<apex:commandButton action="{!addList}" value="Insert"/>
        		<apex:commandButton action="{!returnList}" value="Cancel"/>        		      		                	        					             
            </apex:outputPanel>       				             
      </apex:outputPanel>           
      </apex:pageblockSection> 

Controller is :

 

Here is my Controller :
public PageReference getchkLookup() {
  	 		
  		String objectname = selobjectname;
        String locationfield;
        Map<String, String> picklists = new Map<String, String>();        
        picklistoptions = new List<SelectOption>();
        Set<String> picklistlabels = new Set<String>();
        Integer lsize = wsavedsearchfields.size();
        system.debug('field name' + fieldlabel);
        
        locationfield = Apexpages.CurrentPage().getParameters().get('fieldLabel');
        
        system.debug('field name' + locationfield);
        
        for(Integer i=0;i<wsavedsearchfields.size();i++){
          locationfield = fieldValueLabels.get(fieldLabel);
          if(fieldpicklists.containsKey(locationfield)) {
        	if(fieldLabel == wsavedsearchfields[i].savedsearchfield.Label__c) {
        			wsavedsearchfields[i].isLookup = true;        			
        			picklists = fieldpicklists.get(locationfield);        			
        			picklistlabels = picklists.keyset();        			
        			for(String s: picklistlabels){
        				picklistoptions.add(new Selectoption(s,picklists.get(s)));
        				
        			}        			
        		}else {
        			wsavedsearchfields[i].isLookup = true;        			
        			picklists = fieldpicklists.get(locationfield);        			
        			picklistlabels = picklists.keyset();        			
        			for(String s: picklistlabels){
        				picklistoptions.add(new Selectoption(s,picklists.get(s)));
        				wsavedsearchfields[i].lookupvalues.add(picklists.get(s));
        			}
        	 }
        	} else
        		wsavedsearchfields[i].isLookup = false;
        }
        
  	return null;	
  }
    
  
  public List<SelectOption> getpicklistoptions(){
  	return picklistoptions;
  }
  
  public String[] selPicklist{ 
  	get {
  		List<String> selPicklist = new List<String>();
  		List<SelectOption> sos = this.picklistoptions;
  		for(Selectoption s:sos){
  			if(selcheckboxString.contains(s.getValue()))
  			selPicklist.add(s.getValue());
  		}
  		return selPicklist;
  	}  	
  	set {
  		system.debug('Inside String');
  		String listValueconcat = '';
  		for(String s : value){
  			if(listValueconcat=='')
  			listValueconcat += s;
  			else
  			listValueconcat += ';' + s;
  		}
  		selcheckboxString = listValueconcat;   		
  	}
  	
  }
  
  public void getisLookup(){
  	
  	Integer lsize = wsavedsearchfields.size();
  	isLookupfld = false;
  	
  	
  	for(Integer i=0;i<wsavedsearchfields.size();i++){
  		if(fieldLabel == wsavedsearchfields[i].savedsearchfield.Label__c ){
  			if (wsavedsearchfields[i].isLookup)
  				isLookupfld = true;
  		}
  	}
  	
  }

 

 


 

 

 

Any help is appreciated.

 

Thanks

Kartik 

Best Answer chosen by Admin (Salesforce Developers) 
logontokartiklogontokartik

Finally I figured out that instead of using command button if we use Command LInk and attach <apex:param parameter to the command link, we can pass the value from VF page to Controller.

 

This helped me a lot when figuring out which row we are accessing in the pageblocktable. (we can also use a wrapper class to add a RowID to every row in the pageblock table.

All Answers

SteveBowerSteveBower

Isn't your setter for selPicklist being called to set the user selected value?   Or am I not understanding your question?  Best, Steve.

logontokartiklogontokartik

Yes, it is setting the value, but its setting into an Array. How will I know which array position is the value set? unless I pass the parameter and compare it with the array that i am using to populate the pageblocktable.

 

I am not sure why the <apex:param> parameter not passing the value back to controller before the actionsupport action method is called. ??  Can you please clarify me on this?

 

logontokartiklogontokartik

Finally I figured out that instead of using command button if we use Command LInk and attach <apex:param parameter to the command link, we can pass the value from VF page to Controller.

 

This helped me a lot when figuring out which row we are accessing in the pageblocktable. (we can also use a wrapper class to add a RowID to every row in the pageblock table.

This was selected as the best answer
DevNVDevNV

I've seen this same issue before - using a commandButton doesn't pass the param value when the commandLink does. 

 

The documentation seems to say that either component can support the param component but I have not had any luck with the button.  Unfortunately I want to display a button and not a link, so I'm trying to figure out the styles to use to turn it into a button.  Anyone have that handy?  So far I have the following but it isn't quite the right height:

 

 

<apex:commandLink value=" Remove Risks " action="{!ListDelete}" styleClass="btn" style="padding:2px 3px 2px 3px; text-decoration:none;"> 
    <apex:param assignTo="{!listName}" value="EnvironmentRisks" name="link"/> 
</apex:commandLink>