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
asadim2asadim2 

Rerendering loses my values -except the last one!

Controller:
public class TestRerenderController
{
    public List<String> titles { get; set; }
    public List<VO> vos { get; set; }
    
    public TestRerenderController()
    {
        titles = new String[]{'Title A', 'Title B', ' Title C'};
        
        vos = new List<VO>();
        vos.add(new VO('text a'));
        vos.add(new VO('text b'));
        vos.add(new VO('text c'));
    }
    
    public void hi()
    {
        titles.add('Title D');
        vos.add(new VO('text d'));
    }
    
    public class VO
    {
        public String text { get; set; }
        public VO(String s)
        {
            text = s;
        }
    }
}

VF page:
<apex:page controller="TestRerenderController" id="p">
    <apex:form id="f">
    <apex:pageBlock id="pb">
        <apex:variable value="{!-1}" var="index"/>
        <apex:repeat value="{!titles}" var="title">
            <b>{!title}</b><br/>
            <apex:variable value="{!index+1}" var="index"/>
            <apex:inputText value="{!vos[index].text}"/><br/>
            <apex:commandLink action="{!hi}" value="Add D row" reRender="pb"/>
            <br/><br/>
        </apex:repeat>
    </apex:pageBlock>
    </apex:form>
</apex:page>

Problem 1:
When the commandLink on each row except the last one is clicked, I get a new row but all the input fields reset to their original values. However if I change the value of the last row and click the link on the last row, the value is remembered.

Problem 2:
On click of the link, if I rerender the page instead of pageBlock I don't get a new row. Any ideas why?!

To make it simple you can directly copy/paste above code into your org and test.
Best Answer chosen by asadim2
Matt WhalleyMatt Whalley
If you look at the note (second paragraph) of the apex:variable documentation, it does not support counters, which is what you are doing:

https://www.salesforce.com/us/developer/docs/pages/Content/pages_compref_variable.htm

Note:<apex:variable> does not support reassignment inside of an iteration component, such as <apex:dataTable> or <apex:repeat>. The result of doing so, e.g., incrementing the <apex:variable> as a counter, is unsupported and undefined.

All Answers

AshlekhAshlekh
Hi 

Just try below code
public class TestRerenderController
{
    //public List<String> titles { get; set; }
    //public List<VO> vos { get; set; }
    public List<VO1> listOfVO1{set;get;}
   
    public TestRerenderController()
    {
        //titles = new String[]{'Title A', 'Title B', ' Title C'};
        listOfVO1 = new List<VO1>();
        listOfVO1.add(new VO1('text a','title a'));
        listOfVO1.add(new VO1('text b','title b'));
        listOfVO1.add(new VO1('text c','title c'));

        /*vos = new List<VO>();
        vos.add(new VO('text a'));
        vos.add(new VO('text b'));
        vos.add(new VO('text c'));
    */
     }
   
    public void hi()
    {
        system.debug('-------------------------'+listOfVO1);
       
        listOfVO1.add(new VO1('text d','Title D'));
    }
   
   /* public class VO
    {
        public String text { get; set; }
        public VO(String s)
        {
            text = s;
        }
    }*/
   
    public class VO1
    {
        public String title{SET;GET;}
        public String text { get; set; }
        public VO1(String s,String t)
        {
            text = s;
            title =t;
        }
    }
}
Page

<apex:page controller="TestRerenderController" id="p">
    <apex:form id="f">
    <apex:pageBlock id="pb">
        
        <apex:repeat value="{!listOfVO1}" var="t">
            <b>{!t.title}</b><br/>
            <apex:inputText value="{!t.text}"/><br/>
            <apex:commandLink action="{!hi}" value="Add D row" reRender="f"/>
            <br/><br/>
        </apex:repeat>
    </apex:pageBlock>
    </apex:form>
</apex:page>


asadim2asadim2
My code is not the problem. The issue is why is VF behaving like this?
AshlekhAshlekh
Hi,

Yes there is not any error in you code but salesforce getting value in by your way on page.
Matt WhalleyMatt Whalley
If you look at the note (second paragraph) of the apex:variable documentation, it does not support counters, which is what you are doing:

https://www.salesforce.com/us/developer/docs/pages/Content/pages_compref_variable.htm

Note:<apex:variable> does not support reassignment inside of an iteration component, such as <apex:dataTable> or <apex:repeat>. The result of doing so, e.g., incrementing the <apex:variable> as a counter, is unsupported and undefined.
This was selected as the best answer
asadim2asadim2
Thanks. Any comments on the second problem?