jqGrid排序或搜索不适用于具有json点表示法的列

时间:2011-01-09 13:48:23

标签: javascript json syntax jqgrid

我有这个jqGrid:

$("#report").jqGrid( {
        url:        '/py/db?coll=report',
        datatype:   'json',
        height:     250,
        colNames:   ['ACN', 'Status', 'Amount'],
        colModel:   [ {name:'acn', sortable:true},
                      {name:'meta.status', sortable:true},
                      {name:'amount'} ],
        caption: 'Show Report',
        rownumbers: true,
        gridview: true,
        rowNum: 10,
        rowList: [10,20,30],
        pager: '#report_pager',
        viewrecords: true,
        sortname: 'acn',
        sortorder: "desc",
        altRows: true,
        loadonce: true,
        mtype: "GET",
        rowTotal: 1000,
        jsonReader: {
            root: "rows",
            page: "page",
            total: "total",
            records: "records",
            repeatitems: false,
            id: "acn"
            }
     });

请注意,'meta.status'列是JSON点表示法,因此从服务器发送的数据是这样的:

{"page": "1", "total": "1", "records": "5", "rows": [ 
        {"acn":1,"meta": {"status":"Confirmed"}, "amount": 50},
        {"acn":2,"meta": {"status":"Started"}, "amount": 51},
        {"acn":3,"meta": {"status":"Stopped"}, "amount": 52},
        {"acn":4,"meta": {"status":"Working"}, "amount": 53},
        {"acn":5,"meta": {"status":"Started"}, "amount": 54} ] }

问题有两个:

  • 排序不适用于带点符号的列,此处为“meta.status”。它甚至没有在列标题上显示可排序的图标,即使单击标题也没有任何反应。无论loadonce是true还是false,排序都不起作用。
  • 如果我尝试搜索(在设置loadonce为true之后)列meta.status(没有点表示法的其他列可以),那么它会抛出这样的javascript错误。 alt text

2 个答案:

答案 0 :(得分:3)

将最后一列的定义从{name:amount}更改为{name:'amount'}后,我可以重现您的问题:“状态”的排序不起作用,但我看不到任何错误消息(请参阅{ {3}})。

可以解决从

更改第二列定义的问题
{name:'meta.status', sortable:true}

{name:'status', sortable:true, jsonmap: "meta.status"}

请参阅固定演示the demo

答案 1 :(得分:1)

作为避免此问题的经验法则:

  1. 确保您的nameindex值相同

    name: 'Date', index: 'Date',
    name: 'Clicks', index: 'Clicks',
    ...
    
  2. 确保设置类似

    的内容
    $("#jqGrid").setGridParam({datatype: 'local'}); 
    

    当您重新加载网格时 - 如果您正在使用它,则在重新加载时将其更正为“JSON” - 即

    $("#yourGridID").setGridParam({datatype: 'json'}).trigger("reloadGrid");
    
  3. 最后,请确保使用

    name: 'Date', index: 'Date', sortable:true
    

    你需要它的地方。