Вы можете использовать эту структуру:
Контроллер :
@RequestMapping(value = "/my/url/list")
public ResponseEntity listAllTable(@RequestParam("draw") int draw,
@RequestParam("start") int start,
@RequestParam("length") int length) {
int page = start / length; //Calculate page number
Pageable pageable = PageRequest.of(
page,
length ,
new Sort(
Sort.Direction.DESC ,
"name"
)
) ;
Page<MyEntity> responseData = repository.findAll(pageable);
DataTable dataTable = new DataTable();
dataTable.setData(responseData.getContent());
dataTable.setRecordsTotal(responseData.getTotalElements());
dataTable.setRecordsFiltered(responseData.getTotalElements());
dataTable.setDraw(draw);
dataTable.setStart(start);
return ResponseEntity.ok(dataTable);
}
DataTable Class :
public class DataTable<T> {
private int draw;
private int start;
private long recordsTotal;
private long recordsFiltered;
private List<T> data;
// setter and getter ...
}
javascript :
$('#mainTable').DataTable({
"processing": true,
"serverSide": true,
"pageLength": 10,
"searching": false,
"info" : true,
"ajax": {
"url": "/my/url/list",
"method":"get",
"dataSrc": function (response) {
var data = response.data; // your data list
var all = [];
for (var i = 0; i < data.length; i++) {
var row = {
rows: response.start + i + 1,
name: data[i].name, // name ... ,
validFrom: data[i].validFrom,
validTo: data[i].validTo,
amount: data[i].amount,
currency: data[i].currency,
};
all.push(row);
}
return all;
}
},
"columns": [
{ "data": "name"},
{ "data": "validFrom"},
{ "data": "validTo"},
{ "data": "amount"},
{ "data": "currency"}
]
});