Added Multiple Row Dynamically on VF Page
Requirement: Added Multiple row based on provided a numeric value in text box
Solution:
Controller Class:
public class multiRowAddedDynamicallyController
{
public integer rowsnum { get; set; }
public List<Account> lstofaccount {get; set;}
public multiRowAddedDynamicallyController()
{
lstofaccount = new List<Account>();
lstofaccount.add(new Account());
}
public void addRows() {
for(integer i=0;i<rowsnum ;i++)
{
lstofaccount.add(new Account());
}
}
public PageReference saveClose()
{ insert lstofaccount;
PageReference pgrefernce = new PageReference('https://ap1.salesforce.com/001/o');
pgrefernce .setRedirect(true);
return pgrefernce ;
}
}
VF Page:
<apex:page controller="multiRowAddedDynamicallyController" sidebar="false">
<apex:form >
<apex:sectionHeader title="" subtitle="Account Details" help="https://sumitshuklainfo.blogspot.in/"/>
<apex:pageBlock title="Add row Dynamically" >
<apex:pageBlockButtons >
<apex:commandButton value="Save" action="{!saveClose}"/>
</apex:pageBlockButtons>
<div align="right">
<apex:inputText value="{!rowsnum }" style="width:35px"/>
<apex:commandButton value="Add rows" action="{!addRows}"/>
</div>
<br/>
<apex:pageBlockTable value="{!lstofaccount}" var="acc" id="pbtid">
<apex:column headerValue="Name">
<apex:inputField value="{!acc.Name}"/>
</apex:column>
<apex:column headerValue="Phone">
<apex:inputField value="{!acc.phone}"/>
</apex:column>
<apex:column headerValue="Website">
<apex:inputField value="{!acc.website}"/>
</apex:column>
</apex:pageBlockTable>
</apex:pageBlock>
</apex:form>
</apex:page>
SnapShot:
Solution:
Controller Class:
public class multiRowAddedDynamicallyController
{
public integer rowsnum { get; set; }
public List<Account> lstofaccount {get; set;}
public multiRowAddedDynamicallyController()
{
lstofaccount = new List<Account>();
lstofaccount.add(new Account());
}
public void addRows() {
for(integer i=0;i<rowsnum ;i++)
{
lstofaccount.add(new Account());
}
}
public PageReference saveClose()
{ insert lstofaccount;
PageReference pgrefernce = new PageReference('https://ap1.salesforce.com/001/o');
pgrefernce .setRedirect(true);
return pgrefernce ;
}
}
VF Page:
<apex:page controller="multiRowAddedDynamicallyController" sidebar="false">
<apex:form >
<apex:sectionHeader title="" subtitle="Account Details" help="https://sumitshuklainfo.blogspot.in/"/>
<apex:pageBlock title="Add row Dynamically" >
<apex:pageBlockButtons >
<apex:commandButton value="Save" action="{!saveClose}"/>
</apex:pageBlockButtons>
<div align="right">
<apex:inputText value="{!rowsnum }" style="width:35px"/>
<apex:commandButton value="Add rows" action="{!addRows}"/>
</div>
<br/>
<apex:pageBlockTable value="{!lstofaccount}" var="acc" id="pbtid">
<apex:column headerValue="Name">
<apex:inputField value="{!acc.Name}"/>
</apex:column>
<apex:column headerValue="Phone">
<apex:inputField value="{!acc.phone}"/>
</apex:column>
<apex:column headerValue="Website">
<apex:inputField value="{!acc.website}"/>
</apex:column>
</apex:pageBlockTable>
</apex:pageBlock>
</apex:form>
</apex:page>
SnapShot:
Thanks
Sumit Shukla
sumitshukla.mca@gmail.com
9711055997
Comments
Post a Comment