Get additional data with Ajax request

Get additional data with Ajax request

yv_7yv_7 Posts: 3Questions: 2Answers: 0

I am using server-side processing in my DataTable and am trying to implement individual column searching (select inputs), like in this example. How is this possible? This is my code:

$(document).ready(function () {
    $("#review").DataTable({
        serverSide: true,
        deferRender: 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' },
        ],
    });
})

And the controller in laravel that gets called and fetches the data:

    /**
     * Returns data of the basic datatables.
     *
     * @return  Illuminate\Http\JsonResponse
     **/
    public function reviewData()
    {
        return Laratables::recordsOf(Review::class);
    }

When I'm trying the implementation of the example, the only data that I can sort by is the data from the current page. But I want to filter the whole dataset. Is there a way to get all unique values from a column with the ajax-call?

Or is there another workaround for making the filtering work? Because the default search and pagination is working properly on the whole dataset.

This question has accepted answers - jump to:

Answers

  • colincolin Posts: 15,237Questions: 1Answers: 2,599
    Answer ✓

    Yep, when serverSide is set, it's the responsibility of the server-side script to filter the data as requested by the browser. The browser would send the filters for each column, then it's upto the server to return the correct data.

    Colin

  • yv_7yv_7 Posts: 3Questions: 2Answers: 0

    But how should the data for the filters look like?

    For example, if I only want the data for filtering one column, I could just find all unique values for that column from the dataset server-side. How should the json for that response be structured?

  • colincolin Posts: 15,237Questions: 1Answers: 2,599
    Answer ✓

    It would be the same style response - only rows matching are returned. The browser sends all filters to the server - the global search and the individual column searches - then the server just returns the hits.

    The protocol is discussed here. Also see examples here.

    Cheers,

    Colin

This discussion has been closed.