# datatables-responsive ## Introduction [Datatables][1] is hands down the best [jQuery][2] table plugin. I have enjoyed using it over the years and highly recommend it to all. Recently, I have tried to use Datatables in an responsive web project. Datatables did everything brilliantly but was not responsive. After some research, I found [FooTable][3] which handles the responsive behavior perfectly. After tinkering around, I've come up with something that helps make Datatables respond like FooTable. Complete working [examples][4] are provided using [Bootstrap][5]. You may use any front-end framework you prefer. Below are the instructions on how to use the helper. ## Add Viewport Meta Tag For Mobile Support Add the following viewport meta tag to your HTML's head section: ```html ``` ## Include CSS files Add Bootstrap, Datatables-Bootstrap and responsive Datatables helper CSS files. **DataTables 1.9.x and Bootstrap 3.x** ```html ``` If you are using Bootstrap 2, see the `ajax-bootstrap2.html` example. **DataTables 1.10.x and Bootstrap 3.x** ```html ``` For more information on Datatables and Bootstrap 2, see [http://www.datatables.net/blog/Twitter_Bootstrap][6] [http://www.datatables.net/blog/Twitter_Bootstrap_2][7] For Bootstrap 3, see [https://github.com/DataTables/Plugins/tree/master/integration/bootstrap/3][8] ## Include JS files Add jQuery, Datatables, Datables-Bootstrap and the responsive Datatables helper scripts. **DataTables 1.9.x and Bootstrap 3.x** ```html ``` **DataTables 1.10.x and Bootstrap 3.x** ```html ``` ## Create variables and break point definitions. ```javascript var responsiveHelper; var breakpointDefinition = { tablet: 1024, phone : 480 }; var tableElement = $('#example'); ``` ## Create Datatables Instance Create the datatables instance with the following ### DataTables 1.9.x - Set `bAutoWidth` to `false`. - Set `fnPreDrawCallback` to only initialize the responsive datatables helper once - Set `fnRowCallback` to create expand icon. - Set `fnDrawCallback` to respond to window `resize` events. **DataTables 1.9.x and Responsive Helper Initialization** ```javascript tableElement.dataTable({ // Setup for Bootstrap support. sPaginationType : 'bootstrap', oLanguage : { sLengthMenu: '_MENU_ records per page' }, // Setup for responsive datatables helper. bAutoWidth : false, fnPreDrawCallback: function () { // Initialize the responsive datatables helper once. if (!responsiveHelper) { responsiveHelper = new ResponsiveDatatablesHelper(tableElement, breakpointDefinition); } }, fnRowCallback : function (nRow, aData, iDisplayIndex, iDisplayIndexFull) { responsiveHelper.createExpandIcon(nRow); }, fnDrawCallback : function (oSettings) { responsiveHelper.respond(); } }); ``` ### DataTables 1.10.x - Set `autoWidth` to `false`. - Set `preDrawCallback` to only initialize the responsive datatables helper once - Set `rowCallback` to create expand icon. - Set `drawCallback` to respond to window `resize` events. **DataTables 1.10.x and Responsive Helper Initialization** ```javascript tableElement.dataTable({ autoWidth : false, preDrawCallback: function () { // Initialize the responsive datatables helper once. if (!responsiveHelper) { responsiveHelper = new ResponsiveDatatablesHelper(tableElement, breakpointDefinition); } }, rowCallback : function (nRow) { responsiveHelper.createExpandIcon(nRow); }, drawCallback : function (oSettings) { responsiveHelper.respond(); } }); ``` ## Add Data Attributes to the Table Elements - Add the `data-class="expand"` attribute to the `th` element for the respective column that will you want to display the expand icon in. The `th` element cannot be for a column that will be hidden. - Add `data-hide="phone,tablet"` to the `th` element for the respective column that will you want to hide when the window is resized. - Add `data-name="Hidden Column Name"` to the `th` element for the respective column that will you would like its label to be set to when hidden. ```html
| Rendering engine | Browser | Platform(s) | Engine version | CSS grade | |
|---|---|---|---|---|---|
| Engine | Browser | Platform(s) | Engine version | CSS grade |