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
miko Leemiko Lee 

How to pass value from controller to apex:component which the apex:component is put in a VF page.

// Controller

public with sharing class FTG_QB_ResultsController{
 public List<FTG_QB_Results__c> viewResInfo {get; set;}
 public void gradeRecordMethod(){
viewResInfo = [SELECT Id, Name, Date_Taken__c, FROM FTG_QB_Results__c  LIMIT  10 ] ;
}
 
<!--Page-->

<apex:page controller="FTG_QB_ResultsController" showHeader="false" sidebar="false">
<apex:form >	

<c:FTG_QB_Grade_Results  / >

  <apex:form>
</apex:page>
 
<!--Copmponent-->

<apex:component controller="FTG_QB_ResultsController"  >
    <apex:pageBlock title="Grade and Result"  > 
                <h3>Hello, {!$User.FirstName} {!$User.LastName}</h3>
                          <apex:outputText value="{!viewResInfo[0].Name}"/>
    </apex:pageBlock>
</apex:component>
How can I pass value from controller to component? I cannot get the outputText in the page. But if i did not use apex:component and put the content of component in apex:page. It can get the viewResInfo and display the value. How to solve it to pass value from controller to component?
LBKLBK
Try this controller code.

I have made a minor change to include a contructor.
 
// Controller

public with sharing class FTG_QB_ResultsController{
   public List<FTG_QB_Results__c> viewResInfo {get; set;}
   public FTG_QB_ResultsController(){
       viewResInfo = [SELECT Id, Name, Date_Taken__c, FROM FTG_QB_Results__c  LIMIT  10 ] ;
   }
}
Let me know if this helps.
 
miko Leemiko Lee

HI LBK,

I got use this way before and I know it work.... But what I want to know is can I still put viewResInfo in function and pass the value from the function to component? It is possible?

Thanks.

LBKLBK
Hi miko,

Your problem is not whether you are displaying the data inside a component or in the page itself.

Your method gradeRecordMethod() is not called even once, to build the viewResInfo list.

if you want this list to be built on page load, you need to call it within the consructor.

If you don't want to use a constructor for some reason, you can call it using apex:page action attribute.
 
<apex:page action="{!gradeRecordMethod}">
Let me know  if this helps.
miko Leemiko Lee

Hi LBK,

I try to put <apex:page> in the component but it show that <apex:page> cannot be used inside <apex:component>. Hence, I not sure how to call the function in the <apex:component>.

Actually my page still got some code. When it press View Link, it will link to component and showGradeResult.

<!--Page-->

<apex:page controller="FTG_QB_ResultsController" showHeader="false" sidebar="false">
<apex:form >	
<apex:pageBlock title="Examination Result" rendered="{!showRecords}">           
    <apex:pageBlockTable value="{!resultList}" var="rl">
        
        <apex:column value="{!rl.Exam_Date__c}"/>
        <apex:column >
            <apex:facet name="header">Grade and Result</apex:facet>
            <apex:commandLink value="View" action="{!gradeRecordMethod}">
                <apex:param name="rId" value="{!rl.Id}" assignTo="{!getExamResultId}"/> 
            </apex:commandLink>                   
        </apex:column>

</apex:pageBlockTable>

<c:FTG_QB_Grade_Results rendered="{!showGradeResult}" / >

  <apex:form>
</apex:page>

Then the controller will SELECT the field only WHERE Exam_id__r.Id =:getExamResultId. If i put viewResInfo in constructor, then  getExamResultId cannot run . So how should I pass the function instead of using <apex:page action="{!gradeRecordMethod}">

// Controller

public with sharing class FTG_QB_ResultsController{
public List<FTG_QB_Results__c> viewResInfo {get; set;}

 public void gradeRecordMethod(){
     viewResInfo = [SELECT Id, Name, Date_Taken__c FROM FTG_QB_Results__c WHERE Exam_id__r.Id =:getExamResultId    LIMIT 1 ] ;
}
<!--Copmponent-->

<apex:component controller="FTG_QB_ResultsController"  rendered="{!showGradeResult}">
    <apex:pageBlock title="Grade and Result"  > 
                <h3>Hello, {!$User.FirstName} {!$User.LastName}</h3>
                          <apex:outputText value="{!viewResInfo[0].Name}"/>
    </apex:pageBlock>
</apex:component>