Col reorder
by jsfiddlertwo
HTML
<link rel="stylesheet" href="https://cdn.datatables.net/v/dt/dt-1.10.16/datatables.min.css">
<script src="https://cdn.datatables.net/v/dt/dt-1.10.16/datatables.min.js"></script>
<script src="https://cdn.datatables.net/colreorder/1.5.1/js/dataTables.colReorder.min.js"></script>
<link rel="stylesheet" href="https://cdn.datatables.net/1.10.19/css/jquery.dataTables.min.css">
<link rel="stylesheet" href="https://cdn.datatables.net/colreorder/1.5.1/css/colReorder.dataTables.min.css">
<hr>
<table id="example" class="display" cellspacing="0" width="100%">
</table>
CSS
td.details-control {
background: url('https://cdn.rawgit.com/DataTables/DataTables/6c7ada53ebc228ea9bc28b1b216e793b1825d188/examples/resources/details_open.png') no-repeat center center;
cursor: pointer;
}
tr.shown td.details-control {
background: url('https://cdn.rawgit.com/DataTables/DataTables/6c7ada53ebc228ea9bc28b1b216e793b1825d188/examples/resources/details_close.png') no-repeat center center;
}
JavaScript
/* Formatting function for row details - modify as you need */
function format ( d ) {
var childId = d.extn;
var div = $('<div><table id=\'child-table' + childId + '\' class=\'table-striped table-bordered\' width="100%"></table></div>');
var childTable = $('table', div).DataTable({
colReorder: true,
searching: false,
info: false,
paging: false,
data: d.children,
columns: [{
title: "Name",
render: function(data, type, row, meta) {
return row.name;
}
},
{
title: "Class",
render: function(data, type, row, meta) {
return row.class;
}
},
]
});
return div;
}
$(document).ready(function() {
var table = $('#example').DataTable({
ajax: 'https://api.myjson.com/bins/17z2am',
colReorder: true,
columns: [
{
'className': 'details-control',
'orderable': false,
'data': null,
'defaultContent': ''
},
{ 'title': 'Name', 'data': 'name' },
{ 'title': 'Position', 'data': 'position' },
{ 'title': 'Office', 'data': 'office' },
{ 'title': 'Salary', 'data': 'salary' }
],
order: [[1, 'asc']]
} );
// Add event listener for opening and closing details
$('#example tbody').on('click', 'td.details-control', function(){
var tr = $(this).closest('tr');
var row = table.row( tr );
if(row.child.isShown()){
// This row is already open - close it
row.child.hide();
tr.removeClass('shown');
} else {
// Open this row
row.child(format(row.data())).show();
tr.addClass('shown');
}
});
});