DataTables example Footer callback

Through the use of the header and footer callback manipulation functions provided by DataTables (headerCallback and footerCallback), it is possible to perform some powerful and useful data manipulation functions, such as summarising data in the table.

The example below shows a footer callback being used to total the data for a column (both the visible and the hidden data) using the column().data() API method and column().footer() for writing the value into the footer.

First nameLast namePositionOfficeSalary
Total:
TigerNixonSystem ArchitectEdinburgh$320,800
GarrettWintersAccountantTokyo$170,750
AshtonCoxJunior Technical AuthorSan Francisco$86,000
CedricKellySenior Javascript DeveloperEdinburgh$433,060
AiriSatouAccountantTokyo$162,700
BrielleWilliamsonIntegration SpecialistNew York$372,000
HerrodChandlerSales AssistantSan Francisco$137,500
RhonaDavidsonIntegration SpecialistTokyo$327,900
ColleenHurstJavascript DeveloperSan Francisco$205,500
SonyaFrostSoftware EngineerEdinburgh$103,600
JenaGainesOffice ManagerLondon$90,560
QuinnFlynnSupport LeadEdinburgh$342,000
ChardeMarshallRegional DirectorSan Francisco$470,600
HaleyKennedySenior Marketing DesignerLondon$313,500
TatyanaFitzpatrickRegional DirectorLondon$385,750
MichaelSilvaMarketing DesignerLondon$198,500
PaulByrdChief Financial Officer (CFO)New York$725,000
GloriaLittleSystems AdministratorNew York$237,500
BradleyGreerSoftware EngineerLondon$132,000
DaiRiosPersonnel LeadEdinburgh$217,500
JenetteCaldwellDevelopment LeadNew York$345,000
YuriBerryChief Marketing Officer (CMO)New York$675,000
CaesarVancePre-Sales SupportNew York$106,450
DorisWilderSales AssistantSidney$85,600
AngelicaRamosChief Executive Officer (CEO)London$1,200,000
GavinJoyceDeveloperEdinburgh$92,575
JenniferChangRegional DirectorSingapore$357,650
BrendenWagnerSoftware EngineerSan Francisco$206,850
FionaGreenChief Operating Officer (COO)San Francisco$850,000
ShouItouRegional MarketingTokyo$163,000
MichelleHouseIntegration SpecialistSidney$95,400
SukiBurksDeveloperLondon$114,500
PrescottBartlettTechnical AuthorLondon$145,000
GavinCortezTeam LeaderSan Francisco$235,500
MartenaMccrayPost-Sales supportEdinburgh$324,050
UnityButlerMarketing DesignerSan Francisco$85,675
HowardHatfieldOffice ManagerSan Francisco$164,500
HopeFuentesSecretarySan Francisco$109,850
VivianHarrellFinancial ControllerSan Francisco$452,500
TimothyMooneyOffice ManagerLondon$136,200
JacksonBradshawDirectorNew York$645,750
OliviaLiangSupport EngineerSingapore$234,500
BrunoNashSoftware EngineerLondon$163,500
SakuraYamamotoSupport EngineerTokyo$139,575
ThorWaltonDeveloperNew York$98,540
FinnCamachoSupport EngineerSan Francisco$87,500
SergeBaldwinData CoordinatorSingapore$138,575
ZenaidaFrankSoftware EngineerNew York$125,250
ZoritaSerranoSoftware EngineerSan Francisco$115,000
JenniferAcostaJunior Javascript DeveloperEdinburgh$75,650
CaraStevensSales AssistantNew York$145,600
HermioneButlerRegional DirectorLondon$356,250
LaelGreerSystems AdministratorLondon$103,500
JonasAlexanderDeveloperSan Francisco$86,500
ShadDeckerRegional DirectorEdinburgh$183,000
MichaelBruceJavascript DeveloperSingapore$183,000
DonnaSniderCustomer SupportNew York$112,000

The Javascript shown below is used to initialise the table shown in this example:

$(document).ready(function() { $('#example').DataTable( { "footerCallback": function ( row, data, start, end, display ) { var api = this.api(), data; // Remove the formatting to get integer data for summation var intVal = function ( i ) { return typeof i === 'string' ? i.replace(/[\$,]/g, '')*1 : typeof i === 'number' ? i : 0; }; // Total over all pages total = api .column( 4 ) .data() .reduce( function (a, b) { return intVal(a) + intVal(b); }, 0 ); // Total over this page pageTotal = api .column( 4, { page: 'current'} ) .data() .reduce( function (a, b) { return intVal(a) + intVal(b); }, 0 ); // Update footer $( api.column( 4 ).footer() ).html( '$'+pageTotal +' ( $'+ total +' total)' ); } } ); } );

In addition to the above code, the following Javascript library files are loaded for use in this example:

The HTML shown below is the raw HTML table element, before it has been enhanced by DataTables:

This example uses a little bit of additional CSS beyond what is loaded from the library files (below), in order to correctly display the table. The additional CSS used is shown below:

th { white-space: nowrap; }

The following CSS library files are loaded for use in this example to provide the styling of the table:

This table loads data by Ajax. The latest data that has been loaded is shown below. This data will update automatically as any additional data is loaded.

The script used to perform the server-side processing for this table is shown below. Please note that this is just an example script using PHP. Server-side processing scripts can be written in any language, using the protocol described in the DataTables documentation.