+ Start a Discussion
Jyothsna ReddyJyothsna Reddy 

Add entered text values to an List/map....

Hello everyone,
     Good Morning,
     Please have a glance on  the code and images .My main target is to add entered text values to an array/list/map.
 {!textarray} ----->But If you look to figure 2 I am getting values in textarray .
But I am getting only last entered value i.e., eeeee. Previously entered values are getting replaced.
I think I need to add these "textArray" values to List.
To acheive that I changed the datatype of textarray as Map<String,String>  Then I got the error as shown below
[EXTERNAL]|System.TypeException: Invalid conversion from runtime type String to MAP<String,String>
....................................

VisualForce Code:
<apex:page controller="repeatandsave" showHeader="false">
  <apex:form >
   <apex:pageBlock >
    <apex:repeat value="{!con}" var="a" id="re">
         <apex:inputtext value="{!textarray}" id="in" >
            <!-- <apex:actionsupport event="onblur" action="{!loopList}"  rerender="re"/>-->
         </apex:inputtext>
         <br/>
    </apex:repeat>
    <apex:commandButton value="Save" action="{!myaction}"/>
   </apex:pageBlock>
  </apex:form>
</apex:page>
Controller/Apex Code:

public class repeatandsave {
    public Map<String,String> strList;
    public String textarray {get; set;}
    Map<Integer, Contact> mapToContact = new Map<Integer, Contact>();
    public list<contact> myconlist{set;get;}
   //Constructor
    public repeatandsave(){
        Integer i = 0;
        for (Contact a :[Select Id, Name From Contact limit 5]) {
            mapToContact.put(i, a);
            i++;
        }
       
    }
    
    public Map<Integer,Contact> getcon (){    
        return mapToContact;
    }
    //save action
    public void myaction(){
      
        system.debug('action invoked::::::::::+textarray'+textarray);
    }
}
ScreenShot:
                           fig 1
First Image
User-added image
                       fig2


bob_buzzardbob_buzzard
The problem here is that you have bound every input to the same controller property - textarray.  As Visualforce makes no guarantee about the order that setters will be called, the last one to fire will overwrite everyone else's changes.

A better approach to this would be to use a wrapper class to encapsulate a contact and the text input - that way you have a separate string for each input.

You can find more information on wrapper classes at:

https://developer.salesforce.com/page/Wrapper_Class
vamshi(Nani)vamshi(Nani)

Hey do a small change in your controller

 

public String[] textarray {get; set;}

vamshi(Nani)vamshi(Nani)

and can you clarify one thing why you are fetching contacts from sfdc. if you are target is saving the values to a array/list.

and where you want use these contact values..if  you give the clear picture sure i will help you.