我将此代码用于列搜索https://datatables.net/examples/api/multi_filter_select.html 我使用Spring MVC所以我有contoller类。我从MongoDB获取数据。我的问题是如何成为控制器类? 这是我的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>')
});
``` 这是我的控制方:
@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) { //... }
我在浏览器中看到这样的请求:
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]=
所以我找到了这个网站:DataStructure for the DataTable server side processing
我收到此错误:400错误请求
请帮帮我:)。
答案 0 :(得分:0)
我解决了我的问题。首先,我们应该在javascript中更改并添加一些属性:
"ajax": {
"url": "locations/pagedList",
"type": "POST",
dataType: "json",
"contentType": "application/json",
"data": function ( d ) {
return JSON.stringify( d );
}
其次我们应该改变这样的控制器:
@RequestMapping(value = "/pagedList", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public LocationListResponse pagination(@RequestBody JsonNode data) {
多数民众赞成:)