kokomo
kokomo

Reputation: 77

Update row in WebGrid with JQuery

FOUND THE PROBLEM: Just needed to replace row.replaceWith with row.parent().parent().replaceWith().


I'm trying to update a WebGrid row with JQuery after I've clicked a submit button in a modal dialog, but the updated data just append the last column, not the whole row as I want.

Let's say I want the table to look like this after the update:

ID - Name - Phone number

But with my code it looks like this after the update:

ID - Name - ID - Name - Phone number 

as it just replaces the last column with a new table within the last column with the updated data.

I'm getting the correct data as output, but in the wrong place in the row.

Please help! :)

Here is the Javascript code:

$(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;
    });

Upvotes: 0

Views: 3098

Answers (1)

Z. Zlatev
Z. Zlatev

Reputation: 4820

You have set row to $(this) which is your case represents $("#event-edit-btn") ( btw i suggest using classes as identifiers, but it's not a problem ). Later on you replace your actual button with the new <tr> set but what you actually need to do is traverse to the tr parent of that button and replace it.

Change your live handler to:

$("#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;


});

Upvotes: 1

Related Questions