//sample source data
const srcData = [
{item: 'apple', type: 'fruit', qty: 5},
{item: 'pear', type: 'fruit', qty: 4},
{item: 'banana', type: 'fruit', qty: 7},
{item: 'carrot', type: 'vegie', qty: 14},
{item: 'goosberry', type: 'berry', qty: 6}
];
//initialize DataTables
const dataTable = $('#mytable').DataTable({
dom: 'ft',
data: srcData,
columns: ['item', 'type', 'qty'].map(header => ({title: header, data: header})),
});
//append footer
$('#mytable').append('<tfoot><tr></tr></tfoot>');
//populate that with input fields
dataTable.columns().every(function(){
$('#mytable tfoot tr').append(`<td><input colindex="${this.index()}" placeholder="${$(this.header()).text()}"></input></td>`);
});
//filter upon column inputs
$('#mytable').on('keyup', 'tfoot input', function(){
dataTable.column($(this).attr('colindex')).search($(this).val()).draw();
});
//implement clearall button
$('#clearall').on('click', () => {
//empty all inputs
[...$('.dataTables_wrapper input')].forEach(input => $(input).val(''));
//clear individual columns search
dataTable.columns().every(function(){this.search('')});
//clear global search
dataTable.search('');
//re-draw
dataTable.draw();
});
//toggle 'clearall' button visibility
$('.dataTables_wrapper input').on('keyup', () => {
if([...$('.dataTables_wrapper input')].some(input => $(input).val().length > 0)) $('#clearall').show();
else $('#clearall').hide();
});
<!doctype html>
<html>
<head>
<script type="application/javascript" src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<script type="application/javascript" src="https://cdn.datatables.net/1.10.19/js/jquery.dataTables.min.js"></script>
<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.19/css/jquery.dataTables.min.css">
</head>
<body>
<table id="mytable"></table>
<button id="clearall" hidden>Clear all</button>
</body>
</html>