I want to use DataTables column search. But I don't know how should be server side code.

I want to use DataTables column search. But I don't know how should be server side code.

KancerKancer Posts: 2Questions: 1Answers: 0

I use this code for column search https://datatables.net/examples/api/multi_filter_select.html
I use Spring MVC so I've contoller class. I get data from MongoDB. My question is how should be controller class?
This is my Javascript code:

 var table = $('#example').DataTable({
                "processing": true,
                "serverSide": true,
                "ajax": {"url": "locations/pagedList", "type": "GET"},
                "searching": true,
                "ordering": false,
                "columns": [
                    {"data": "id", "visible": false},
                   // .........
                   ],
                   "initComplete": function () {

                    //for search
                    var column = this.api().column(7);

                    var select = $('<select><option value=""></option></select>')
                        .appendTo($(column.footer()).empty())
                        .on('change', function () {

                            column
                                .search($(this).val())
                                .draw();
                        });

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

And this is my controller side:

 @RequestMapping(value = "/pagedList", method = RequestMethod.GET)
    @ResponseBody
    public LocationListResponse pagination(@RequestBody int draw,
                                           @RequestBody DataColumns[] columns,
                                           @RequestBody int start,
                                           @RequestBody int length,
                                           @RequestBody Search search) {

I see request in browser like this:

columns[0][data]=
columns[0][name]=
columns[0][orderable]=false
columns[0][search][regex]=false
columns[0][search][value]=
columns[0][searchable]=true
columns[1][data]=
columns[1][name]=
columns[1][orderable]=false
columns[1][search][regex]=false
columns[1][search][value]=
columns[1][searchable]=true
order[0][column]=4
order[0][dir]=desc
order[1][column]=4
order[1][dir]=desc
search[regex]=false
search[value]=

So I found this site about this: http://stackoverflow.com/questions/27568446/datastructure-for-the-datatable-server-side-processing

I'm getting this error: 400 Bad Request

Please help me :)

This question has an accepted answers - jump to answer

Answers

  • daniel_rdaniel_r Posts: 460Questions: 4Answers: 67
    Answer ✓

    You can get some help by looking at yadcf server side showcase page code source and see the showcase page itself

  • KancerKancer Posts: 2Questions: 1Answers: 0

    Thank you for your help @daniel_r :) I think this answer not enough for me.
    But I solved my problem.
    First we should change and add some attributes in javascript:

    "ajax": {
                        "url": "locations/pagedList",
                        "type": "POST",
                        dataType: "json",
                        "contentType": "application/json",
                        "data": function ( d ) {
                            return JSON.stringify( d );
                        }
    

    Second we should change controller like this:

     @RequestMapping(value = "/pagedList", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
        @ResponseBody
        public LocationListResponse pagination(@RequestBody JsonNode data) {
    

    Thats it :)

This discussion has been closed.