【问题标题】:jqGrid Sort or Search does not work with columns having json dot notationjqGrid 排序或搜索不适用于具有 json 点表示法的列
【发布时间】:2011-01-09 13:48:22
【问题描述】:

我有这个 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 是真还是假。
  • 如果我尝试搜索(在将 loadonce 设置为 true 之后)列 meta.status(其他没有点表示法的列也可以),那么它会抛出这样的 javascript 错误。

【问题讨论】:

  • 你试过不带引号的meta.status吗?
  • 如果你想让我在 colModel 中给出 meta.status 而没有 qutoes,它不会起作用,因为 Javascript 会抛出 meta.status 未定义的错误。

标签: javascript json syntax jqgrid


【解决方案1】:

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

只要改变第二列的定义,就可以解决这个问题

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

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

查看固定演示here

【讨论】:

  • 太棒了!非常感谢!这解决了问题,我太高兴了!此外,仅当我使用 loadonce: true 进行搜索(简单搜索或工具栏搜索)等时才会发生发布的 JS 错误(修复后现在不会发生)。注意:对于错误的 js 代码,没有引用 'amount' 表示抱歉
  • @rsmoorthy:欢迎您!我很高兴能帮助你!因为'meta.status' 的使用在名称上是合法的并在此处描述:例如trirand.com/jqgridwiki/…,我建议您将问题作为错误发布在trirand.com/blog/?page_id=393/bugs 论坛中。然后 jqGrid (Tony) 的开发者可以知道这个问题,并且可能会在下一个版本中修复它。
  • 当然,会提交错误。也是“jsonmap”仅使用一种方式 - 即。通过jqgrid提取json数据?当我对 meta.status 字段进行单元格编辑时,jqGrid 仅将字段/值作为“status”发送到服务器,而不是作为“meta.status” - 即在向服务器发送数据时它不是指 jsonmap。该文档似乎表明这仅在检索数据时使用。你怎么看?发送时也不应该使用吗?
  • @rsmoorthy:这种行为是绝对正确的。 index 将被发送到服务器以识别排序列。如果您没有为该列定义index,则将使用namejsonmap 仅用于读取 JSON 数据。
  • @Oleg:我有完全相同的问题,但是完全按照上面的答案使用 jsonmap 并不能为我呈现结果。我的问题完全相同,但属性名称不同。我应该创建一个单独的问题并为您发布吗?
【解决方案2】:

根据经验避免这个问题:

  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
    

    您需要它的地方。

【讨论】:

    猜你喜欢
    • 2013-01-04
    • 1970-01-01
    • 1970-01-01
    • 2015-08-01
    • 1970-01-01
    • 2011-07-29
    • 2013-10-13
    • 2017-05-26
    • 1970-01-01
    相关资源
    最近更新 更多