Ajax and individual column searching (select inputs)

Ajax and individual column searching (select inputs)

yv_7yv_7 Posts: 3Questions: 2Answers: 0

Hello,
I'm trying to add some filters for my datatable columns, but ran into the problem that only the row-contents of the first page are put into the inputs. I'm working inside the laravel-framework and am using the code from this example.

This is my code:

$(document).ready(function () {
    $("#review").DataTable({
        serverSide: true,
        processing: true,
        ajax: "/getReviews",
        columns: [
            { name: 'id', searchable: false },
            { name: 'location_name' },
            { name: 'reviewer_name' },
            { name: 'created', searchable: false },
            { name: 'rating', searchable: false },
            { name: 'comment' },
        ],
        initComplete: function () {
            this.api().columns([1,4]).every(function () {
                var column = this;
                var select = $('<select><option value=""></option></select>')
                    .appendTo($(column.footer()).empty())
                    .on('change', function () {
                        var val = $.fn.dataTable.util.escapeRegex(
                            $(this).val()
                        );

                        column
                            .search(val ? '^' + val + '$' : '', true, false)
                            .draw();
                    });

                column.data().unique().sort().each(function (d, j) {
                    select.append('<option value="' + d + '">' + d + '</option>')
                });
            });
        }
    });
});

Is there a way to get all unique rows while the datatable is paginated?

Answers

  • kthorngrenkthorngren Posts: 21,330Questions: 26Answers: 4,951

    You can use the technique described in this thread to include the options in the initial serverside response from the server.

    Kevin

This discussion has been closed.