JQgrid在生成行时不显示数据

时间:2015-06-19 08:44:33

标签: json knockout.js jqgrid jsonreader

Jqgrid没有显示JSON数据,但行正在生成

服务器端代码:

public JsonResult Denominations()
{
.
.
int counter = 0;
var jsonData = new
{
total = result.UserObject.Count,
page = 1,
rows = (
      from p in result.UserObject
      select new
      {
            id = ++counter,
            cell = new string [] { 
                   p.CurrencyID.ToString(), 
                   p.DenominationID.ToString(), 
                   p.DenominationName.ToString(), 
                   p.DenominatorCount.ToString(), 
                   p.Multiplier.ToString(), 
                   p.TenderID.ToString()
                     }
                 }).ToArray()

            };
            return Json(jsonData, JsonRequestBehavior.AllowGet);
}

服务器端的数据如下: {"总":1,"页面":1,"行":[{" ID":1,"细胞& #34;:[" 1"" 1""便士"" 0"" 0.0100" " 1"]}]}

JavaScript代码:

$("#denominators").jqGrid({
        url: '/Denominations?tenderid=1&currencyid=1',
        contentType: "application/json",
        datatype: "json",
        jsonReader: {
            root: 'rows',
            page: 'page',
            total: 'total',
            repeatitems: false,
            cell: 'cell',
            id: 'id',
            userdata:'userdata'
        },
        mtype: "GET",
        colNames: ["CurrencyID", "DenominationID", "TenderID", "Multiplier", "DenominationName", "DenominatorCount"],
        colModel: [
            { name: "currencyid", width: 80, align: "center" },
            { name: "denominationid", width: 90, align: "center" },
            { name: "tenderid", width: 250 },
            { name: "multiplier", width: 250 },
            { name: "denominationname", width: 95 },
            { name: "denominatorcount", width: 95 },
        ],
        height: 'auto',
        loadonce: true,
        sortname: "DenominationID",
        sortorder: "desc",
        viewrecords: true,
        gridview: true,
        autoencode: true
    });

查看:

<table id="denominators" ></table>

View使用列标题创建网格,但是生成行但行没有任何数据int。

1 个答案:

答案 0 :(得分:0)

您使用了错误的jsonReader。准确地说属性repeatitems: false是错误的。这意味着rows数组中每个项目的格式都是

{
    "currencyid": "1",
    "denominationid": "1",
    "tenderid": 1,
    "denominationname": "Penny",
    "denominatorcount": "0",
    "multiplier": "0.0100"
}

您使用

{
    "id": 1,
    "cell": [
        "1",
        "1",
        "Penny",
        "0",
        "0.0100",
        "1"
    ]
}

代替。因此,您应该删除jsonReader,因为输入数据的格式与默认jsonReader相对应,但您仍需要重新排序网格列或更改放置在cell数组中的项目的顺序这样它就对应colModel中的列顺序。

补充说明:您对total使用了错误的值。它应该是页面的数量。顺便提一句,你使用loadonce: true。如果您可以从响应中删除"total":1,"page":1部分,只返回名为项的数组。您应该只选择与项目名称相同的列名称。