Tuesday, February 18, 2014


Adding Rows to Dynamically

Vf page
<apex:page controller="DoctorSearchPage2" tabStyle="Doctor__c" showHeader="false">
    <script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js"></script>
        <span id="hideThisHomePageComp"></span> 
        <script type="text/javascript">
            $j = jQuery.noConflict();
            $j(document).ready(function() {
                var startYear=1900;
                var endYear=2024;
                var htmlStr='';
                    if(startYear<endYear){
                        for(i=startYear;i<endYear+1;i++){
                            htmlStr += "<option value=\""+i+"\">"+i+"</option>";
                        }
                        $j('#calYearPicker').html(htmlStr);
                    }
                $j('#sidebarDiv #hideThisHomePageComp').parent().parent().hide();
               
            });
        </script>   
        <script>
            var newWin=null;
            function openLookupPopup(name, id, fees){
                var url="/apex/DoctorName?namefield=" + name + "&idfield=" + id + "&feefield=" +fees;
                newWin=window.open(url, 'Popup','height=500,width=600,left=100,top=100,resizable=no,scrollbars=yes,toolbar=no,status=no');
                if (window.focus){
                    newWin.focus();
                }
                return false;
            }
                 
            function closeLookupPopup(){
               if (null!=newWin){
                   newWin.close();
               } 
            }
    </script>
   
  
    <apex:form id="frm">
       
        <apex:pageBlock title="Search Patient" id="pg">
            <apex:pagemessages ></apex:pagemessages>
            <apex:inputText value="{!ipd_reg_no}"/>
            <apex:commandButton value="Search" reRender="op" action="{!addPatient}"/><br/> <br/>
            <apex:outputPanel id="op">
                <apex:outputText rendered="{!op}" ><font size="3" color="Blue">Hi..! you search for {!na}.</font></apex:outputText>          
            </apex:outputPanel>
        </apex:pageBlock>
       
        <apex:pageBlock title="Add Doctor" id="pageblock">
            <apex:inputhidden value="{!testCount}"/><br/>
            <apex:repeat value="{!lstInner}" var="e1" id="therepeat" >
               
                <apex:commandButton value="Remove Doctor" action="{!Del}" rerender="pageblock,total_amount">
                    <apex:param name="rowToBeDeleted" value="{!e1.recCount}" assignTo="{!selectedRowIndex}" />
                </apex:commandButton>
               
                <apex:inputField id="targetName" value="{!e1.acct.Name}">
                    <apex:actionsupport event="onkeydown" action="{!removeName}" rerender="targetId,targetFees,targetVisitCount,targetVisitFees,total_amount"/>
                </apex:inputField>
                   
                <apex:inputhidden value="{!e1.acct.Name}" id="targetId" />
               
                <a href="" onclick="openLookupPopup('{!$Component.targetName}','{!$Component.targetId}','{!$Component.targetFees}'); return false">
                    <apex:commandButton value="Select" />
                </a>
                <apex:inputField id="targetFees"  value="{!e1.acct.Doctors_Fees__c}"/>
               
                <apex:inputField id="targetVisitCount"  value="{!e1.acctt.Doctor_Visit_Count__c}" >
                    <apex:actionSupport action="{!calcFees}" reRender="targetVisitFees,total_amount1" event="onkeyup"/>                                   
                </apex:inputField>
               
                <apex:inputField id="targetVisitFees"  value="{!e1.acctt.Total_Doctor_Fees__c}"/>    <!--  onchange="calcFee();"-->
                               
                <br/><br/>
            </apex:repeat>
            <br/><br/><apex:commandLink value="Add Doctor" action="{!Add}" reRender="pageblock"/> <br/>
            <br/><apex:commandButton value="Save" action="{!mySave}"/><br/><br/>
            <apex:pageBlockSection id="total_amount1">
                <apex:outputText id="total_amount">
                    <font size="5" color="Red">Rs. {!total_amt}</font>
                </apex:outputText>
            </apex:pageBlockSection>          
        </apex:pageBlock>
    </apex:form>
</apex:page>

Controller
public with sharing class DoctorSearchPage2 {

    public boolean op { get; set; }
    public String na { get; set; }
    IPD_Registration__c ipd_reg_obj;

    public decimal total_amt { get; set; }
    public String testCount { get; set; }
    public doctor__c doc{set;get;}
    public List<innerClass> lstInner{get;set;}            //list of the inner class
    public String selectedRowIndex{get;set;}             //will indicate the row to be deleted
    public Integer count = 1;                        //no. of rows added/records in the inner class list
    public String Docname {set;get;}
    public String ipd_reg_no { get; set; }
    decimal d = 0;
   
   
    public DoctorSearchPage2(){
        lstInner = new List<innerClass>();
        addMore();
        selectedRowIndex = '0';
        doc = new doctor__c();
        total_amt = 0;  
        op = false;    
    }
    
    public class innerClass{      
        public String recCount{get;set;}
        public doctor__c acct{get;set;}
        public IPD_Service__c acctt {get;set;}
       
        public innerClass(Integer intCount){
            recCount = String.valueOf(intCount);       
            acct = new doctor__c();
            acctt = new IPD_Service__c();
            acctt.Total_Doctor_Fees__c = 0;
            acctt.Doctor_Visit_Count__c = 0;
        }
    }
   
    public void Add(){  
        system.debug('add :::::::::::::::::::: '+lstInner);
        count = count+1;
        addMore();       
        system.debug('add :::::::::::::::::::: '+lstInner);
    }
   
    public void Del(){
        d = total_amt;
        for(innerClass c : lstInner){
            if(c.reccount == selectedRowIndex){
                d = d - c.acctt.Total_Doctor_Fees__c;
            }           
        }
        total_amt = d; 
        d = 0;     
        if(lstInner != null && selectedRowIndex != null && lstInner[integer.valueof(selectedRowIndex)-1] != null && lstInner[integer.valueof(selectedRowIndex)-1].acct != null && lstInner[integer.valueof(selectedRowIndex)-1].acct.name != null||lstInner[integer.valueof(selectedRowIndex)-1].acct.name == null){
            lstInner.remove(Integer.valueOf(selectedRowIndex)-1);
            count = count-1;
        }                       
    }
   
    public void addMore(){
        innerClass objInnerClass = new innerClass(count);
        lstInner.add(objInnerClass);       
    }
       
    public PageReference calcFees() {
        for(innerClass c : lstInner){
            c.acctt.Total_Doctor_Fees__c = c.acctt.Total_Doctor_Fees__c + c.acct.Doctors_Fees__c * c.acctt.Doctor_Visit_Count__c;
            d = c.acctt.Total_Doctor_Fees__c;
        }
        total_amt = total_amt  + d;
        d = 0;
        return null;
    }
   
    public PageReference removeName() {
        return null;
    }
   
    public PageReference addPatient() {
        if(ipd_reg_no != null){
            try{
                ipd_reg_obj = [select id,name,Permanent_Reg_No__c,Patient_Name__c,Status__c  from IPD_Registration__c where name =: 'IPD--'+ipd_reg_no and Status__c =: 'Admitted'];
                if(ipd_reg_obj != null){
                    ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Info,'data is :: '+ipd_reg_obj+' only'));
                    na = ipd_reg_obj.Patient_Name__c;
                    op = true;
                }else{
                    ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error, 'Data is not there.'));
                }
            }catch(QueryException e){
                ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error, 'Data Can not be fetch.'));
            }
        }else{
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error, 'Please give the values here.'));
        }
       
       
        return null;
    }
   
    public PageReference mySave() {
        for(innerClass c : lstInner){
            IPD_Service__c ipd_serv = new IPD_Service__c();
            ipd_serv.Service_Name__c = c.acct.name;
            ipd_serv.Service_Charge__c = c.acct.Doctors_Fees__c;
            //ipd_serv.IPD_Registration__c = c.acctt.Doctor_Visit_Count__c;
            ipd_serv.Doctor_Visit_Count__c = c.acctt.Doctor_Visit_Count__c;
            ipd_serv.Total_Doctor_Fees__c = c.acctt.Doctor_Visit_Count__c * c.acct.Doctors_Fees__c;
            try{
                insert ipd_serv;
            }catch(DMLException e){
           
            }
        }       
        return null;
    }
    
}

No comments:

Post a Comment