发现问题:只需要替换row.replaceWith
为row.parent().parent().replaceWith()
.
在模式对话框中单击提交按钮后,我正在尝试使用 JQuery 更新 WebGrid 行,但更新的数据只是附加最后一列,而不是我想要的整行。
假设我希望表格在更新后看起来像这样:
ID - Name - Phone number
但是我的代码在更新后看起来像这样:
ID - Name - ID - Name - Phone number
因为它只是用更新数据的最后一列中的新表替换最后一列。
我得到了正确的数据作为输出,但在行中的错误位置。
请帮忙!:)
这是Javascript代码:
$(function () {
$("#edit-event-dialog").dialog({
resizable: false,
height: 300,
modal: true,
autoOpen: false,
open: function (event, ui) {
var objectid = $(this).data('id');
$('#edit-event-dialog').load("/Events/CreateEditPartial", { id: objectid });
},
buttons: {
"Save": function () {
var ai = {
EventID: $(this).data('id'),
Name: $("#Name").val(),
Phone: $("#Phone").val()
};
var json = $.toJSON(ai);
var row = $(this).data('row');
$.ajax({
url: $(this).data('url'),
type: 'POST',
dataType: 'json',
data: json,
contentType: 'application/json; charset=utf-8',
success: function (data) {
var grid = $(".pretty-table");
row.replaceWith('<tr><td>' + data.ev.EventID + '</td><td>' +
data.ev.Name + '</td><td>' + data.ev.Phone + '</td></tr>');
},
error: function (data) {
var data = data;
alert("Error");
}
});
$(this).dialog("close");
},
Cancel: function () {
$(this).dialog("close");
}
}
});
$("#event-edit-btn").live("click", function () {
var url = $(this).attr('controller');
var row = $(this);
var id = $(this).attr('objectid');
$("#edit-event-dialog")
.data('id', id)
.data('url', url)
.data('row', row)
.dialog('open');
event.stopPropagation();
return true;
});