I've been working on getting some filtering functionality working on a practice project that uses Metronic by KeenThemes for building pages with Datatables. The only issue is I cannot get multi-column filtering working for the tables. I've been working on the "search" onClick function below trying to get that to work to no avail. No errors get produced but nothing changes with the table after it runs. I am aiming to have an input for every column that requires filtering. Am I missing something or am I just going about this entirely wrong? Any advice is greatly appreciated! Relevant code below
Javascript for init table
"use strict";
// Class definition
var KTDatatable = function() {
var datatable;
var init = function() {
datatable = $('#gift_datatable').KTDatatable({
// datasource definition
data: {
type: 'remote',
source: {
read: {
url: HOST_URL + '/pages/gifts/datatable',
// sample custom headers
// headers: {'x-my-custom-header': 'some value', 'x-test-header': 'the value'},
map: function(raw) {
// sample data mapping
var dataSet = raw;
if (typeof raw.data !== 'undefined') {
dataSet = raw.data;
}
return dataSet;
},
},
},
pageSize: 20,
serverPaging: true,
serverFiltering: true,
serverSorting: true,
},
// layout definition
layout: {
scroll: false,
footer: false,
},
// column sorting
sortable: true,
pagination: true,
// columns definition
columns: [{
field: 'id',
title: '#',
sortable: 'asc',
width: 60,
type: 'number',
selector: false,
textAlign: 'center',
}, {
field: 'attention',
title: 'Attention',
// callback function support for column rendering
template: function(row) {
if (row.attention) {
return '<span class="badge badge-warning">Attention</span>';
} else {
return '';
}
},
}, {
field: 'gift_date',
title: 'Gift Date',
}, {
field: 'gift_amount',
title: 'Gift Amount',
// callback function support for column rendering
template: function(row) {
return '$' + row.gift_amount.toLocaleString(undefined, { minimumFractionDigits: 2 });
},
}, {
field: 'recognition_name',
title: 'Donor',
width: 120,
}, {
field: 'restricted',
title: 'Restricted',
width: 85,
// callback function support for column rendering
template: function(row) {
if (row.restricted) {
return '<div class="text-center"><span class="fa fa-check"></span></div>';
} else {
return '';
}
},
}, {
field: 'campaign',
title: 'Campaign',
}, {
field: 'fund',
title: 'Fund',
}, {
field: 'Actions',
title: 'Actions',
sortable: false,
width: 80,
overflow: 'visible',
autoHide: false,
template: function(row) {
return `\
<a href="/pages/gifts/${row.id}" class="btn btn-sm btn-clean btn-icon mr-2 editbtn" title="Edit details">\
<span class="fa fa-pencil-alt" style="font-size: 16px">\
</span>\
</a>\
\
`;
},
}],
});
$('#search').on('click', function(e) {
e.preventDefault();
var params = {};
$('.datatable-input').each(function() {
var i = $(this).data('col-index');
if (params[i]) {
params[i] += '|' + $(this).val();
}
else {
params[i] = $(this).val();
}
});
$.each(params, function(i, val) {
var col = datatable.column(i);
col.search(val ? val : '', false, false);
});
datatable.reload();
});
$('#reset').on('click', function(e) {
e.preventDefault();
$('.datatable-input').each(function() {
$(this).val('');
table.column($(this).data('col-index')).search('', false, false);
});
datatable.reload();
});
};
return {
// public functions
init: function() {
init();
},
};
}();
jQuery(document).ready(function() {
KTDatatable.init();
});
HTML Code
<!--begin::Entry-->
<div class="d-flex flex-column-fluid">
<!--begin::Container-->
<div class="container">
<!--begin::Card-->
<div class="card card-custom">
<div class="card-header flex-wrap border-0 pt-6 pb-0">
<div class="card-title">
<h3 class="card-label">Gifts
<span class="text-muted pt-2 font-size-sm d-block">Donors</span></h3>
</div>
</div>
<div class="card-body">
<!--begin: Search Form-->
<!--begin::Search Form-->
<div class="mb-15">
<div class="row mb-6">
<div class="col-lg-3 mb-lg-0 mb-6">
<label>
Gift ID:
</label>
<input type="text" class="form-control datatable-input" data-col-index="0">
</div>
<div class="col-lg-3 mb-lg-0 mb-6">
<label>
Gift Date:
</label>
<input type="text" class="form-control datatable-input" data-col-index="2">
</div>
<div class="col-lg-3 mb-lg-0 mb-6">
<label>
Gift Amount:
</label>
<input type="text" class="form-control datatable-input" data-col-index="3">
</div>
<div class="col-lg-3 mb-lg-0 mb-6">
<label>Donor:</label>
<input type="text" class="form-control datatable-input" data-col-index="4">
</div>
</div>
<div class="row mb-6">
<div class="col-lg-3 mb-lg-0 mb-6">
<label>
Campaign:
</label>
<input type="text" class="form-control datatable-input" data-col-index="6">
</div>
<div class="col-lg-3 mb-lg-0 mb-6">
<label>Fund:</label>
<input type="text" class="form-control datatable-input" data-col-index="7">
</div>
</div>
<div class="row mt-8">
<div class="col-lg-12">
<button class="btn btn-primary" id="search">
<span>
<i>
<span>
Search
</span>
</i>
</span>
</button>
<button class="btn btn-secondary" id="reset">
<span>
<i>
<span>
Reset
</span>
</i>
</span>
</button>
</div>
</div>
</div>
<!--end::Search Form-->
<!--end: Search Form-->
<!--begin: Datatable-->
<div class="datatable datatable-bordered datatable-head-custom" id="gift_datatable"></div>
<!--end: Datatable-->
</div>
</div>
<!--end::Card-->
</div>
<!--end::Container-->
</div>
<!--end::Entry-->