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

Labels

visualforce page ( 13 ) apex integration ( 5 ) apex trigger ( 4 ) csv file from vf page ( 4 ) javascript ( 4 ) csv visualforce page ( 3 ) Too many ( 2 ) call out ( 2 ) integration ( 2 ) rest api ( 2 ) salesforce rest api ( 2 ) salesforce to salesforce integration ( 2 ) sfdc rest api ( 2 ) trigger ( 2 ) 15 digit to 18 digit ( 1 ) DML rows in Apex ( 1 ) Date Conversion ( 1 ) Date/Time conversion ( 1 ) Deploy ( 1 ) Objects to Future Annotated Methods ( 1 ) SFDC limits ( 1 ) Sobject to Future Annotated Methods ( 1 ) Test Class ( 1 ) TimeZone Conversion ( 1 ) Too many dml rows ( 1 ) Too many future calls ( 1 ) annotations ( 1 ) apex code ( 1 ) closed opportunities ( 1 ) commit ( 1 ) convert ( 1 ) create records ( 1 ) csv create records ( 1 ) custom setting ( 1 ) deployment ( 1 ) deployment changeset ( 1 ) disable apex class ( 1 ) disable apex trigger ( 1 ) disable in production ( 1 ) document ( 1 ) download ( 1 ) field name ( 1 ) formula fields ( 1 ) iframe ( 1 ) inactive ( 1 ) intellisense ( 1 ) jsforce ( 1 ) limits ( 1 ) matrix report in vf page ( 1 ) multi select ( 1 ) multi select salesforce ( 1 ) multiselect ( 1 ) paypal ( 1 ) picklist ( 1 ) record type ( 1 ) rollback ( 1 ) salesforce limits ( 1 ) salesforce list ( 1 ) salesforce map ( 1 ) salesforce rest ( 1 ) salesforce set ( 1 ) salesforce1 ( 1 ) sandbox deployment ( 1 ) sfdc collection ( 1 ) sfdc list ( 1 ) sfdc map ( 1 ) sfdc rest ( 1 ) sfdc set ( 1 ) uncommitted ( 1 ) updated field ( 1 ) user ( 1 ) validation rule opportunity ( 1 ) validation rules opportunities ( 1 ) vf page ( 1 )

Ad