I making a simple CRUD web application using spring boot(1.5.2.RELEASE) + thymeleaf(2.1.5) + datatables(1.10.13) + jQuery(3.1.1) + Bootstrap(3.3.7). Everything works fine, but the problem is to make it display links correctly after adding new row from form. (table_view_issue) And after I reload the page - it's OK. So, this is my table:
<table id="table_users" class="display" cellspacing="0" width="100%">
<thead>
<tr>
<th class="text-center">NAME</th>
<th class="text-center">AGE</th>
<th class="text-center">ADDRESS</th>
<th class="text-center">DELETE</th>
<th class="text-center">EDIT</th>
</tr>
</thead>
<tbody>
<tr class="text-center" th:each="user : ${users}">
<td th:text="${user.name}"></td>
<td th:text="${user.age}"></td>
<td th:text="${user.address}"></td>
<td><a th:href="'/delete/' + ${user.id}">Delete</a></td>
<td><a th:href="'/edit/' + ${user.id}">Edit</a></td>
</tr>
</tbody>
</table>
form for adding:
<form id="userForm" class="form-horizontal">
<div class="form-group">
<label class="col-sm-4 control-label" for="name">Name: </label>
<div class="col-sm-4">
<input class="form-control" id="name" name="name" type="text" th:minlength="1" th:maxlength="15"
th:required="required"/>
</div>
</div>
<div class="form-group">
<label class="col-sm-4 control-label" for="age">Age: </label>
<div class="col-sm-4">
<input class="form-control" id="age" name="age" type="number" th:min="1" th:max="130"
th:required="required"/>
</div>
</div>
<div class="form-group">
<label class="col-sm-4 control-label" for="address">Address: </label>
<div class="col-sm-4">
<input class="form-control" id="address" name="address" type="text" th:minlength="5"
th:required="required"/>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-4 col-sm-10">
<input class="btn btn-success" type="submit" value="Add"/>
<input class="btn btn-warning" type="reset" value="Reset"/>
</div>
</div>
</form>
script for datatables:
$(document).ready(function () {
$('#table_users').DataTable({
"lengthMenu": [[10, 25, 50, -1], [10, 25, 50, "All"]]
});
});
script to add user:
<script type="text/javascript">
$(document).ready(function () {
$("#userForm").submit(function (event) {
// Prevent the form from submitting via the browser.
event.preventDefault();
ajaxPost();
});
function ajaxPost() {
// PREPARE FORM DATA
var formData = {
name: $("#name").val(),
age: $("#age").val(),
address: $("#address").val()
};
// DO POST
$.ajax({
type: "post",
contentType: "application/json",
url: "/add",
data: JSON.stringify(formData),
dataType: 'json',
success: addData()
});
function addData() {
var deleteCell = '<td><a>Delete</a></td>';
var editCell = '<td><a>Edit</a></td>';
var rowAdded = $('#table_users').DataTable()
.row.add(
[
formData.name,
formData.age,
formData.address,
$(deleteCell).find('a').attr('href', '/delete/${user.id}'),
$(editCell).find('a').attr('href', '/edit/${user.id}')
]
).draw(false).node();
$(rowAdded).addClass('text-center');
}
// Reset FormData after Posting
resetData();
}
function resetData() {
$("#name").val("");
$("#age").val("");
$("#address").val("");
}
});
</script>
Is it possible to do that without reloading? Thanks!