I want to use paging in my Spring controller and in my view where I use datatables. In the controller I use a Spring data repository with paging support:
@GetMapping("my/url")
public String listAll(
Model model,
@PageableDefault(size=200, sort="name") Pageable pageable)
{
Page<MyEntity> page = repository.findAll(pageable);
model.addAttribute("page", page);
return "path-to-html-resource";
}
In my view I don't know how to use the Page object properly.
// this is how I extract the data from the controller (thymeleaf style)
var datatableRows = /*[[${page.getContent()}]]*/'';
$('#mainTable').DataTable({
"paging" : true,
"serverSide" : true,
"ajax" : function (data, callback, settings) {
// this is definitely not the way to go
// but so far was the only way I got some data into my table
var myObject = new Object();
myObject.aaData = datatableRows;
callback(myObject);
},
"info" : true,
"columns": [
{ "data": "name"},
{ "data": "validFrom"},
{ "data": "validTo"},
{ "data": "amount"},
{ "data": "currency"}
]
});
Also, how do I have to write the ajax part of the datatables definition so I can pass the Spring pageable object back and forth? And how to I wire the datatable buttons to the paging index?