jQuery Datatables responsive example
HTML
<script src="//code.jquery.com/jquery-1.9.1.js"></script>
<script src="http://code.jquery.com/ui/1.9.2/jquery-ui.js"></script>
<link rel="stylesheet" href="http://code.jquery.com/ui/1.9.2/themes/base/jquery-ui.css">
<link rel="stylesheet" href="//cdn.datatables.net/1.10.1/css/jquery.dataTables.css">
<script src="//cdn.datatables.net/1.10.1/js/jquery.dataTables.min.js"></script>
<link rel="stylesheet" href="//cdn.datatables.net/responsive/1.0.0/css/dataTables.responsive.css">
<script src="//cdn.datatables.net/responsive/1.0.0/js/dataTables.responsive.js"></script>
<button id="FilterCurrentPage" name="button" onclick="javascript:void(0);">Filter Current Page</button>
<button id="ReloadTable" name="button" onclick="javascript:void(0);">Reload Table</button>
<table id="example" class="display" cellspacing="0" width="100%">
<thead>
<tr>
<th>Visible</th>
<th>Name</th>
<th>Position</th>
<th>Office</th>
<th>Age</th>
<th>Start date</th>
<th>Salary</th>
</tr>
</thead>
<tfoot>
<tr>
<th>Visible</th>
<th>Name</th>
<th>Position</th>
<th>Office</th>
<th>Age</th>
<th>Start date</th>
<th>Salary</th>
</tr>
</tfoot>
<tbody>
<tr>
<td>0</td>
<td>Tiger Nixon</td>
<td>System Architect</td>
<td>Edinburgh</td>
<td>61</td>
<td>2011/04/25</td>
<td>$320,800</td>
</tr>
<tr>
<td>0</td>
<td>Garrett Winters</td>
<td>Accountant</td>
<td>Tokyo</td>
<td>63</td>
<td>2011/07/25</td>
<td>$170,750</td>
</tr>
<tr>
<td>0</td>
<td>Ashton Cox</td>
<td>Junior Technical Author</td>
<td>San Francisco</td>
<td>66</td>
...
CSS
table th:nth-child(3), td:nth-child(3) {
display: none;
}
JavaScript
$(document).ready(function () {
var table = $('#example').DataTable({
"columnDefs": [{
"targets": [0],
"visible": false,
"searchable": true
}]
});
// Show page 0
table.page(0).draw(false);
// Sort by column 1 (Name)
table.order([1, 'asc']).draw(false);
$("#ReloadTable").click(function () {
// Clear the current filter
oTable = $('#example').dataTable();
oTable.fnFilter('', 0);
oTable.fnFilter('');
// Reset all "visible" values in the first cell
var table = $('#example').DataTable();
table.rows().indexes().each(function (idx) {
var d = table.row(idx).data();
table.row(idx).data()[0] = 0;
d.counter++;
table.row(idx).data(d);
});
});
$("#FilterCurrentPage").click(function () {
// Create a hash of the index of currently visible rows
var h = new Object();
var x = $('.odd,.even').filter(function () {
var idx = $(this)[0]._DT_RowIndex;
h[idx] = idx;
return this.style.display == ''
});
// update the first value based on the currently visible rows
var table = $('#example').DataTable();
table.rows().indexes().each(function (idx) {
var d = table.row(idx).data();
if (h.hasOwnProperty(idx)) {
table.row(idx).data()[0] = 1;
}
d.counter++;
table.row(idx).data(d);
});
// filter rows with a value of 1 in the first cell
oTable = $('#example').dataTable();
oTable.fnFilter(1, 0);
});
});