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
mritzmritz 

apex:repeat with inputText

Apex Code

public class myClass1{

        public string out1{get;set;}
        public list<decimal> capitals{get;set;}
        public list<decimal> revenues{get;set;}
        
        public myClass1(){
            out1='Hello';
            capitals=new list<decimal>(4);
            revenues=new list<decimal>(4);
            for(integer i=0;i<4;i++){
                capitals[i]=revenues[i]=i;
            }
        }
        public void mySave(){
           out1=String.valueOf(capitals[1]);
          //this line should display value entered in second input text in place of 'Hello'
        }
}


VisualFroce page


<apex:page controller="myClass1" showHeader="false" doctype="HTML-5.0">
    <apex:form id="form">
        <apex:tabPanel >
            <apex:tab label="one">
                <p>Tab one Content</p>
                <apex:outputtext id="out1" value="{!out1}" escape="false"/>
                <apex:commandButton value="Save" action="{!mySave}" rerender="dataTable,out1,rpt"/>
                <table id="dataTable" style="border:2px solid green">
                    
                    <apex:repeat value="{!capitals}" var="c" id="rpt">
                    <tr>
                        <td><apex:inputtext  value="{!c}"/></td>
                    </tr>
                    </apex:repeat>    
                </table>
            </apex:tab>
            <apex:tab label="two">
            
            </apex:tab>
        </apex:tabPanel>
    </apex:form>
    
</apex:page>
ideally above code should display numbers 0 to 4 in four input Text fields.

When i assign  value="{!c}", for inputText.
i am getting an error -> 'unkown property c'
in my opinion i should work perfectly.

Please correct my mistake.
Best Answer chosen by mritz
Leslie  KismartoniLeslie Kismartoni
How about this... Create an iterator series and use that as the index:

Apex Class 
This also includes a button action that helps read out the current values of the list to debug.
public class tst_ctr_account {

	public list<decimal> capitals{get;set;}
    public List<Integer> iterator {get; set;}
    
    public tst_ctr_account() {
        capitals=new list<decimal>(4);
		for(integer i=0;i<4;i++){
	        capitals[i]=i;
        }

        iterator = new List<Integer>();
		for(integer i=0; i < capitals.size(); i++){
            iterator.add(i);
        }
    }
    
    public PageReference saveList() {
        System.debug('********' + capitals);
        Return null;
    }
    
}

VF Page:
<apex:page controller="tst_ctr_account" docType="html-5.0" >

    <apex:form >
        <apex:commandlink immediate="false" action="{!saveList}" value="debug"/>
        <br/>
        
        <apex:repeat var="c" id="repeatList" value="{!iterator}">
            <apex:inputtext value="{!capitals[c]}"/><br/>
        </apex:repeat>
        
    </apex:form>

</apex:page>

Avoids the problems once you perform an action... Hope this helps...
 

All Answers

Leslie  KismartoniLeslie Kismartoni
The trouble is that input won't bind directly to the list values. With a little syntax-sugar, you can probably work around the issue. The trick is to use a variable (counter) that lets you access the list value directly. Then the input bind without a problem:

Controller
public class tst_ctr_account {

    public List<Integer> intList { get; set; }
    
    public tst_ctr_account() {
        intList = new List<Integer>();
        intList.add(0);
        intList.add(1);
        intList.add(2);    
        intList.add(3);                        
    }
    
}

Page
 
<apex:page controller="tst_ctr_account" docType="html-5.0" >

    <apex:form >
        <apex:variable value="0" var="num"/>        
        <apex:repeat var="c" id="repeatList" value="{!intList}">
            
            <apex:inputtext value="{!intList[Value(num)]}"/><br/>

            <apex:variable value="{!VALUE(num) + 1}" var="num"/>
        </apex:repeat>
        
    </apex:form>

</apex:page>


Hope this helps... 


 
Leslie  KismartoniLeslie Kismartoni
Here's a screen shot of it working, btw... (My page was just called "accttest"

User-added image

 
mritzmritz
Getting following error:

Subscript value 4 not valid. Must be between 0 and 3
Error is in expression '{!capitals[Value(num)]}' in component <apex:inputText> in page test_list1


Code:
<apex:variable value="0" var="num"/>
                    <apex:repeat value="{!capitals}" var="c" id="rpt">
                    <tr>
                        <td><apex:inputtext value="{!capitals[Value(num)]}"/>
                            <apex:variable value="{!VALUE(num) + 1}" var="num"/>
                        </td>
                        <td><apex:inputtext /></td>

                    </tr>
 </apex:repeat>

 
mritzmritz
I also see similar screen.
but it seems its showing index.

if you change value in constructor. you will see error.
or
even if you try to change values in input text  and do some action on it, it flags error.
Leslie  KismartoniLeslie Kismartoni
How about this... Create an iterator series and use that as the index:

Apex Class 
This also includes a button action that helps read out the current values of the list to debug.
public class tst_ctr_account {

	public list<decimal> capitals{get;set;}
    public List<Integer> iterator {get; set;}
    
    public tst_ctr_account() {
        capitals=new list<decimal>(4);
		for(integer i=0;i<4;i++){
	        capitals[i]=i;
        }

        iterator = new List<Integer>();
		for(integer i=0; i < capitals.size(); i++){
            iterator.add(i);
        }
    }
    
    public PageReference saveList() {
        System.debug('********' + capitals);
        Return null;
    }
    
}

VF Page:
<apex:page controller="tst_ctr_account" docType="html-5.0" >

    <apex:form >
        <apex:commandlink immediate="false" action="{!saveList}" value="debug"/>
        <br/>
        
        <apex:repeat var="c" id="repeatList" value="{!iterator}">
            <apex:inputtext value="{!capitals[c]}"/><br/>
        </apex:repeat>
        
    </apex:form>

</apex:page>

Avoids the problems once you perform an action... Hope this helps...
 
This was selected as the best answer