+ Start a Discussion
rishabh rathor 18rishabh rathor 18 

I want to show dynamic picklist of Objects and its field which is done but i m facing issue that is How to use add row and delete row functionality in vf page.

Here it is my code:-
==========controller===========
public class Dynamic_Table {
    
    public Map <String, Schema.SObjectType> schemaMap = Schema.getGlobalDescribe();

    public String selectedObject {get; set;}

    public String selectedField {get; set;}
   

    Public Dynamic_Table()
    {   
        selectedObject = 'account';
        
    }

    public List<SelectOption> getObjectNames() 
    {
        List<SelectOption> objNames = new List<SelectOption>();
        List<String> entities = new List<String>(schemaMap.keySet());
        entities.sort();
        for(String name : entities)
        {
            objNames.add(new SelectOption(name,name));
        }
        return objNames;
     }

     public List<SelectOption> getObjectFields() 
     {
            Map<String, Schema.SObjectType> schemaMap = Schema.getGlobalDescribe();
            Schema.SObjectType ObjectSchema = schemaMap.get(selectedObject);
            Map<String, Schema.SObjectField> fieldMap = ObjectSchema.getDescribe().fields.getMap();
            List<SelectOption> fieldNames = new List<SelectOption>();
            for (String fieldName: fieldMap.keySet()) 
            {  
                fieldNames.add(new SelectOption(fieldName,fieldName));
              //fieldMap.get(fieldName).getDescribe().getLabel();//It provides to get the object fields label.
            }
            return fieldNames;
      }   
  
}


===========vf page==============
<apex:page controller="Dynamic_Table" lightningStylesheets="true">
    <apex:form > 
      <apex:pageBlock >
          <apex:pageBlockSection columns="2">

              <apex:pageBlockSectionItem >
                  <apex:outputlabel value="Object Names :"/> 
                      <apex:actionRegion >      
                           <apex:selectList value="{!selectedObject}" size="1">
                                    <apex:selectOptions value="{!ObjectNames}"/>
                                    <apex:actionSupport event="onchange" rerender="myFields"/>
                            </apex:selectList>
                     </apex:actionRegion>                         
              </apex:pageBlockSectionItem>

              <apex:pageBlockSectionItem >
                      <apex:outputlabel value="Field Names :"/>   
                      <apex:outputPanel id="myFields">   
                        <apex:actionRegion >  
                           <apex:selectList value="{!selectedField}" size="1">
                                <apex:selectOptions value="{!ObjectFields}"/>
                            </apex:selectList>
                        </apex:actionRegion>      
                     </apex:outputPanel>
             
                  
              </apex:pageBlockSectionItem>

          </apex:pageBlockSection>
      </apex:pageBlock>
  </apex:form>
</apex:page>
 
Khan AnasKhan Anas (Salesforce Developers) 
Hi Rishabh,

Greetings to you!

Below is the sample code which I have tested in my org and it is working fine. Kindly modify the code as per your requirement.

Visualforce:
<apex:page controller="AddDeleteRowC" tabStyle="Account">
    <apex:form >
        <apex:pageBlock >
            <apex:outputPanel id="showpageBlockData" >
                <apex:pageBlockSection columns="1" title="Dynamically Add or Remove Row" collapsible="false">
                    <apex:pageBlockTable value="{!lstWrapperData}" var="wd" id="pgb" >
                        <apex:column headerValue="Account Name">
                            <apex:inputField value="{!wd.objAcc.Name}" />
                        </apex:column>
                        <apex:column headerValue="Industry">
                            <apex:inputField value="{!wd.objAcc.Industry}" />
                        </apex:column>
                        <apex:column headerValue="Phone">
                            <apex:inputField value="{!wd.objAcc.Phone}" />
                        </apex:column>
                        <apex:column headerValue="Account Type">
                            <apex:inputField value="{!wd.objAcc.Type}" />
                        </apex:column>
                        <apex:column > 
                            <apex:commandLink value="Add Row" action="{!addRow}" reRender="pgb" style="color:#61afe8;" />&nbsp;&nbsp;||&nbsp;&nbsp;
                            <apex:commandLink value="Remove Row" action="{!removeRow}" reRender="pgb" style="color:red;" >
                                <apex:param value="{!wd.iIndex}" name="index" />
                            </apex:commandLink>
                        </apex:column>
                    </apex:pageBlockTable>
                </apex:pageBlockSection>           
            </apex:outputPanel> 
        </apex:pageBlock>
    </apex:form>
</apex:page>

Controller:
public class AddDeleteRowC {
    
    private Integer counter = 0;
    public list<WrapperData> lstWrapperData {get;set;}
    
    public AddDeleteRowC() {
        lstWrapperData = new list<WrapperData>();
        addRow();
    }
    
    // Adding Rows 
    public PageReference addRow() {
        counter++;
        lstWrapperData.add(new WrapperData(new Account(), counter));
        return null;
    }
    
    // Removing The Rows
    public PageReference removeRow() {
        Integer iRemoveRow = Integer.valueOf(Apexpages.currentpage().getParameters().get('index'));
        for(Integer i=0; i<lstWrapperData.size(); i++) {
            if(lstWrapperData[i].iIndex == iRemoveRow ) {
                lstWrapperData.remove(i);     
            }
        }
        return null;
    }
    
    // Wrapper Class
    public class WrapperData {
        public Account objAcc {get;set;}
        public Integer iIndex {get;set;}
        
        public WrapperData(Account objAcc, Integer iRow) {
            this.objAcc = objAcc;
            this.iIndex = iRow;
        }
    }
}

Please refer to the below links which might help you further with the above requirement.

http://sfdcsrini.blogspot.com/2014/12/adding-and-deleting-rows-dynamically-in.html

http://forcebrown.blogspot.com/2018/02/dynamically-adddelete-rows-in.html

http://forcebrown.blogspot.com/2018/02/dynamically-adddelete-rows-in.html

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas
rishabh rathor 18rishabh rathor 18
Hi Khan Anas, 
User-added image
I want use add and delete row functionality in my vf page output mean i want use this functionality in my code which is given above in my question.
thanks,
regards rishabh rathor
 
rishabh rathor 18rishabh rathor 18
Can you please give me code of this functionality.
 
rishabh rathor 18rishabh rathor 18
User-added image
Something Like That..
Rishabh Rathor