gpt4 book ai didi

jquery - jqGrid加载所有行,忽略rowNum

转载 作者:行者123 更新时间:2023-12-01 01:07:45 37 4
gpt4 key购买 nike

我在 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.");
});
}

这是向网格发送数据的 Controller 操作:

    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 个类别)。

最佳答案

我通过在 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)
{

});
}

关于jquery - jqGrid加载所有行,忽略rowNum,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18992619/

37 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com