I'm using Spring Boot and JQuery to refresh a fragment in my web page. I call for the fragment to be replaced with:
$.get(urlString, function (fragment) { // get from controller
$("#resultTable").replaceWith(fragment);
});
The fragment is then updated with a new table as a Thymeleaf template shown here:
<span id="resultTable">
<th:block th:if="${table} != null and ${tableHead} != null">
<table id="main" class="display">
<thead>
<tr>
<th:block th:each="columnHeader: ${tableHead}">
<td th:text="${columnHeader}"></td>
</th:block>
</tr>
</thead>
<tbody>
<th:block th:each="row : ${table}">
<tr>
<th:block th:each="column : ${row}">
<td th:text="${column}"></td>
</th:block>
</tr>
</th:block>
</tbody>
</table>
</th:block>
<th:block th:if="${error} != null and ${error} == true">
<div class="errorBox">
<p th:text="${errorMsg}"></p>
</div>
</th:block>
To make it more readable, I'm then using JQuery to update the table to use Data Table
$('#main').DataTable({
paging: false
});
However, the table isn't reworked by Data Tables yet, because the table isn't actually updated when the above code block is run. I have these two JQuery statements run one after the other in the script, and if I delay the process for 5 seconds, it works as expected. Issue is, if the fragment takes longer than that to get, which it very well could since I'm processing SQL Queries, the table then doesn't get the Data Tables update. So, I want to find a way to halt the script until the fragment has actually loaded.
To be clear, the withReplace completes before the table is added to the page, so something else completes the replacement. I just need a way to check if the table has been updated again after the withReplace runs.