How to add Dynamically page block on button click.? pleas can any one help me.
thanks In Advance.
How to add Dynamically page block on button click.? pleas can any one help me.
thanks In Advance.
You can use apex:repeat with a list to generate as many Page Blocks as you like. The code below will display a button called "Add Section". One new page block will be added every time you click on the button. Hope this helps.
<apex:page controller="TestController">
<apex:form>
<apex:commandButton value="Add Section" reRender="sectionsPanel" action="{!addSection}" />
<apex:outputPanel id="sectionsPanel">
<apex:repeat value="{!lstSections}" var="section">
<apex:pageBlock title="PageBlock">
<apex:pageBlockSection title="PageBlockSection">
<apex:pageBlockSectionItem>Do Something Here</apex:pageBlockSectionItem>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:repeat>
</apex:outputPanel>
</apex:form>
</apex:page>
Controller
public with sharing class TestController {
public List<String> lstSections { get; set; }
public TestController(){
lstSections = new List<String>();
}
public PageReference addSection() {
lstSections.add('dummy');
return null;
}
}
Hope this example helps you
<apex:page controller="TestMyPage" >
<apex:form >
<apex:pageBlock id="block">
<apex:pageBlockSection title="Section 1" rendered="{!showSection1}"/>
<apex:pageBlockSection title="Section 2" rendered="{!showSection2}"/>
</apex:pageBlock>
<apex:commandButton action="{!method1}" value="Show/Hide Section1" reRender="block"/>
<apex:commandButton action="{!method2}" value="Show/Hide Section2" reRender="block"/>
</apex:form>
</apex:page>
controller
public with sharing class TestMyPage{
public Boolean showSection1{get;set;}
public Boolean showSection2{get;set;}
public TestMyPage (){
showSection1= true;
showSection2= true;
}
public void method2(){
if(showSection2== true){
showSection2= false;
}else{
showSection2= true;
}
}
public void method1() {
if(showSection1== true){
showSection1= false;
}else{
showSection1= true;
}
}
}
You need to use Dynamic Visualforce Components to create VF components dynamically on runtime. For example, to create a table:
Component.Apex.PageBlockTable
See this link.