我必須通過表格的刪除按鈕刪除數據庫元素。 在表中我展示了這些元素,當用戶點擊刪除按鈕時,我想顯示一個確認模式,然後調用Web服務來刪除元素。我使用的HTML,Thymeleaf,引導和JQuery 現在在我的表,我有:按鈕裏面的表格刪除一行元素
<tr th:each="version : ${versions}">
<td th:text="${version.name}"></td>
<td th:text="${version.releaseDate}"></td>
<td th:text="${version.Note}"></td>
<!-- Give me the size of the list. Each version has a lot of users store in a list named users -->
<td th:text="${#lists.size(version.users)}"></td>
<td th:if="${#lists.size(version.users)}==0"><button
type="button" class="btn btn-danger" id="deleteVersion"
data-toggle="modal" data-target="#deleteVersionModal"
data-toggle="tooltip">Delete</button></td>
<td th:if="${#lists.size(version.users)}!=0"><button
type="button" class="btn btn-danger" id="deleteVersion"
disabled>Delete</button></td>
</tr>
表中的每一個元素都有一個version.idClientVersion,這是在數據庫中的關鍵,所以我必須用這個值在我的web服務,但如何可以將它傳遞給我的模態,然後在JavaScript中我的ajax調用。
<div class="modal" id="deleteVersionModal">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal"
aria-label="Close">
<span aria-hidden="true">×</span>
</button>
<h4 class="modal-title">New version</h4>
</div>
<div class="modal-body">
<!-- form start -->
<div class="box-body">
Are you sure? The release version wiil be deleted permanently, you can't recover it.
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default pull-left"
data-dismiss="modal">Close</button>
<button id="cancelVersionButton" type="button" class="btn btn-primary">Upload
version</button>
</div>
</div>
<!-- /.modal-content -->
</div>
<!-- /.modal-dialog -->
</div>
這是我的Ajax調用
$("#cancelVersionButton").click(
function() {
//CSRF attribute for spring security
var token = $("meta[name='_csrf']").attr("content");
var header = $("meta[name='_csrf_header']").attr("content");
$('#deleteVersionModal').modal("hide");
jQuery.ajax({
type : "DELETE",
url : "/"+,
beforeSend:function(xhr) {
xhr.setRequestHeader(header, token);
waitingModal.showPleaseWait();
},
success: function(data,status,xhr){
//No exception occurred
if (data.status){
//Also the field are right(for e.g. form value)
if(data.success){
$('#versionsTable').load(document.URL + ' #versionsTable');
notifyMessage(data.result + " was deleted!", 'success');
}
else{
//code if there are some error into form for example
}
} else {
notifyMessage(data.result, 'error');
}
},
error: function(xhr,status,e){
window.location.href = "/ATS/500";
}
}).complete(function() {
//add timeout because otherwise user could see a too fast waiting modal
setTimeout(function(){
waitingModal.hidePleaseWait();
}, 1000);
});
});
要回答你的問題,你應該向我們展示如何Thymeleaf實際上呈現在HTML表格。 –
th:each和th:text用於渲染表格。我不明白,你需要什麼? – luca