3

我在 jqGrid 中使用 rowNum 时遇到问题。我试图让网格只加载我在 rowNum 中指定的行数。目前,网格正在加载数组中的所有数据。

这是我的网格:

$(function () {
    var width = $(window).width() - 50;
    $("#category_grid").jqGrid({
        datatype: "local",
        width: width,
        height: "auto",
        search: true,
        autowidth: false,
        shrinkToFit: true,
        colNames: ['ID', 'Name', 'Abbr.', 'Last Update', 'Last Update User', 'Active?', 'Edit'],
        colModel: [
            { name: 'ID', width: 5, align: 'center', sorttype: 'int' },
            { name: 'Name', width: 15, align: 'left' },
            { name: 'Abbreviation', width: 10, align: 'left' },
            { name: 'LastUpdateDateTime', width: 8, align: 'left', formatter: dateFix, sorttype: 'date' },
            { name: 'LastUpdateUser', width: 15, align: 'left', sorttype: 'string' },
            { name: 'Active', width: 5, align: 'center', formatter: activeFix },
            { name: 'Edit', width: 5, align: 'center' },
        ],
        rowNum: 20,
        rowList: [20, 50, 100, 1000, 100000],
        viewrecords: true,
        pager: "#gridpager",
        sortname: "ID",
        sortable: true,
        ignoreCase: true,
        headertitles: true,
        sortorder: "desc",
        onSelectRow: function (rowId)
        {
            var id = $("#category_grid").getCell(rowId, 'ID');
            document.location.href = '/Administration/EditCategoryRecord/'+ id;
        },
    }).navGrid("#gridpager", { edit: false, add: false, del: false }, {}, {}, {}, { multipleSearch: true, multipleGroup: false, showQuery: false, recreateFilter: true });
    $("#category_grid").jqGrid('filterToolbar', { stringResult: true, searchOnEnter: true, defaultSearch: 'cn' })
    setTimeout(function () { getCategories(); }, 200);
});

function getCategories() {
    var data;
    var request = $.ajax({
        url: "@Url.Action("GetCategories", "Administration")",
            type: "GET",
            cache: false,
            async: true,
            data: data,
            dataType: "json",
        });
    request.done(function (orders) {
            var thegrid = $("#category_grid");
            thegrid.clearGridData();
            setTimeout(function () {
                for (var i = 0; i < orders.length; i++) {
                    thegrid.addRowData(i + 1, orders[i]);
                }
            }, 500);
            //alert(thegrid.jqGrid('getGridParam', 'rowNum'));
            thegrid.trigger("reloadGrid");
            //alert(thegrid.jqGrid('getGridParam', 'rowNum'));

        });
        request.fail(function (orders) {
            alert("The request in the getCategories function failed. The grid will not be filled.");
        });
}

这是向网格发送数据的控制器操作:

    public JsonResult GetCategories()
    {
        // Holds all of the Categories as an array to fill the jqGrid
        object data;

        // Holds all of the Categories in the db
        List<Category> Categories;

        // Holds all of the Categories in the db with Usernames as a string instead of an int ID
        List<Category> CategoriesWithUserNames = new List<Category>();

        // Get all of the current Categories
        using (TicketDeskContext context = new TicketDeskContext())
            Categories = context.Categories.ToList();

        // For every Category, add to the List of Categories with Usernames as a string
        foreach (Category Category in Categories)
        {
            string LastUpdateUser = Functions.GetUserNameByID(Category.LastUpdateUserID);
            CategoriesWithUserNames.Add(new Category(Category.ID, Category.Active, Category.Name, Category.Abbreviation, Category.LastUpdateDateTime, LastUpdateUser));
        }

        // Convert the filtered Category List to the data array
        data = CategoriesWithUserNames.ToArray();

        // Return the filtered Category List
        return Json(data, JsonRequestBehavior.AllowGet);
    }

我在这里做错了什么吗?当网格加载时,它不会在 20 处停止(因为我将 rowNum 设置为 20),它只是加载所有数据(目前是 27 个类别)。

4

2 回答 2

3

我通过触发 setTimeout 内部的网格重新加载解决了这个问题。

function getCategories() 
{
    var data;

    var request = $.ajax({
        url: "@Url.Action("GetCategories", "Administration")",
            type: "GET",
            cache: false,
            async: true,
            data: data,
            dataType: "json",
        });

    request.done(function (orders) {
            var thegrid = $("#category_grid");
            thegrid.clearGridData();
            setTimeout(function () {
                for (var i = 0; i < orders.length; i++) 
                {
                    thegrid.addRowData(i + 1, orders[i]);
                }
                // Triggering a grid reload here loads the grid with the number specified in rowNum
                thegrid.trigger("reloadGrid"); 
            }, 500);
        });
        request.fail(function (orders) 
        {

        });
}
于 2013-10-04T02:45:05.923 回答
1

在 rowNum:20 之后添加 loadonce:true。这应该解决分页问题。

于 2013-09-26T03:17:57.697 回答