There is such a table:
<table id="full_balance_table" class="table table-bordered table-striped"> <thead> <tr> <th>Date</th> <th>Category</th> <th>Title</th> <th>Type</th> <th>Value</th> </tr> </thead> <tbody> <if th:if="${not #lists.isEmpty(fullBalanceRecordList)}"> <tr th:each="overallBalanceRecord : ${fullBalanceRecordList}"> <td type="text" th:text="${overallBalanceRecord.date != null} ? ${#calendars.format(overallBalanceRecord.date, 'dd.MM.yyyy')} : null"></td> <td th:text="${overallBalanceRecord.category}"></td> <td th:text="${overallBalanceRecord.title}"></td> <td th:text="${overallBalanceRecord.sign}"></td> <td type="number" th:text="${overallBalanceRecord.value}"></td> </tr> </tbody> </table> Data can come from several users to it, therefore I want to update the table every 30 seconds without reloading the page itself. As a data source, just put the value in the map in the controller:
map.put("fullBalanceRecordList", balanceService.balanceRecordList());
I use thymeleaf and datatables. In datatables I tried to do something.
var table = $('#example').DataTable( { ajax: "/refresh" } ); setInterval( function () { table.ajax.reload(); }, 30000 ); But I get the error java.lang.IllegalArgumentException: Template name cannot be null or empty
The question is how to update the table every 30 seconds?
UPD @enzo
@RequestMapping(value = "/refresh", method = RequestMethod.GET) public String refreshBalanceTable() { String jsonResult = new Gson().toJson(balanceService.balanceRecordList()); jsonResult = "{ \"data\":".join(jsonResult).join("}"); return jsonResult; } When I do in datatables all the time it crashes https://datatables.net/manual/tech-notes/7
DataTablesupdates data via JSON .. You don’t need to transfer them to Thymeleaf in the model, just draw an empty table and add another URL from which you’ll pull JSON. Hint:@ResponseBody. - enzoDataTables will fire a specific error for the case where the request from the server is a valid return (200 Ok for example), but not valid JSON. Look at what's behind JSON and walk around it with a validator . Then, you return JSON as a string, and who will specify theContent-Type? - enzo