Skip to main content

How to Add jQuery DataTables to Visualforce: Pagination, Search & Sorting Guide

Displaying large datasets in Salesforce Visualforce pages using standard tables can quickly lead to slow page loads, messy interfaces, and limited user controls. Integrating the lightweight jQuery DataTables plugin turns standard HTML tables into dynamic data grids complete with instant search, multi-column sorting, and responsive pagination.

In plain words: jQuery DataTables is a drop-in JavaScript library that enhances basic HTML tables with out-of-the-box client-side pagination, instant keyword filtering, and column sorting without reloading the page.

1. Apex Controller Implementation

Start by creating a simple Apex controller to query the records you want to render in Visualforce. In this example, we retrieve a list of Contact records:

public with sharing class DataTableController {
    public List<Contact> contacts { get; set; }

    public DataTableController() {
        contacts = [
            SELECT Id, Name, Email, Title, Department 
            FROM Contact 
            WITH USER_MODE 
            LIMIT 1000
        ];
    }
}

2. Visualforce Page Markup & JavaScript Setup

Create a Visualforce page named DataTablePage. Load the required jQuery and DataTables stylesheets and scripts, structure the HTML table inside standard semantic tags (<thead> and <tbody>), and initialize the plugin with safe variable scoping:

<apex:page controller="DataTableController" docType="html-5.0">
    <!-- Include jQuery and DataTables CSS/JS -->
    <apex:includeScript value="https://code.jquery.com/jquery-3.7.1.min.js"/>
    <apex:includeScript value="https://cdn.datatables.net/1.13.7/js/jquery.dataTables.min.js"/>
    <apex:stylesheet value="https://cdn.datatables.net/1.13.7/css/jquery.dataTables.min.css"/>

    <script>
        // Avoid namespace collisions with Salesforce JS libraries
        const $j = jQuery.noConflict();

        $j(document).ready(function() {
            $j('#contactTable').DataTable({
                pageLength: 10,
                lengthMenu: [5, 10, 25, 50],
                ordering: true,
                searching: true
            });
        });
    </script>

    <apex:pageBlock title="Contact Directory">
        <table id="contactTable" class="display" style="width:100%">
            <thead>
                <tr>
                    <th>Name</th>
                    <th>Email</th>
                    <th>Title</th>
                    <th>Department</th>
                </tr>
            </thead>
            <tbody>
                <apex:repeat value="{!contacts}" var="con">
                    <tr>
                        <td>{!con.Name}</td>
                        <td>{!con.Email}</td>
                        <td>{!con.Title}</td>
                        <td>{!con.Department}</td>
                    </tr>
                </apex:repeat>
            </tbody>
        </table>
    </apex:pageBlock>
</apex:page>
Step-by-Step Implementation Flow:
  • Step 1: Query records safely using Apex and bind the collection to your page.
  • Step 2: Load the DataTables CDN or static resource bundles in your Visualforce markup.
  • Step 3: Build a standard HTML table utilizing explicit <thead> and <tbody> elements (DataTables requires both).
  • Step 4: Invoke jQuery.noConflict() and call .DataTable() once the DOM finishes loading.

3. Critical Traps & Platform Limits

Developer Trap: Client-side DataTables renders all records into the browser DOM at once. Loading collections larger than 1,000–2,000 records can trigger Visualforce view state limits (135 KB) and cause browser lag. For larger record sets, use Server-Side Processing via Remote Actions or modernize to Lightning Web Components (lightning-datatable).

4. Core Architecture Overview

Data Grid Comparison:
  • Visualforce + DataTables: Great for legacy enhancements where fast client-side searching and minimal code refactoring are needed.
  • Apex StandardSetController: Native Salesforce server-side pagination without external third-party JavaScript dependencies.
  • Lightning Datatable (LWC): The modern Salesforce standard offering native Lightning Design System (SLDS) styling, inline editing, and infinite scrolling.
Core Rule: Always wrap your scripts with jQuery.noConflict() and ensure strict <thead> markup to prevent script execution crashes in Visualforce.