Dynamic DataTables with jQuery and Visualforce
Create sortable, filterable, searchable, and paginated data tables using jQuery, DataTables jQuery plugin, and Visualforce.
Very excited to share this with the world! This post will get updated soon with step by step details, for those devs that want to figure it out for themselves, source code is right here on this page with a link to a live demo on my developer edition org site as well as a link to download the static resource used in the example. Enjoy!
Live Demo on my Salesforce.com Developer Site
Download the Static Resource Used in this Example
Source Code
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
public class DataTableExampleController { public ApexPages.StandardSetController contactSet { get { if(contactSet == null) { contactSet = new ApexPages.StandardSetController(Database.getQueryLocator( [SELECT Name, Phone, Email, Title, Account.Name FROM Contact])); } return contactSet; } set; } public List<contact> getContacts() { return (List<contact>) contactSet.getRecords(); } } |
Important! Make sure your SOQL query includes all the fields you need for the datatable.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
<apex:page Controller="DataTableExampleController" docType="html-5.0"> <head> <apex:includescript value="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.js"/> <style type="text/css" title="currentStyle"> @import "{!URLFOR($Resource.DataTables, 'media/css/demo_page.css')}"; @import "{!URLFOR($Resource.DataTables, 'media/css/demo_table.css')}"; @import "{!URLFOR($Resource.DataTables, 'media/css/ColReorder.css')}"; @import "{!URLFOR($Resource.DataTables, 'media/css/ColVis.css')}"; @import "{!URLFOR($Resource.DataTables, 'media/css/TableTools.css')}"; @import "{!URLFOR($Resource.DataTables, 'media/css/ColumnFilterWidgets.css')}"; @import "{!URLFOR($Resource.DataTables, 'media/css/demo_table_jui.css')}"; @import "{!URLFOR($Resource.DataTables, 'media/examples_support/themes/smoothness/jquery-ui-1.8.4.custom.css')}"; </style> <script src="{!URLFOR($Resource.DataTables, 'media/js/jquery.dataTables.min.js')}"></script> <script src="{!URLFOR($Resource.DataTables, 'media/js/jquery.dataTables.js')}"></script> <script src="{!URLFOR($Resource.DataTables, 'media/js/ColVis.js')}"></script> <script src="{!URLFOR($Resource.DataTables, 'media/js/ZeroClipboard.js')}"></script> <script src="{!URLFOR($Resource.DataTables, 'media/js/TableTools.js')}"></script> <script src="{!URLFOR($Resource.DataTables, 'media/js/ColumnFilterWidgets.js')}"></script> <script type="text/javascript" charset="UTF-8"> $(document).ready( function () { var oTable = $('#contacttable').dataTable( { "sDom": 'WRC<"clear">lftip', "bJQueryUI": true, "sPaginationType": "full_numbers", "aoColumnDefs": [ { "bVisible": false, "aTargets": [ ] }], "oColumnFilterWidgets": { "aiExclude": [ 0, 3, 4 ] } }); }); </script> </head> <body> <h2>Contact Search Demo with jQuery, DataTables, and Visualforce"</h2> <h3>Try out the keyword search, show/hide columns, sort, and pagination!</h3> <table cellpadding="0" cellspacing="0" border="0" class="display" id="contacttable" style="margin-top:20px;"> <thead> <tr> <th>Name</th> <th>Account</th> <th>Title</th> <th>Phone</th> <th>Email</th> </tr> </thead> <tfoot> <tr> <th>Name</th> <th>Account</th> <th>Title</th> <th>Phone</th> <th>Email</th> </tr> </tfoot> <tbody> <apex:repeat value="{!Contacts}" var="c"> <tr> <td>{!c.Name}</td> <td>{!c.Account.Name}</td> <td>{!c.Title}</td> <td>{!c.Phone}</td> <td>{!c.Email}</td> </tr> </apex:repeat> </tbody> </table> </body> </apex:page> |
-
Sandeep Das
-
http://macscloud.com/ Mac Anderson
-
andrew
-
http://macscloud.com/ Mac Anderson
-
http://twitter.com/reetan_adc Reetan
-
Sunny Sharma
-
ram
-
Klaus Schmetkamp
-
Klaus Schmetkamp
-
Kotir1
-
Klaus
Mac Anderson

Salesforce.com Technical Architect, with experience developing custom applications on the Force.com platform as well as large enterprise implementations of both Sales and Service Cloud (CRM).






