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
mohit Srivastavamohit Srivastava 

Visual force add row

hi guyzz,
 i want to implement a functionality in whichi am showing a contact list via outputfield and i have a button add row . the problem is that when i am adding a row it gets added in vf page but as a outputfield i want a input field instead of output field when i click a add row on a same pageblock.. is it possible.

Thanks in advance
Best Answer chosen by mohit Srivastava
Venkat HVenkat H
Hi Mohit Srivastava,

When you click addRow Instead of outputField inputField is added.
Please refer the below code.

<apex:pageblock id="pb">
       <apex:pageblockTable value="{!accList}" var="acc">
           <apex:column >
               <apex:outputField value="{!acc.Name}" rendered="{!!state}"/>
               <apex:inputField value="{!acc.Name}" rendered="{!state}"/>
           </apex:column>
       </apex:pageblockTable>
       <apex:pageBlockButtons >
           <apex:commandButton action="{!addRow}" value="Add Row" reRender="pb"/>
           <apex:commandButton action="{!sveAcc}" value="Save" reRender="pb"/>
       </apex:pageBlockButtons>
   </apex:pageblock>  

public classExample(){
public List<account> accList{get;set;}
  public Boolean state{get;set;}
public Example()
  {    
      state=false;
      accList=new List<account>();
      accList=[select id,Name from account];
 }
 public void addRow(){
      state=true;
      accList.add(New Account());
  }
  public void sveAcc(){
      state=false;
      upsert accList;
  }
}

All Answers

Venkat HVenkat H
Hi Mohit Srivastava,

When you click addRow Instead of outputField inputField is added.
Please refer the below code.

<apex:pageblock id="pb">
       <apex:pageblockTable value="{!accList}" var="acc">
           <apex:column >
               <apex:outputField value="{!acc.Name}" rendered="{!!state}"/>
               <apex:inputField value="{!acc.Name}" rendered="{!state}"/>
           </apex:column>
       </apex:pageblockTable>
       <apex:pageBlockButtons >
           <apex:commandButton action="{!addRow}" value="Add Row" reRender="pb"/>
           <apex:commandButton action="{!sveAcc}" value="Save" reRender="pb"/>
       </apex:pageBlockButtons>
   </apex:pageblock>  

public classExample(){
public List<account> accList{get;set;}
  public Boolean state{get;set;}
public Example()
  {    
      state=false;
      accList=new List<account>();
      accList=[select id,Name from account];
 }
 public void addRow(){
      state=true;
      accList.add(New Account());
  }
  public void sveAcc(){
      state=false;
      upsert accList;
  }
}
This was selected as the best answer
mohit Srivastavamohit Srivastava
hi venkat, thanks buddy the code is working fine .. :)