$(document).ready(function() {
$("table.my-table").DataTable({
"dom": "lBfrtip",
"buttons": [{
"extend": 'excel',
"exportOptions": {
"format": {
"header": function(content, index) {
// Here 2 is the index of the column whose header name we want to change(0 based)
return index === 2 ? "Gender" : content;
}
}
}
}],
// execute callback when DataTable is completely initialiazed
"initComplete": function() {
// Select the column whose header we need replaced using its index(0 based)
this.api().column(2).every(function() {
var column = this;
// Put the HTML of the <select /> filter along with any default options
var select = $('<select class="form-control input-sm"><option value="">All</option></select>')
// remove all content from this column's header and
// append the above <select /> element HTML code into it
.appendTo($(column.header()).empty())
// execute callback when an option is selected in our <select /> filter
.on('change', function() {
// escape special characters for DataTable to perform search
var val = $.fn.dataTable.util.escapeRegex(
$(this).val()
);
// Perform the search with the <select /> filter value and re-render the DataTable
column
.search(val ? '^' + val + '$' : '', true, false)
.draw();
});
// fill the <select /> filter with unique values from the column's data
column.data().unique().sort().each(function(d, j) {
select.append("<option value='" + d + "'>" + d + "</option>")
});
});
},
// disable sorting on the column with the filter in its header.
"columnDefs": [{
targets: [2],
orderable: false
}]
});
});
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.