【问题标题】:Add a table row in webgrid with pagination problem?在有分页问题的 webgrid 中添加表格行?
【发布时间】:2011-11-02 18:28:09
【问题描述】:

我正在尝试向使用 JQuery 启用分页的 MVC3 WebGrid 添加一行。

当我添加新行时,它作为最后一行正确插入,但是当我的 WebGrid 中有超过 1 页时出现问题。

在最后一个 WebGrid 页面的最后一行插入的新行被插入到第一个 WebGrid 页面。

Javascript 代码:

$("#add-category-dialog").dialog({
        resizable: false,
        height: 300,
        modal: true,
        autoOpen: false,
        open: function (event, ui) {
            var objectid = 0;
            $('#add-category-dialog').load("/Categories/CreateEditPartial", { id: objectid });
        },
        buttons: {
            "Save": function () {
                var ai = {
                    categoryID: $(this).data('id'),
                    Name: $("#Name").val()
                };
                var json = $.toJSON(ai);

                $.ajax({
                    url: $(this).data('url'),
                    type: 'POST',
                    dataType: 'json',
                    data: json,
                    contentType: 'application/json; charset=utf-8',
                    success: function (data) {
                        $('.pretty-table tr:last').after('<tr><td>....</td></tr>');
                    },
                    error: function (data) {
                        var data = data;
                        alert("Error");
                    }
                });
                $(this).dialog("close");
            },
            Cancel: function () {
                $(this).dialog("close");
            }
        }
    });

$("#add-category-btn").live("click", function () {
    var url = $(this).attr('controller');

    $("#add-category-dialog")
        .data('url', url)
        .dialog('open');

    event.stopPropagation();
    return true;

});

控制器:

[HttpPost]
public ActionResult Create(Category Category)
{
    if (ModelState.IsValid)
    {
        context.Categories.Add(Category);
        context.SaveChanges();
    }

    return Json(new { Success = Category.CategoryID > 0, Category });
}

查看:

var grid = new WebGrid(Model, ajaxUpdateContainerId: "category-grid", canPage: true, rowsPerPage: 30);

@grid.GetHtml(
        tableStyle: "pretty-table",
        headerStyle: "ui-widget-header",
        columns: grid.Columns(
        grid.Column("CategoryID", "Id", canSort: true, format: @<b>@item.CategoryID</b>),
        grid.Column("Name", "Name", canSort: true, format: @<b>@item.Name</b>)
        )
    )

【问题讨论】:

    标签: javascript jquery ajax asp.net-mvc-3 webgrid


    【解决方案1】:

    通过以下行,您将添加新行作为当前页面的最后一行。

    $('.pretty-table tr:last').after('<tr><td>....</td></tr>');
    

    带有分页的WebGrid,只显示特定页面的行。因此,生成的最终 html (table >tr) 将仅适用于该特定页面。因此,上面的 jQuery 行在生成的 html 中找到最后一行并在其后插入行。在您的情况下,可能是第一页。尝试导航到最后一页(或第一页和最后一页之间的任何一页),然后添加新行,看看是否是这样。

    控制器代码看起来没问题,因此您不应在 ajax 调用成功时做任何事情。在上面的行注释并测试您的代码。添加新行并导航到最后一页。您的新行应该会显示在那里。

    【讨论】:

      猜你喜欢
      • 2013-05-09
      • 1970-01-01
      • 2011-05-10
      • 2013-03-17
      • 2011-08-12
      • 2022-10-19
      • 2011-10-26
      • 2012-11-30
      • 1970-01-01
      相关资源
      最近更新 更多