Don't redraw the table in Datatables

Don't redraw the table in Datatables

Maxim_1Maxim_1 Posts: 15Questions: 5Answers: 1
edited November 2023 in Free community support

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

 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

$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 Allan - Syntax highlighting. Details on how to highlight code using markdown can be found in this guide.

Answers

This discussion has been closed.