I have a amateur sports club mamagement system that I have written using HTML5/PHP/Bootstrap, and use Datatables at various places.
For one of the tables, managing the waiting list, I need to filter a database set by a maximum and minimum age, so have used the code provided by Datatables in the following link (https://www.datatables.net/examples/plug-ins/range_filtering.html).
Everything about the table works, except changing the max and min values has no effect.
The head area has the following Javascript (copied from Datatables)
$.fn.dataTable.ext.search.push(
function (settings, data, dataIndex) {
var min = parseInt($('#min').val(), 10);
var max = parseInt($('#max').val(), 10);
var age = parseFloat(data[2]) || 0; // use data for the age column
if ((isNaN(min) && isNaN(max)) ||
(isNaN(min) && age <= max) ||
(min <= age && isNaN(max)) ||
(min <= age && age <= max))
{
return true;
}
return false;
}
);
The tables within the body section (html/php)
<table border="0" cellspacing="5" cellpadding="5">
<tr>
<td>Minimum age:</td>
<td><input type="number" id="min" name="min" min="3" max="21"></td>
<td>Maximum age:</td>
<td><input type="number" id="max" name="max" min="3" max="21"></td>
</tr>
</table>
<table id = "table" class = "display compact cell-border stripe" width="100%">
<thead>
<tr>
<th>Name</th>
<th>Gender</th>
<th>Age</th>
<th>Date Added</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<?php
$query = $conn->query("SELECT * FROM `waitlist` WHERE wl_status ='Active'") or die(mysqli_error());
while ($f_query = $query->fetch_array()) {
echo '<tr>';
echo '<td>' . $f_query['wl_Name'] . '</td>';
echo '<td>' . $f_query['wl_Gender'] . '</td>';
echo '<td>' . getAge($f_query['wl_DoB']) . '</td>';
echo '<td data-sort="' . $f_query['wl_DateAdded'] . '">' . date('d/m/Y', strtotime($f_query['wl_DateAdded'])) . '</td>';
echo '<td><center><a href = "wl_edit.php?wl_id=' . $f_query['wl_ID'] . '" class = "btn btn-warning btn-sm"><span class = "fa fa-edit"></span> Details</a>' . ' | <a onclick = "javascript:confirmationDelete($(this)); return false;" href = "wl_delete.php?wl_id=' . $f_query['wl_ID'] . '" class = "btn btn-danger btn-sm"><span class = "fa fa-trash"></span> Delete</a>' . ' | <a href = "wl_transfer.php?wl_id=' . $f_query['wl_ID'] . '" class = "btn btn-primary btn-sm"><span class = "fa fa-paste"></span> To Member</a></center></td>';
echo '</tr>';
};
?>
</tbody>
</table>
The Javascript at bottom of page contains:
$(document).ready(function () {
$('#table').DataTable({
"lengthChange": false,
"pageLength": 12,
"pagingType": "full_numbers",
"order": [[3, "asc"]]
});
var table = $('#table').DataTable();
$('#min, #max').keyup(function () {
table.draw();
});
});
The keyup function part was copied from the Datatables link
The getage PHP function is part of the PHP header
function getAge($date) { // Y-m-d format
return intval(substr(date('Ymd') - date('Ymd', strtotime($date)), 0, -4));
I am still struggling with javascript, and expect that is where the problem lies, but cannot see the obvious cause of my problem. Thanks