1

How would I go about passing the ID from a record in an apex:repeat iteration into a variable used in a controller method and then display the value the method returns in the VF page?

I've hacked around quite a bit with apex:param but haven't been able to get this to work as expected.

Here's where I'm at so far:

<apex:repeat value="{!mycourses}" var="course">
  <div class="course clearfix">
    <div class="progress-container">
      <apex:input type="text" value="{!wholeNumberComplete}" styleclass="progress">
        <apex:param name="courseId" value="{!course.id}" assignTo="{!courseId}"/>
      </apex:input>
      <label>Complete</label>
    </div>
  </div>
</apex:repeat>

public List<Training_Plan__c> mycourses;
public List<Learning_Assignment__c> learnings;
public Decimal displayNumber {get; set;}

public List<almond__Training_Plan__c> getmycourses() {
  mycourses = SystemUtils.getCourses();
  learnings = SystemUtils.getLearningAssignments(courseId);
  displayNumber = learnings.size();
  return mycourses;
}

Thanks in advance for the assistance.

2 Answers 2

1

I'm not sure if you didn't copy all of your code over or not but I do not see a variable named courseId. Also from the developer guide...

The component can only be a child of the following components:

<apex:actionFunction>
<apex:actionSupport>
<apex:commandLink>
<apex:outputLink>
<apex:outputText>
<flow:interview>

There is an awesome blog that will walk you through an example as well here. Example from the blog:

<apex:commandButton value="Del" action="{!delCont}" rerender="all">
   <apex:param name="contIdParam" value="{!cont.id}" assignTo="{!contIdChosen}"/>
</apex:commandButton>

public String contIdChosen {get; set;}

0

You can always use Maps for this Using Maps in Visualforce

Check below examples

public Map<String,String> directors {
get {
    return new Map<String, String> {
        'Kieslowski' => 'Poland', 
        'del Toro' => 'Mexico', 
        'Gondry' => 'France'
    };
}
 set;
}

Using Visualforce for same

<apex:repeat value="{!directors}" var="dirKey">
    <apex:outputText value="{!dirKey}" /> -- 
    <apex:outputText value="{!directors[dirKey]}" /><br/>
</apex:repeat>

In your case you want to process logic inside repeat ,which i will avoid and instead already cache the logic for all keys inside map.

Check below example as well

<apex:page controller="ListsMapsController">
<apex:outputPanel id="box" layout="block">
    <apex:pageMessages/>
    <apex:form >

        <apex:repeat value="{!inputFields}" var="fieldKey">
            <apex:outputText value="{!fieldKey}"/>: 
            <apex:inputText value="{!inputFields[fieldKey]}"/><br/>
        </apex:repeat>

        <apex:commandButton action="{!submitFieldData}" 
            value="Submit" id="button" rerender="box"/>

    </apex:form>
</apex:outputPanel>

public class ListsMapsController {

public Map<String, String> inputFields { get; set; }

public ListsMapsController() {
    inputFields = new Map<String, String> { 
        'firstName' => 'Jonny', 'lastName' => 'Appleseed', 'age' => '42' };
}

public PageReference submitFieldData() {
    doSomethingInterestingWithInput();
    return null;
}

public void doSomethingInterestingWithInput() {
    inputFields.put('age', (Integer.valueOf(inputFields.get('age')) + 10).format());
 }
}
6
  • Thanks for the reply but I'm still stuck...I'm iterating over a list of sObjects records and need to take the ID from each record and pass it into a variable used in a controller method. The controller method uses this record ID to create a list of child records. From this list of child records, I'm then performing a math calculation and am returning a decimal. This decimal is then what needs to be displayed in the VF page.
    – John_Henry
    Commented Sep 17, 2015 at 19:14
  • you would need an action to do this .Provide a button and run action for all this .Doing everything inside repeat is not recommended . Commented Sep 17, 2015 at 19:20
  • Is there a way I could invoke the action on page load?
    – John_Henry
    Commented Sep 17, 2015 at 19:25
  • Use Action attribute sir .apex:page has action to call all your actions Commented Sep 17, 2015 at 19:27
  • Thanks. How would I go about passing the ID from each record in apex:repeat into the method called from the action attribute?
    – John_Henry
    Commented Sep 17, 2015 at 19:30

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .