• WHO WE ARE
  • WHAT WE DO
    • Implementations
      • Salesforce
        • Sales Cloud
        • Service Cloud
        • CPQ
        • Field Service for SMEs
        • Field Service Lightning
      • Salesforce Partner Apps
        • Panda Doc
        • Conga
    • Developments
      • Salesforce Customization
      • Custom Application Development
      • AppExchange Product Development
    • Migrations
      • Classic to Lightning Migration
      • Other Systems to Salesforce Migration
    • Integrations
  • HOW WE DO
    • Delivery Model
    • Our Works
  • REACH US
    • Contact Us
    • Careers
  • BLOG
    • WHO WE ARE
    • WHAT WE DO
      • Implementations
        • Salesforce
          • Sales Cloud
          • Service Cloud
          • CPQ
          • Field Service for SMEs
          • Field Service Lightning
        • Salesforce Partner Apps
          • Panda Doc
          • Conga
      • Developments
        • Salesforce Customization
        • Custom Application Development
        • AppExchange Product Development
      • Migrations
        • Classic to Lightning Migration
        • Other Systems to Salesforce Migration
      • Integrations
    • HOW WE DO
      • Delivery Model
      • Our Works
    • REACH US
      • Contact Us
      • Careers
    • BLOG
  • [email protected]
  • (+91) 44-49521562
Merfantz - Salesforce Solutions for SMEs
Merfantz - Salesforce Solutions for SMEs
  • WHO WE ARE
  • WHAT WE DO
    • Implementations
      • Salesforce
        • Sales Cloud
        • Service Cloud
        • CPQ
        • Field Service for SMEs
        • Field Service Lightning
      • Salesforce Partner Apps
        • Panda Doc
        • Conga
    • Developments
      • Salesforce Customization
      • Custom Application Development
      • AppExchange Product Development
    • Migrations
      • Classic to Lightning Migration
      • Other Systems to Salesforce Migration
    • Integrations
  • HOW WE DO
    • Delivery Model
    • Our Works
  • REACH US
    • Contact Us
    • Careers
  • BLOG

How to Freeze Column and Header using Visualforce Page

  • September 15, 2023
  • Tech Blogger
  • Merfantz Developments, Salesforce Developments Tutorial
  • 0

Description:

Create a custom Visual force page that displays data in a table with several columns. It would be beneficial for users to have the ability to “freeze” or “lock” specific columns while scrolling through the data, ensuring that important information remains visible as they navigate the table.

Source Code with Controller:

FreezeColumn.vfp

<apex:page sidebar=”true” Controller=”AccountDetailsController”>

<apex:includeScript value=”https://code.jquery.com/jquery-1.11.1.min.js”/>

<apex:includeScript value=”https://cdn.datatables.net/1.10.7/js/jquery.dataTables.min.js”/>

<apex:includeScript value=”https://www.datatables.net/release-datatables/extensions/FixedColumns/js/dataTables.fixedColumns.js”/>

<apex:stylesheet value=”https://cdn.datatables.net/1.10.7/css/jquery.dataTables.css”/>

<apex:stylesheet value=”https://www.datatables.net/release-datatables/extensions/FixedColumns/css/dataTables.fixedColumns.css”/>

<html>

<body>

<br/><br/>

<apex:outputText >Freeze Columns Starting From: </apex:outputText>

<select onchange=”freezeColChange();” id=”freezeColSel”>

<option value=”0″ selected=”selected”>None</option>

<option value=”1″ >Account Id</option>

<option value=”2″ >Account Name</option>

<option value=”3″ >Number of Employees</option>

<option value=”4″ >Website</option>

<option value=”5″ >Annual Revenue</option>

</select>

<br/>

<table id=”accTable” class=”display” cellspacing=”0″ width=”100%”>

<thead style=”background-color:green;color:white;”>

<th>Account Id</th>

<th>Account Name</th>

<th>Number of Employees</th>

<th>Website</th>

<th>Phone</th>

<th>Annual Revenue</th>

<th>Industry</th>

<th>Type</th>

<th>SLA</th>

<th>Number of Locations</th>

</thead>

<tbody>

<apex:repeat value=”{!accounts}” var=”acc”>

<tr>

<td>{!acc.Id}</td>

<td>{!acc.Name}</td>

<td>{!acc.NumberOfEmployees}</td>

<td>{!acc.Website}</td>

<td>{!acc.Phone}</td>

<td>{!acc.AnnualRevenue}</td>

<td>{!acc.Industry}</td>

<td>{!acc.Type}</td>

<td>{!acc.SLA__c}</td>

<td>{!acc.NumberofLocations__c}</td>

</tr>

</apex:repeat>

</tbody>

</table>

</body>

</html>

<script type=”text/javascript”>

var table;

$(document).ready( initializeDataTable());

function initializeDataTable(){

var freezeColIdx =  $( “#freezeColSel” ).val();

table = $(‘#accTable’).DataTable( {

scrollY:        “300px”,

scrollX:        true,

scrollCollapse: true,

paging:         false

} );

new $.fn.dataTable.FixedColumns( table, {

leftColumns: freezeColIdx

} );

}

function freezeColChange(){

table.destroy();

initializeDataTable();

}

</script>

<style>

th, td { white-space: nowrap; }

div.dataTables_wrapper {

width: 900px;

margin: 0 auto;

}

</style>

</apex:page>

Using Controller to fetch records:

AccountDetailsController.apxc:

public with sharing class AccountDetailsController

{

public List<Account> accounts { get; set; }

public AccountDetailsController()

{

// Query for Account records and store them in the ‘accounts’ variable

accounts = [SELECT Name,Type,Industry,Website,Phone,SLA__c,NumberofLocations__c, Description, AnnualRevenue, NumberOfEmployees, Rating, Site, CreatedDate, LastModifiedDate FROM Account];

}

}

Freeze Column Final Output:

Final Output

Code Explanation:

FreezeColumn.vfp

<apex:page sidebar=”true” Controller=”AccountDetailsController”>

<!–Include a table plug-in for jquery and css–>

<html>

<body>

<!–Include table and your custom code–>

</body>

</html>

</apex:page>

Add table to display account details:

To Display account details like Account’s Id , Name , Phone No , Industry , Annual Revenue, etc. You can also use this for standard(Contact, Opportunity…) and custom object based on your requirement.

<table id=”accTable” class=”display” cellspacing=”0″ width=”100%”>

<thead style=”background-color:green;color:white;”>

<th>Account Id</th>

<th>Account Name</th>

<th>Number of Employees</th>

<th>Website</th>

<th>Phone</th>

<th>Annual Revenue</th>

<th>Industry</th>

<th>Type</th>

<th>SLA</th>

<th>Number of Locations</th>

</thead>

<tbody>

<apex:repeat value=”{!accounts}” var=”acc”>

<tr>

<td>{!acc.Id}</td>

<td>{!acc.Name}</td>

<td>{!acc.NumberOfEmployees}</td>

<td>{!acc.Website}</td>

<td>{!acc.Phone}</td>

<td>{!acc.AnnualRevenue}</td>

<td>{!acc.Industry}</td>

<td>{!acc.Type}</td>

<td>{!acc.SLA__c}</td>

<td>{!acc.NumberofLocations__c}</td>

</tr>

</apex:repeat>

</tbody>

</table>

Table with Account Details

Using Controller to fetch records:

AccountDetailsController.apxc:

public with sharing class AccountDetailsController

{

public List<Account> accounts { get; set; }

public AccountDetailsController()

{

// Query for Account records and store them in the ‘accounts’ variable

accounts = [SELECT Name,Type,Industry,Website,Phone,SLA__c,NumberofLocations__c, Description, AnnualRevenue, NumberOfEmployees, Rating, Site, CreatedDate, LastModifiedDate FROM Account];

}

}

Add Libraries for Data table and CSS:

I’ve included all the required libraries and CSS files in FreezeColumn.vfp. I recommend saving and managing these files as Static Resources and using this method for referencing them in your project.

  1. <apex:includeScript value=”https://code.jquery.com/jquery-1.11.1.min.js”/>
  2. <apex:includeScript value=”https://cdn.datatables.net/1.10.7/js/jquery.dataTables.min.js”/>
  3. <apex:includeScript value=”https://www.datatables.net/release-datatables/extensions/FixedColumns/js/dataTables.fixedColumns.js”/>
  4. <apex:stylesheet value=”https://cdn.datatables.net/1.10.7/css/jquery.dataTables.css”/>
  5. <apex:stylesheet value=”https://www.datatables.net/release-datatables/extensions/FixedColumns/css/dataTables.fixedColumns.css”/>

<script type=”text/javascript”>

var table;

$(document).ready( initializeDataTable());

function initializeDataTable(){

var freezeColIdx =  $( “#freezeColSel” ).val();

table = $(‘#accTable’).DataTable( {

scrollY:        “300px”,

scrollX:        true,

scrollCollapse: true,

paging:         false

} );

new $.fn.dataTable.FixedColumns( table, {

leftColumns: freezeColIdx

} );

}

</script>

Data-Table

Include script and style:

    <script type=”text/javascript”>

function freezeColChange(){

table.destroy();

initializeDataTable();

}

</script>

<style>

th, td { white-space: nowrap; }

div.dataTables_wrapper {

width: 900px;

margin: 0 auto;

}

</style>

<apex:outputText >Freeze Columns Starting From: </apex:outputText>

<select onchange=”freezeColChange();” id=”freezeColSel”>

<option value=”0″ selected=”selected”>None</option>

<option value=”1″ >Account Id</option>

<option value=”2″ >Account Name</option>

<option value=”3″ >Number of Employees</option>

<option value=”4″ >Website</option>

<option value=”5″ >Annual Revenue</option>

</select>

Conclusion:

              By implementing this Freeze Column functionality, you empower users to have more control over how they interact with the data, making it easier for them to analyze, compare, and extract valuable insights. It not only enhances the usability of your application but also contributes to a more efficient and productive workflow.

Related Links:

https://merfantz.com/blog/dynamically-remove-table-column-based-on-header-name/

Tags: Salesforce Lightning Development
  • Previous How to Clone Record Automatically Using Batch Apex
  • Next Effortless Salesforce Migration Services | Merfantz Technologies
Merfantz Technologies is a leading Salesforce consulting firm dedicated to helping small and medium enterprises transform their operations and achieve their goals through the use of the Salesforce platform. Contact us today to learn more about our services and how we can help your business thrive.

Discover More

Contact Info

  • No 96, 2nd Floor, Greeta Tech Park, VSI Industrial Estate, Perungudi, Chennai 600 096, Tamil Nadu, INDIA
  • (+91) 44-49521562
  • [email protected]
  • 9:30 IST - 18:30 IST

Latest Posts

How to Send a Custom Notification using Flow September 29, 2023
Unlocking Marketing Automation Excellence with Salesforce Pardot | Merfantz September 24, 2023
Looking for the Leading Salesforce Integration Companies? Discover Merfantz September 24, 2023

Copyright @2023 Merfantz Technologies, All rights reserved