# 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-visibility` event and updating all other tables when triggered.

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

Name | Position | Office | Age | Salary
--- | --- | --- | --- | ---
Name | Position | Office | Age | Salary
Tiger Nixon | System Architect | Edinburgh | 61 | $3,120
Garrett Winters | Director | Edinburgh | 63 | $5,300
Cedric Kelly | Javascript Developer | Edinburgh | 22 | $3,600
Jenna Elliott | Financial Controller | Edinburgh | 33 | $5,300
Rhona Davidson | Integration Specialist | Edinburgh | 55 | $6,730
Sonya Frost | Software Engineer | Edinburgh | 23 | $3,600
Quinn Flynn | Financial Controller | Edinburgh | 22 | $4,200
Dai Rios | System Architect | Edinburgh | 35 | $4,200
Doris Wilder | Sales Assistant | Edinburgh | 23 | $4,965
Gavin Joyce | Developer | Edinburgh | 42 | $4,525
Russell Chavez | Director | Edinburgh | 20 | $3,600
Michelle House | Integration Specialist | Edinburgh | 37 | $3,750
Martena Mccray | Integration Specialist | Edinburgh | 46 | $4,080
Miriam Weiss | Support Engineer | Edinburgh | 64 | $4,965
Odessa Jackson | Support Engineer | Edinburgh | 37 | $3,600
Elton Baldwin | Data Coordinator | Edinburgh | 64 | $6,730
Jennifer Acosta | Javascript Developer | Edinburgh | 43 | $2,875
Shad Decker | Regional Director | Edinburgh | 51 | $5,300
Michael Bruce | Javascript Developer | Edinburgh | 29 | $4,080

Name | Position | Office | Age | Salary
--- | --- | --- | --- | ---
Name | Position | Office | Age | Salary
Jena Gaines | System Architect | London | 30 | $5,000
Haley Kennedy | Senior Marketing Designer | London | 43 | $4,800
Tatyana Fitzpatrick | Regional Director | London | 19 | $2,875
Michael Silva | Senior Marketing Designer | London | 66 | $3,750
Bradley Greer | Software Engineer | London | 41 | $3,120
Angelica Ramos | System Architect | London | 36 | $2,875
Jennifer Chang | Regional Director | London | 28 | $4,080
Suki Burks | Developer | London | 53 | $2,875
Prescott Bartlett | Technical Author | London | 27 | $6,730
Timothy Mooney | Financial Controller | London | 37 | $4,200
Bruno Nash | Software Engineer | London | 38 | $4,200
Hermione Butler | Director | London | 47 | $4,080
Lael Greer | Systems Administrator | London | 21 | $3,120

- Javascript
- HTML
- CSS
- Ajax
- Server-side script

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

`$(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:

- ../../../media/js/jquery.js
- ../../../media/js/jquery.dataTables.js
- ../js/dataTables.colVis.js

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:

- ../../../media/css/jquery.dataTables.css
- ../css/dataTables.colVis.css

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.