【发布时间】:2017-09-13 18:46:06
【问题描述】:
我有一张桌子,我正在使用jQuery Datatables。
图片:
场景:
如图所示,有一个Delete 链接。单击该链接时,将显示一个模式弹出窗口,询问用户是否真的要删除该项目。如果是,删除..如果没有..取消模态。
我想要什么:
当用户决定删除一个项目并确认它时。我想通过 ajax 将该项目的状态更改为“已删除”。我可以更改该值,但该值未显示在表中。我已经researched 这几天了,但似乎没有任何效果。
我的代码
<table id="Item-Table" class="table table-bordered">
<thead>
<tr>
<th class="text-center">
@Html.DisplayNameFor(model => model.AssetTag)
</th>
<th class="text-center">
@Html.DisplayNameFor(model => model.codeMakeModel.MakeModel)
</th>
<th class="text-center">
@Html.DisplayNameFor(model => model.codeStatu.Status)
</th>
<th></th>
</tr>
</thead>
<tbody>
@foreach (var item in Model)
{
<tr class="text-center">
<td>
@Html.ActionLink(item.AssetTag, "Edit", new { id = item.Id })
</td>
<td>
@Html.DisplayFor(modelItem => item.codeMakeModel.MakeModel)
</td>
<td class="changeStatus">
@Html.DisplayFor(modelItem => item.codeStatu.Status)
</td>
<td>
<a href="#" class="js-item-delete text-danger" data-item-id="@item.Id">Delete</a>
</td>
</tr>
}
</tbody>
</table>
@section scripts{
<script>
var settings = {};
settings.baseUri = '@Request.ApplicationPath';
var infoGetUrl = "";
if (settings.baseUri === "/projectonservername") {
infoGetUrl = settings.baseUri + "/api/itemsapi/";
} else {
infoGetUrl = settings.baseUri + "api/itemsapi/";
}
$(document).ready(function () {
var itemsTable = $("#Item-Table").DataTable({
"aoColumnDefs": [
{ "bSortable": false, "aTargets": [3] },
{ "bSearchable": false, "aTargets": [3] }
]
});
$("#Item-Table").on("click",
".js-item-delete",
function() {
var link = $(this);
bootbox.confirm({
title: "Delete Item?",
message: "Are you sure you want to delete this item?",
buttons: {
cancel: {
label: '<i class="fa fa-times"></i> Cancel'
},
confirm: {
label: '<i class="fa fa-check"></i> Confirm'
}
},
callback: function(result) {
if (result) {
toastr.options = {
timeOut: 5000
}
$.ajax({
url: infoGetUrl + link.data("item-id"),
method: "DELETE",
success: function (result) {
//itemsTable.cell(itemsTable.row(this), 2).data("Deleted");
//itemsTable.draw();
//itemsTable.reload();
console.log(itemsTable.cell(itemsTable.row(this), $('.changeStatus')).data());
itemsTable.cell(itemsTable.row(this), $('.changeStatus')).data("Deleted").draw();
console.log(itemsTable.cell(itemsTable.row(this), $('.changeStatus')).data());
toastr.success("Item successfully deleted");
},
error: function(jqXHR, textStatus, errorThrown) {
var status = capitalizeFirstLetter(textStatus);
console.log(jqXHR);
toastr.error(status + " - " + errorThrown, "Sorry, something went wrong.");
}
});
}
}
});
});
function capitalizeFirstLetter(string) {
return string.charAt(0).toUpperCase() + string.slice(1);
}
})
</script>
}
我收到了什么
在上面的代码中,特别是这些行:
console.log(itemsTable.cell(itemsTable.row(this), $('.changeStatus')).data());
itemsTable.cell(itemsTable.row(this), $('.changeStatus')).data("Deleted").draw();
console.log(itemsTable.cell(itemsTable.row(this), $('.changeStatus')).data());
我在更新单元格值之前记录单元格的值,然后更改单元格值,然后记录新的/更新的单元格值。
这是我在控制台中收到的内容:
但是表格没有更新,或者更确切地说.. 重绘自身以显示已删除.. 显示已删除的唯一方法是刷新页面,这违背了 ajax 的目的..
如何在不刷新页面的情况下让表格更新单元格值?
感谢任何帮助。
【问题讨论】:
-
您确定行选择器
itemsTable.row(this)中的this指的是您要查找的行吗?您是否尝试传递行 - ID 选择器? -
@DavidDomain
row-ID选择器? -
天哪,你给了我这个想法,现在它正在工作!在我有
var link = $(this);的地方,我添加了var row = $(this).parents("tr");.. 然后我添加了itemsTable.cell(itemsTable.row(row), $('.changeStatus')).data("Deleted").draw();,它成功了! -
干得好。我感觉您没有选择正确的行。看起来你现在明白了。 ;)
标签: javascript jquery ajax datatable