DataTables example Multi-column ordering

DataTables allows ordering by multiple columns at the same time, which can be activated in a number of different ways:

  • User shift click on a column (added the clicked column as a secondary, tertiary etc ordering column).
  • On a per-column basis (i.e. order by a specific column and then a secondary column if the data in the first column is identical), through the columns.orderData option.
  • Using the columns.orderData option to specify a multiple column order by default (for example [ 0, 1 ]).
  • Through the order() API method.

Note that, the ability for the user to shift click to order multiple columns can be disabled through the orderMulti option.

The example below shows the first column having a secondary order applied to the second column in the table, vice-versa for the second column being tied directly to the first and the salary column to the first name column.

First nameLast namePositionOfficeSalary
TigerNixonSystem ArchitectEdinburgh320800
GarrettWintersAccountantTokyo170750
AshtonCoxJunior Technical AuthorSan Francisco86000
CedricKellySenior Javascript DeveloperEdinburgh433060
AiriSatouAccountantTokyo162700
BrielleWilliamsonIntegration SpecialistNew York372000
HerrodChandlerSales AssistantSan Francisco137500
RhonaDavidsonIntegration SpecialistTokyo327900
ColleenHurstJavascript DeveloperSan Francisco205500
SonyaFrostSoftware EngineerEdinburgh103600
JenaGainesOffice ManagerLondon90560
QuinnFlynnSupport LeadEdinburgh342000
ChardeMarshallRegional DirectorSan Francisco470600
HaleyKennedySenior Marketing DesignerLondon313500
TatyanaFitzpatrickRegional DirectorLondon385750
MichaelSilvaMarketing DesignerLondon198500
PaulByrdChief Financial Officer (CFO)New York725000
GloriaLittleSystems AdministratorNew York237500
BradleyGreerSoftware EngineerLondon132000
DaiRiosPersonnel LeadEdinburgh217500
JenetteCaldwellDevelopment LeadNew York345000
YuriBerryChief Marketing Officer (CMO)New York675000
CaesarVancePre-Sales SupportNew York106450
DorisWilderSales AssistantSydney85600
AngelicaRamosChief Executive Officer (CEO)London1200000
GavinJoyceDeveloperEdinburgh92575
JenniferChangRegional DirectorSingapore357650
BrendenWagnerSoftware EngineerSan Francisco206850
FionaGreenChief Operating Officer (COO)San Francisco850000
ShouItouRegional MarketingTokyo163000
MichelleHouseIntegration SpecialistSydney95400
SukiBurksDeveloperLondon114500
PrescottBartlettTechnical AuthorLondon145000
GavinCortezTeam LeaderSan Francisco235500
MartenaMccrayPost-Sales supportEdinburgh324050
UnityButlerMarketing DesignerSan Francisco85675
HowardHatfieldOffice ManagerSan Francisco164500
HopeFuentesSecretarySan Francisco109850
VivianHarrellFinancial ControllerSan Francisco452500
TimothyMooneyOffice ManagerLondon136200
JacksonBradshawDirectorNew York645750
OliviaLiangSupport EngineerSingapore234500
BrunoNashSoftware EngineerLondon163500
SakuraYamamotoSupport EngineerTokyo139575
ThorWaltonDeveloperNew York98540
FinnCamachoSupport EngineerSan Francisco87500
SergeBaldwinData CoordinatorSingapore138575
ZenaidaFrankSoftware EngineerNew York125250
ZoritaSerranoSoftware EngineerSan Francisco115000
JenniferAcostaJunior Javascript DeveloperEdinburgh75650
CaraStevensSales AssistantNew York145600
HermioneButlerRegional DirectorLondon356250
LaelGreerSystems AdministratorLondon103500
JonasAlexanderDeveloperSan Francisco86500
ShadDeckerRegional DirectorEdinburgh183000
MichaelBruceJavascript DeveloperSingapore183000
DonnaSniderCustomer SupportNew York112000

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

$('#example').DataTable({ columnDefs: [ { targets: [0], orderData: [0, 1] }, { targets: [1], orderData: [1, 0] }, { targets: [4], orderData: [4, 0] } ] });
new DataTable('#example', { columnDefs: [ { targets: [0], orderData: [0, 1] }, { targets: [1], orderData: [1, 0] }, { targets: [4], orderData: [4, 0] } ] });

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:

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.