ColVis example Two tables with shared controls

This example shows how the DataTables API can be used with ColVis to use a single ColVis control to effect other tables. This is done by applying ColVis to the first table and then listening for the column-visibilityDT event and updating all other tables when triggered.

This example makes use of the tables()DT and table()DT methods for working with multiple tables, and also initialised ColVis using the new $.fn.dataTable.ColVis(); operator.

NamePositionOfficeAgeSalary
NamePositionOfficeAgeSalary
Cedric Kelly Senior Javascript Developer Edinburgh 22 $433,060
Dai Rios Personnel Lead Edinburgh 35 $217,500
Gavin Joyce Developer Edinburgh 42 $92,575
Jennifer Acosta Junior Javascript Developer Edinburgh 43 $75,650
Martena Mccray Post-Sales support Edinburgh 46 $324,050
Showing 1 to 5 of 9 entries
NamePositionOfficeAgeSalary
NamePositionOfficeAgeSalary
Angelica Ramos Chief Executive Officer (CEO) London 47 $1,200,000
Bradley Greer Software Engineer London 41 $132,000
Bruno Nash Software Engineer London 38 $163,500
Haley Kennedy Senior Marketing Designer London 43 $313,500
Hermione Butler Regional Director London 47 $356,250
Showing 1 to 5 of 12 entries

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
$(document).ready(function() {
    var tables = $('table.display').DataTable( {
        displayLength: 5
    } );
 
    // When the column visibility changes on the firs table, also change it on
    // the others
    tables.table(0).on('column-visibility', function ( e, settings, colIdx, visibility ) {
        tables.tables(':gt(0)').column( colIdx ).visible( visibility );
    } );
 
    // Create ColVis on the first table only
    var colvis = new $.fn.dataTable.ColVis( tables.table(0) );
    $( colvis.button() ).insertAfter('div.info');
} );

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