我的JqGrid从控制器加载数据,但它没有显示列表分页器。
在这种情况下,LINQ的查询返回15行,但只显示10行,无法导航到第2页以查看最后5行。
我做错了什么?拜托,有人可以帮助我吗?
查看
<script type="text/javascript">
jQuery(document).ready(function () {
jQuery("#list2").jqGrid({
url: '@(Url.Action("GetMet", "Meta"))',
datatype: "json",
data: "a",
colNames: ['Id', 'Nome da Loja', 'Mês', 'Ano', 'Meta Monetária', 'Meta Seguro', 'Meta Ticket Médio'],
colModel: [{ name: 'id', index: 'id', width: 20 },
{ name: 'nome', index: 'nome', width: 120, search: true, stype: 'text' },
{ name: 'mes', index: 'mes', width: 40 },
{ name: 'ano', index: 'ano', width: 40 },
{ name: 'metamonetaria', index: 'metamonetaria', width: 90 },
{ name: 'metaseguro', index: 'metaseguro', width: 90 },
{ name: 'metaticketmedio', index: 'metaticketmedio', width: 110 }],
rowNum: 10,
rowList: [10, 20, 30],
pager: '#pager2',
sortname: 'nome',
viewrecords: true,
sortorder: "desc",
caption: 'Metas referente a loja selecionada',
height: 240,
ondblClickRow: function (id) { $("#FUNCIONARIO").val(id); }
});
jQuery("#list2").jqGrid('navGrid', '#pager2', { edit: false, add: false, del: false });
控制器
public JsonResult GetMet(int page = 1, int rows = 10, string sord = "asc", string sidx = "Id")
{
var db = new DataContext().GetGenericRepository();
var totalFunc = db.GetAll<Funcionarios>().Count();
decimal totalPages = 0;
if (totalFunc > 0)
{
totalPages = totalFunc / rows;
totalPages = System.Math.Round(totalPages);
}
else
{
totalPages = 0;
}
if (page > totalPages)
{
page = Convert.ToInt32(totalPages);
}
var result = new
{
total = totalPages,
page = page,
records = totalFunc,
rows = (from metas in db.GetAll<Metas>()
select new
{
id = metas.METAID,
metamonetaria = metas.METAMONETARIA,
metaseguro = metas.METASEGURO,
metaticketmedio = metas.METATICKETMEDIO,
mes = metas.MES,
ano = metas.ANO,
nome = (from lojas in db.GetAll<Lojas>()
where lojas.LOJAID == metas.LOJAID
select lojas.NOME).First()
}).ToArray()
};
return Json(result, JsonRequestBehavior.AllowGet);
答案 0 :(得分:2)
您的totalPages
计算错误。首先,rows
和totalFunc
是整数,因此操作:
totalPages = totalFunc / rows;
无论totalPages
是小数,都是整数除法。在您的情况下,结果是1。
Math.Round()
也不合适,因为它会改变1.4(当你有14行,页面大小为10的情况下)为1(你希望它是2)。
您应该像这样更改此计算:
int totalPages = 0;
if (totalFunc > 0)
totalPages = (int)Math.Ceiling((decimal)totalFunc / (decimal)rows);
else
totalPages = 0;