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
niharnihar 

how to addrow by using addrow button in apexclass

apex class :
public with sharing class EnrollmentFormClass {
public Contact con {get;set;}
public Contact conc {get;set;}
public Contact conct {get;set;}
public List<Contact> concAddList{get;set;}
public Account acc {get;set;}
    
public EnrollmentFormClass() {
     con  = new Contact();
     conc = new Contact();
     conct = new Contact();
     acc  = new Account();
     concAddList  = new List<Contact>();  
     //concAddList.add(con);
     }
    public void AddRow(){
        conct = new Contact();
        concAddList.add(conct);
        //concAddList.add(new Contact());
    }
public void save(){
    insert con;    
    acc.name = con.lastName;
    insert acc;
    con.AccountId = acc.Id;
    conc.AccountId = acc.Id;
    conct.AccountId = acc.Id;
    
    insert conc;
    insert conct;
    insert concAddList;
    update con;
   
    }
     
}
visualforcepage :

<apex:page controller="EnrollmentFormClass">
    <apex:form >
    <apex:pageBlock title="Opportunitiy Zone Enrollment Form">
              <apex:pageBlockSection columns="3">
              <apex:inputField value="{!con.Student_Name__c}"/>
              <apex:inputField value="{!con.School__c}"/> 
              <apex:inputField value="{!con.Date__c}"/> 
            </apex:pageBlockSection>
            <br/> 
            <br/>
                    SPARC programs are funded by United Way of Greater Atlanta. As such, we are required to report demographic, income, educational and health access information on every individual/family we serve. With the United Way belief that serving one family member serves them all, we collect general information on each member of your household. We appreciate your willingness to complete this form in its entirety so that we can continue providing free programs to the community. Thank you!
            <br/>  
            <br/> 
            <apex:pageBlockSection title="Head of Household Information:">
                <apex:inputField value="{!con.FirstName}"/>
                <apex:inputField value="{!con.LastName}"/>
                <apex:inputField value="{!con.Date_of_Birth__c}"/>
                <apex:inputField value="{!con.Gender__c}"/> 
                <apex:inputField value="{!con.Do_you_have_a_primary_medical_provider__c}"/>
                <apex:inputField value="{!con.Do_you_have_a_primary_medical_provider__c}"/>
                <apex:inputField value="{!con.Address__c}"/>
                <apex:inputField value="{!con.City__c}"/>
                <apex:inputField value="{!con.State__c}"/>
                <apex:inputField value="{!con.County__c}"/>
                <apex:inputField value="{!con.Phone__c}"/>
                <apex:inputField value="{!con.Email_Address__c}"/>
                <apex:inputField value="{!con.Preferred_Method_of_Contact_Circle_One__c}"/>
            </apex:pageBlockSection>
            <apex:pageBlocksection title="Demographic Data: (All information is confidential and is used in applying for grants to fund this program.)" columns="3">
                 <apex:inputField value="{!con.Spanish_translation_Services_Needed__c}"/>
                 <apex:inputField value="{!con.Race_Check_One__c}"/>
                 <apex:inputField value="{!con.Marital_Status_check_one__c}"/>
                 <apex:inputField value="{!con.HIGHEST_LEVEL_OF_EDUCATION_Check_one__c}"/>
                 <apex:inputField value="{!con.Employment_check_one__c}"/>
            </apex:pageBlocksection>
            <apex:pageBlocksection title="Income:" columns="3">
                <apex:inputField value="{!con.Annual_Income__c}"/> 
                <apex:inputField value="{!con.Source_of_Income__c}"/> 
                <apex:inputField value="{!con.Primary_Method_of_Transportation__c}"/>
            </apex:pageBlocksection>
            <apex:pageBlocksection title="HOUSEHOLD Information:" columns="3">
                <apex:inputField value="{!conc.Other_Adult_First_Name__c}"/>
                <apex:inputField value="{!conc.LastName}"/>
                <apex:inputField value="{!conc.Date_of_Birth__c}"/>
                <apex:inputField value="{!conc.Gender__c}"/> 
                <apex:inputField value="{!conc.Relationship_Primary_Care_Provider__c}"/>
                <apex:inputField value="{!conc.Medical_Insurance__c    }"/> 
            </apex:pageBlocksection>
                <apex:pageBlockButtons location="bottom" >
                <apex:commandButton action="{!save}" value="save"/>
            </apex:pageBlockButtons> 
            <apex:pageBlocksection title="Children (all children currently in your household, INCLUDING those participating in this program):" columns="1">
             <apex:pageBlockTable value="{!conct}" var="c">
             <apex:column headerValue="Name">
             <apex:commandButton action="{!AddRow}" value="AddRow"/>
             </apex:column>
             <apex:column headerValue="Name">
             <apex:inputfield value="{!c.LastName}"/>  
             </apex:column> 
             <apex:column headerValue="Date of Birth">
             <apex:inputfield value="{!c.Date_of_Birth__c}"/>
             </apex:column>
             <apex:column headerValue="Gender">
             <apex:inputfield value="{!c.Gender__c}"/>
             </apex:column>
             <apex:column headerValue="Relationship Primary Care Provider">
             <apex:inputfield value="{!c.Relationship_Primary_Care_Provider__c}"/>
             </apex:column>
             <apex:column headerValue="Medical Insurance">
             <apex:inputfield value="{!c.Medical_Insurance__c}"/>
             </apex:column>             
       </apex:pageBlockTable> 
       </apex:pageBlocksection>
       </apex:pageBlock>
    </apex:form>
</apex:page>
Best Answer chosen by nihar
Ayush TripathiAyush Tripathi
Hi Nihar 

This is a dummy code I guess you can add the fields name accordingly to your requirement 
You have to replace your pageblocktable by this:
<apex:page controller="sample1" >
 <apex:form >
   <apex:pageBlock >
      <apex:pageBlockTable value="{!wrappers}" var="wrapper" id="wtable">
         <apex:column headerValue="Ident">
            <apex:outputText value="{!wrapper.ident}"/>
         </apex:column>
         <apex:column headerValue="Name">
            <apex:inputField value="{!wrapper.acc.Name}"/>
         </apex:column>
         
          <apex:column headerValue="Action">
              <apex:commandButton value="Add Row" action="{!addRows}" rerender="wtable">
                  <apex:param name="addCount" value="1" assignTo="{!addCount}"/> 
              </apex:commandButton>
          </apex:column>
      </apex:pageBlockTable>
   </apex:pageBlock>
 </apex:form>
</apex:page>

In Controller Add these methods And Change the codes Accordingly
 
public class sample1 
{
 public List<ContactWrapper> wrappers {get; set;}
 public static Integer toDelIdent {get; set;}
 public static Integer addCount {get; set;}
 private Integer nextIdent=0;
  
 public sample1()
 {
  wrappers=new List<ContactWrapper>();
  for (Integer idx=0; idx<1; idx++)
  {
  wrappers.add(new ContactWrapper(nextIdent++));
  }
 }
  
 
  
 public void addRows()
 {
  for (Integer idx=0; idx<addCount; idx++)
  {
   wrappers.add(new ContactWrapper(nextIdent++));
  }
 }
  

  
 public class ContactWrapper
 {
  public Contact acc {get; private set;}
  public Integer ident {get; private set;}
   
  public ContactWrapper(Integer inIdent)
  {
   ident=inIdent;
   acc=new Contact();
  }
 }
}

Mark this thread as close if the issue has been resolved

All Answers

niharnihar
hi ayush,
when i open the link it opening the same page
shailesh_rawteshailesh_rawte
Hey

you can rerender the page after click on addrow button

Regards
Shailesh Rawte
Ayush TripathiAyush Tripathi
niharnihar
hi ayush,
can u spend some time for my code and get the output
Ayush TripathiAyush Tripathi
Hi Nihar 

This is a dummy code I guess you can add the fields name accordingly to your requirement 
You have to replace your pageblocktable by this:
<apex:page controller="sample1" >
 <apex:form >
   <apex:pageBlock >
      <apex:pageBlockTable value="{!wrappers}" var="wrapper" id="wtable">
         <apex:column headerValue="Ident">
            <apex:outputText value="{!wrapper.ident}"/>
         </apex:column>
         <apex:column headerValue="Name">
            <apex:inputField value="{!wrapper.acc.Name}"/>
         </apex:column>
         
          <apex:column headerValue="Action">
              <apex:commandButton value="Add Row" action="{!addRows}" rerender="wtable">
                  <apex:param name="addCount" value="1" assignTo="{!addCount}"/> 
              </apex:commandButton>
          </apex:column>
      </apex:pageBlockTable>
   </apex:pageBlock>
 </apex:form>
</apex:page>

In Controller Add these methods And Change the codes Accordingly
 
public class sample1 
{
 public List<ContactWrapper> wrappers {get; set;}
 public static Integer toDelIdent {get; set;}
 public static Integer addCount {get; set;}
 private Integer nextIdent=0;
  
 public sample1()
 {
  wrappers=new List<ContactWrapper>();
  for (Integer idx=0; idx<1; idx++)
  {
  wrappers.add(new ContactWrapper(nextIdent++));
  }
 }
  
 
  
 public void addRows()
 {
  for (Integer idx=0; idx<addCount; idx++)
  {
   wrappers.add(new ContactWrapper(nextIdent++));
  }
 }
  

  
 public class ContactWrapper
 {
  public Contact acc {get; private set;}
  public Integer ident {get; private set;}
   
  public ContactWrapper(Integer inIdent)
  {
   ident=inIdent;
   acc=new Contact();
  }
 }
}

Mark this thread as close if the issue has been resolved
This was selected as the best answer