使用JQuery更新WebGrid中的行

问题描述 投票:0回答:1

发现问题:只需要用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;
    });
javascript jquery html html-table webgrid
1个回答
1
投票

你已经将row设置为$(this),这是你的情况代表$("#event-edit-btn")(顺便说一下,我建议使用类作为标识符,但这不是问题)。稍后您用新的<tr>集替换您的实际按钮,但您实际需要做的是遍历该按钮的tr父级并替换它。

将您的实时处理程序更改为

$("#event-edit-btn").live("click", function () {

       var url = $(this).attr('controller');
       var row = $(this).closest('tr'); //or use some #id or .class assigned to that element
       var id = $(this).attr('objectid');

       $("#edit-event-dialog")
           .data('id', id)
           .data('url', url)
           .data('row', row )
           .dialog('open');

       event.stopPropagation();
       return true;


});
© www.soinside.com 2019 - 2024. All rights reserved.