Thursday, March 20, 2014

Fixed headertable in visualforce page.

Before Implementing this create a custom object and the fields with number data type.
Create one more (Master-detail)(Account-Custom object) relationship.

Vf page:


<apex:page StandardController="Account" extensions="CustomClass" tabStyle="Account">
     <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script>
    <script src="{!URLFOR($Resource.jquery_vfFloatingHeaders)}"></script>
    <style>
        .tableContainer
        {
            height:175px;
            width: 100%;
            overflow: auto;
        }    
        .floatingStyle
        {
            position:relative;
        }
    </style>
    <script>
    $(document).ready(function() {
        $('.floatingHeaderTable').vfFloatingHeaders();
    });
    </script>
    <apex:pageblock mode="maindetail">
        <div class="bPageBlock brandSecondaryBrd apexDefaultPageBlock secondaryPalette">
        <div class="pbBody" >
        <div class="tableContainer" >
                    <table class="list floatingHeaderTable">
                        <thead class="rich-table-thead" >
                            <tr class="headerRow">
                                <apex:outputText value="No Records To Display" rendered="{!if(records.size == 0,'true','false')}"></apex:outputText>
                                <!-- Table for displaying field label -->
                                <apex:repeat rendered="{!(records.size != 0)}" value="{!fields}" var="f">
                                    <th style="text-align:{!if(f.Label = 'Product Code','left' , 'Right')};width:{!if(f.Label = 'Product Code','150px' , '')};" class="headerRow   floatingStyle" scope="col">
                                        {!f.Label}
                                    </th>
                                </apex:repeat>
                            </tr>
                        </thead>
                         <tbody>
                            <apex:repeat value="{!records}" var="item">
                                <!-- ListRow -->
                                <tr class="dataRow even first">
                                    <apex:repeat value="{!vfFields}" var="itm">
                                       <td align="{!if(itm = 'CustomObject__c(Roll up) Field', 'left', 'right')}" class=" zen-deemphasize" scope="col">                                    
                                            <apex:outputtext rendered="{!item[itm] != null && itm = 'CustomObject__c(Roll up) Field'}" value="{!item[itm]}"/>
                                            <apex:outputtext rendered="{!item[itm] != null && itm != 'CustomObject__c(Roll up) Field'}" value="{0, number,###,###,###,##0.00}">
                                                <apex:param value="{!item[itm]}"/>
                                            </apex:outputtext>
                                            <apex:outputtext rendered="{!if(itm = 'CustomObject__c(Roll up) Field' && item[itm] == null, True, False)}" value="Total" style="font-weight:900;"/>
                                       </td>
                                    </apex:repeat>
                                </tr>
                            </apex:repeat>                
                        </tbody>
                    </table>
</div>
</div>
</div>
    </apex:pageblock>
</apex:page>

Apex Controller:
//Custom Object and Account should have master-detail Relationship

public class CustomClass  {
    //Account
    public Account account {get; set;}

    //List to hold Custom Object records
    public List<AggregateResult> records{get; set;}

    //List to hold fields
    public List<Schema.FieldSetMember> fields{get; set;}

    public List<String> vfFields {get; set;}

    //Standard Controller
    public CustomClass (ApexPages.StandardController controller) {    
        //Get record
        account = (Account)controller.getRecord();
   
        //Memory allocation
        records = new List<AggregateResult>();
   
        //Method calling
        funds();
    }

    //This method is to get related fund data for Account record.
    public void funds() {
        String fieldvalue='';
        String namespaceval = '';
        Integer namespaceLength = 0;

        LIST<ApexClass> namespaceprefix=[SELECT NamespacePrefix FROM ApexClass WHERE Name = 'CustomObject__c'];
   
        if(namespaceprefix.size()>0)
        {
            namespaceval = namespaceprefix[0].NamespacePrefix + '__';
            namespaceLength = namespaceval.length();
        }
           
        //populate the list with the fields    
        fields = Schema.SObjectType.CustomObject__c.fieldSets.FieldSetName.getFields();
   
        // In visualforcepages the alias should not be more than 25 characters, So do concatenate .
        for(Schema.FieldSetMember f:fields){
            fieldvalue +=f.getFieldPath().subString(namespaceLength) + ',';
            /*
            if(f.getType() != Schema.DisplayType.String){
   
            System.debug(f.getFieldPath());
            fieldvalue +=f.getFieldPath().subString(0,f.getFieldPath().length()-3)+',';
            }else{
                fieldvalue +=f.getFieldPath()+',';
            }*/
            //System.debug(vfFields);
        }  
   
        fieldvalue = fieldvalue.removeEnd(',');
        vfFields=fieldvalue.split(',');

        records = getResultById(account.Id , fields);
    }
    public List<AggregateResult> getResultById(Id entityId , List<Schema.FieldSetMember> fields) {
   
        //String to hold soql string
        System.debug(entityId );
        String query = 'SELECT ';
        String querywhere = ' ';
        String namespaceval = ' ';
        Integer namespaceLength = 0;

        LIST<ApexClass> namespaceprefix=[SELECT NamespacePrefix FROM ApexClass WHERE Name = 'CustomObject__c'];
   
        if(namespaceprefix.size()>0)
        {
            namespaceval = namespaceprefix[0].NamespacePrefix + '__';
            namespaceLength = namespaceval.length();
        }    
   
        //loop through fields
        for(Schema.FieldSetMember f : fields) {
            if(f.getType() != Schema.DisplayType.String)
            {
                query += 'sum('+ f.getFieldPath()  + ') ' + f.getFieldPath().subString(namespaceLength)  + ', ';
                querywhere += 'or (' + f.getFieldPath() + ' != null) ';
            }
            else
            {
                query += f.getFieldPath() + ' ' + f.getFieldPath().subString(namespaceLength)  + ', ';
            }
        }
           
        query = query.removeEnd(', ');
        query += ' FROM '+ namespaceval + 'CustomObject__c WHERE Account(Master-Detail Field) =: entityId';            
 
        querywhere = querywhere.replaceFirst('or', 'and (');
        query += querywhere + ')';
        query += ' group by rollup ('+ namespaceval + 'CustomField__c)';
   
        List<AggregateResult> lstfundSummary = Database.query(query);
        System.debug('\n lstfundSummary'+lstfundSummary);
        return lstfundSummary;
    }
}





No comments:

Post a Comment