[英]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.