我使用数据表的spring数据 似乎搜索无效。
服务侧控制器
@GetMapping("cars")
@ResponseBody
public Map<String, Object> getCars(@RequestParam("draw") Integer draw,
@RequestParam(value = "search", defaultValue = "") String search,
@RequestParam(value = "page", required = false) Integer page,
@RequestParam(value = "size", required = false) Integer size,
@RequestParam(value = "order", required = false) String direction,
@RequestParam(value = "sort", required = false) String sortProperty) {
search is alway null
...
}
客户端
var url = "/cars";
$('#carsTable').DataTable({
'bLengthChange': false,
'processing': true,
'serverSide': true,
'pagingType': 'simple_numbers',
'dom': 'Bfrtip',
'ajax': {
'type': 'get',
'url': url,
'data': function (d) {
var current = $('#carsTable').DataTable();
d.page = (current != undefined) ? current.page.info().page : 0;
d.size = (current != undefined) ? current.page.info().length : 5;
d.sort = d.columns[d.order[0].column].data;
d.order = d.order[0].dir;
}
},
'columns': [
{'data': 'id'},
{'data': 'buildDate'},
{'data': 'brand'},
{'data': 'model'},
{'data': 'color'}
]
});
由datatable生成的网址,不知道是否可以使用更干净的网址?
http://localhost:8080/cars?draw=6&columns%5B0%5D%5Bdata%5D=id&columns%5B0%5D%5Bname%5D=&columns%5B0%5D%5Bsearchable%5D=true&columns%5B0%5D%5Borderable%5D=true&columns%5B0%5D%5Bsearch%5D%5Bvalue%5D=&columns%5B0%5D%5Bsearch%5D%5Bregex%5D=false&columns%5B1%5D%5Bdata%5D=buildDate&columns%5B1%5D%5Bname%5D=&columns%5B1%5D%5Bsearchable%5D=true&columns%5B1%5D%5Borderable%5D=true&columns%5B1%5D%5Bsearch%5D%5Bvalue%5D=&columns%5B1%5D%5Bsearch%5D%5Bregex%5D=false&columns%5B2%5D%5Bdata%5D=brand&columns%5B2%5D%5Bname%5D=&columns%5B2%5D%5Bsearchable%5D=true&columns%5B2%5D%5Borderable%5D=true&columns%5B2%5D%5Bsearch%5D%5Bvalue%5D=&columns%5B2%5D%5Bsearch%5D%5Bregex%5D=false&columns%5B3%5D%5Bdata%5D=model&columns%5B3%5D%5Bname%5D=&columns%5B3%5D%5Bsearchable%5D=true&columns%5B3%5D%5Borderable%5D=true&columns%5B3%5D%5Bsearch%5D%5Bvalue%5D=&columns%5B3%5D%5Bsearch%5D%5Bregex%5D=false&columns%5B4%5D%5Bdata%5D=color&columns%5B4%5D%5Bname%5D=&columns%5B4%5D%5Bsearchable%5D=true&columns%5B4%5D%5Borderable%5D=true&columns%5B4%5D%5Bsearch%5D%5Bvalue%5D=&columns%5B4%5D%5Bsearch%5D%5Bregex%5D=false&order=asc&start=0&length=10&search%5Bvalue%5D=honda1&search%5Bregex%5D=false&page=0&size=10&sort=id&_=1524671861781
答案 0 :(得分:1)
之后
d.order = d.order[0].dir;
添加
d.search=d.search.value;
否则你所有的字段都有字段搜索...