简体   繁体   English

我想使用DataTables列搜索。 但是我不知道服务器端代码应该如何

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

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. 我使用此代码进行列搜索https://datatables.net/examples/api/multi_filter_select.html我使用Spring MVC,所以我使用了contoller类。 I get data from MongoDB. 我从MongoDB获取数据。 My question is how should be controller class? 我的问题是控制器类应该如何? This is my Javascript code: 这是我的Javascript代码:

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: DataStructure for the DataTable server side processing 因此,我找到了有关此内容的网站: 用于DataTable服务器端处理的DataStructure

I'm getting this error: 400 Bad Request 我收到此错误:400错误的请求

Please help me :) 请帮我 :)

I solved my problem. 我解决了我的问题。 First we should change and add some attributes in javascript: 首先,我们应该更改并在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 :) 而已 :)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我想重复代码,但不知道如何使用循环 - I want to repeat code but don't know how t use loops 想在我的代码中使用 for 循环,但我不知道怎么可能 - A want to use the for loop in my code, but I don't know how is it possible 我是 React 的新手,我想将普通的 javascript 代码转换为 React,我不知道该怎么做 - I am new in react i want to convert normal javascript code to the react i don't know how to do that 我知道我想要什么,但我不知道该怎么做! (chrome扩展名) - I know what I want, but I don't know how to do it! (chrome extension) 数据添加到firestore的时候,想用onSnapshot监听,这样就可以自动调用数据了,但是不知道怎么用 - When data is added to the firestore, I want to listen with onSnapshot so that the data can be called automatically, but I don't know how to use 如何在服务器端处理数据表上使用搜索? - How to use search on datatables with server-side processing? 数据表服务器端列过滤器搜索延迟 - Datatables server side column filter search delay 在这种情况下如何使用泛型这种类型? 我不知道为什么 - how to use generic this type in this case ? i don't know why 我不知道如何在 sequelize (Mac) 中使用 POST 方法 - I don't know how to use POST method in sequelize (Mac) 我有一个无循环功能问题,我不知道如何或是否应该费心解决 - I have a no-loop-func issue that I don't know how or if I should bother solving
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM