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
SFDCU$erSFDCU$er 

Add values to a table dynamically

I have a visualforce page with input text  Student Name and an Add Button. When I enter a value and click Add It should add the name in the table below along with the created date and Student number which would be an auto-number field.

So far this is what I have done:

Wrapper Class:
public with Sharing class WrapperClass{
public class StudentRosterData{
public Date DateCreated { get; set; }
public String Name { get; set; }
public Integer StudentNumber { get; set; }

}
public StudentRosterData studentData{get; set;}

public WrapperClass(){
studentData = new StudentRosterData();

}
}


Visualforce Page:
<apex:page controller="StudentRosterController">
    <apex:form >
Student Name: <apex:inputText value="{!Names}" />
<apex:commandButton value="Add" action="{!AddNames}"/>
<table border="0">

<tr>
     <td><b>Name</b></td>
     <td><b> Entered Date</b> </td>
    </tr>
<apex:repeat value="{!wrapper.studentData}" var="stu">
<tr>
    <td> <apex:outputText value="{!stu.Name}"/></td>
    <td> <apex:outputText value="{!stu.StudentNumber}"/></td>
    <td> <apex:outputText value="{!stu.DateCreated}"/></td></tr>
</apex:repeat>
</table>
    </apex:form>
</apex:page>


StudentRosterController:

  
public class StudentRosterController{

        public String Names { get; set; }


        public WrapperClass wrapper{get; set;}

        public pageReference AddNames() {
        WrapperClass.StudentRosterData addstudent = new WrapperClass.StudentRosterData();

           wrapper.studentData.Name = Names;
           wrapper.studentData.DateCreated = Date.today();  
           

           return null;
        }


}


Please help.
Best Answer chosen by SFDCU$er
hitesh90hitesh90
Hi,

You have to make following changes in your Controllers.

WrapperClass
public with Sharing class WrapperClass{
    public class StudentRosterData{
        public Date DateCreated { get; set; }
        public String Name { get; set; }
        public Integer StudentNumber { get; set; }    
    }
    public List<StudentRosterData> studentData{get; set;}
    
    public WrapperClass(){
        studentData = new List<StudentRosterData>();    
    }
}

Visualforce Page:
<apex:page controller="StudentRosterController">
    <apex:form >
        Student Name: <apex:inputText value="{!Names}" />
        <apex:commandButton value="Add" action="{!AddNames}"/>
        <table border="0">    
            <tr>
            <td><b>Name</b></td>
            <td><b> Entered Date</b> </td>
            </tr>
            <apex:repeat value="{!wrapper.studentData}" var="stu">
                <tr>
                    <td> <apex:outputText value="{!stu.Name}"/></td>
                    <td> <apex:outputText value="{!stu.StudentNumber}"/></td>
                    <td> <apex:outputText value="{!stu.DateCreated}"/></td>
                </tr>
            </apex:repeat>
        </table>
    </apex:form>
</apex:page>
StudentRosterController:
public class StudentRosterController{
    public String Names { get; set; }
    public WrapperClass wrapper{get; set;}   
    Public  StudentRosterController(){
        wrapper = new WrapperClass();
    }
    public pageReference AddNames() {        
        WrapperClass.StudentRosterData addstudent = new WrapperClass.StudentRosterData();   
        addstudent.Name = Names;
        addstudent.DateCreated = Date.today();
        wrapper.studentData.add(addstudent);      
        return null;
    }
}

Thank You,
Hitesh Patel
SFDC Certified Developer & Administrator & Advanced Administrator & Sales cloud consultant
Email :- hiteshpatel.aspl@gmail.com
My Blog:- http://mrjavascript.blogspot.in/