How to redraw a table, when I apply filters?
How to redraw a table, when I apply filters?
Maxim_1
Posts: 15Questions: 5Answers: 1
I try to make filters for my table, but when I want to draw only filtered data, it nothing do. I use Datatables. Use ServerSide. When I try to filter, it just nothing does. In console good, but nothing works
Javascript
var table = $('#logbook').DataTable({
"processing": true,
"serverSide": true,
"pagingType": "full_numbers",
"iDisplayLength": 20,
"renderer": '',
"ajax": {
"url": "sort.php",
"type": "POST",
},
"language": {
"lengthMenu": "",
},
function applyFilters() {
var selectedFilters = {};
$('.column-filter:checked').each(function () {
var columnIndex = $(this).data('column-index');
var filterValue = $(this).data('value');
if (!selectedFilters[columnIndex]) {
selectedFilters[columnIndex] = [];
}
selectedFilters[columnIndex].push(filterValue);
});
for (var columnIndex in selectedFilters) {
if (selectedFilters.hasOwnProperty(columnIndex)) {
var filterValues = selectedFilters[columnIndex];
var filterString = filterValues.join('|');
console.log(filterValues);
console.log(filterString);
console.log(table.columns(columnIndex).search(filterString));
table.columns(columnIndex).search(filterString);
}
}
table.draw();
}
php
PHP
$filterParams = isset($_POST['filterParams']) ? json_decode($_POST['filterParams'], true) : [];
if (!empty($filterParams)) {
foreach ($filterParams as $column => $filterValues) {
if (!empty($filterValues) && in_array($column, $columns)) {
$filterValues = array_map([$conn, 'real_escape_string'], $filterValues);
$filterValues = "'" . implode("', '", $filterValues) . "'";
$sql .= " AND $column IN ($filterValues)";
}
}
}
It doesn't work
Edited by Kevin: Syntax highlighting. Details on how to highlight code using markdown can be found in this guide
Answers
Use the browser's network inspector to see what is being sent when
draw()
is called on line 38. You will see the server side processing parameters being sent as documented here. It looks like you are trying to fetch the search parameters from$_POST['filterParams']
but this parameter is not being sent to the server. You will need to process the parameters as sent via the server side processing protocol.You can look at (and use if you wish) the ssp.class.php script used by the server side processing examples. You might want to use the Editor server side processing library as documented in this blog.
Kevin
I want make filters like in Excel
Can you show me a screenshot?
Do you mean like this?
Allan