Saturday, January 11, 2014

Dynamiclly Adding Rows and Deleting Rows and save into Model

===========VFPage===========================
<apex:page sidebar="false" controller="AddNDeleteRowsDynamically">
 <style>
  .headerRow .TableTitle {
color: DarkBlue!important;
font-size:12pt!important;
font-family:timesnewroman;

}
  .headerRow .TableTitle1 {
color: Chocolate !important;
}
.headerRow .TableTitle2 {
color:Fuchsia !important;
}
</style>


<apex:sectionHeader title="AddNDeleteRowsDynamically"/>
 <apex:form >
   <apex:pageBlock id="pb">
      <apex:pageBlockTable value="{!AddRowlist}" var="A">
         <apex:column headerValue="Name" headerClass="TableTitle">
           <apex:InputField value="{!A.name}"/>
         </apex:column>
         <apex:column headerValue="Text" headerClass="TableTitle1 ">
            <apex:InputField value="{!A.ahs__c}"/>
         </apex:column>
         <apex:column headerValue="Text" headerClass="TableTitle1 ">
            <apex:inputCheckbox value="{!CheckBox}">
            <apex:actionSupport event="onclick" action="{!onclickaction}" />
         </apex:inputcheckbox>
         </apex:column>
         <apex:column headerValue="NNR" headerClass="TableTitle1 ">
            <apex:InputText value="{!A.nnr1__c}" disabled="{!disabled}" />
           
         </apex:column>  
         <apex:column headerValue="Number" headerClass="TableTitle2">
           <apex:inputtext value="{!A.number__c}" disabled="{!disabled}"/>
         </apex:column>
      </apex:pageBlockTable>
      <apex:pageBlockSection >
        <apex:pageBlockSectionItem >
          <apex:commandLink value="Add Row" action="{!AddRow}" reRender="pb"/>
          <apex:commandLink value="Remove Row" action="{!RemoveRow}" reRender="pb"/>
        </apex:pageBlockSectionItem>
     </apex:pageBlockSection>
     <apex:pageBlockButtons >
       <apex:commandButton value="Save" action="{!Save}" reRender="pb"/>
       <apex:commandButton value="Cancel" action="{!Cancel}" reRender="pb"/>
     </apex:pageBlockButtons>
   </apex:pageBlock>
 </apex:form>
</apex:page>
===============Controller===================
public with sharing class AddNDeleteRowsDynamically {
    public boolean checkbox{set;get;}
    public PageReference onclickaction() {
        if(checkbox== true){
            disabled= false;
        }
        else{
            disabled= true;
        }
        return null;
       // return null;
    }


    public Boolean disabled { get; set; }

    public PageReference Cancel() {
        return null;
    }


    public PageReference Save() {
        insert addrowlist;
        addrowlist.clear();
        return null;
    }


    public void RemoveRow() {
        AddRowList.remove(0);
        //return null;
    }


    public void AddRow() {
        Addrowlist.add(new nnr__c());
         if(checkbox== true){
            disabled= false;
        }
        else{
            disabled= true;
        }
     
       
        //return null;
    }

    public AddNDeleteRowsDynamically(){
       string sql='select id,name,ahs__c,nnr1__c,number__c from nnr__c';
       addrows=Database.query(sql);
       AddRowlist=new list<nnr__c>();
       AddRowlist.add(new nnr__C());
       disabled=false;
    }
    public List<nnr__c> AddRows { get; set; }
    public List<nnr__c> AddRowlist { get; set; }
}

No comments:

Post a Comment