【问题标题】:jqgrid json reader for geoJson用于geoJson的jqgrid json阅读器
【发布时间】:2011-10-28 18:22:51
【问题描述】:

我有一个 PHP 脚本,可以从数据库中提取有效的 GeoJSON 数据。现在我想用 jqgrid 准备这些数据的网格视图,但我无法为 javascript 代码找出正确的 jsonReader。

这是我的 GeoJSON 输出:

{"type":"FeatureCollection",
"total":0, 
"page":1, 
"records":117, 
"features":[
     {"geometry":{"type":"Point","coordinates":[12.3,41.70052]},
      "type":"Feature",
      "properties":{
      "data":"2006-02-22",
      "specie":"S. coeruleoalba",
      "localita":"Ostia",
      "provincia":"Roma"
     },"id":0},
    {"geometry":{
     "type":"Point","coordinates":[15.26667,40.0502]},
     "type":"Feature",
     "properties":{
        "data":"2006-03-01",
        "specie":"S. coeruleoalba",
        "localita":"Golfo di Salerno",
        "provincia":"Salerno"
     },"id":1},
    {"geometry":{"type":"Point","coordinates":[14.88333,40.56692]},
     "type":"Feature",
     "properties":{
        "data":"2006-03-03",
        "specie":"S. coeruleoalba",
        "localita":"Battipaglia",
        "provincia":"Salerno"
    },"id":2}

]}

使用此阅读器,我的网格显示正确的行数 (117) 和页数,但单元格为空

jsonReader : { 
    root: "features", 
    page: "page", 
    total: "total", 
    records: "records", 
    repeatitems: false, 
    cell: "properties", 
    id: "id"
}

有人可以帮我写一个工作阅读器吗?提前致谢

【问题讨论】:

  • 您希望网格中有哪些列? properties 似乎大多不清楚。所有其他条目在properties 部分数据中是否具有相同的"specie""localita""provincia" 属性?仅从一项很难理解数据如何。您能否在 "features" 数组中包含更多项目?
  • 感谢您的回答,我在 Json 中添加了一些项目
  • 你仍然没有写出你需要在网格中有哪些列。

标签: php json jqgrid geojson jsonreader


【解决方案1】:

您的主要错误是您尝试使用cell: "properties",但如果使用repeatitems: false 属性,cell 属性将被忽略

你应该只使用

jsonReader: {
    root: "features",
    repeatitems: false
}

然后为每一列定义jsonmap 属性。例如,您可以使用以下列定义:

colModel: [
    {name: 'coordX', jsonmap:'geometry.coordinates.0', width: 75, sorttype: 'number',
        formatter: 'number', formatoptions: {decimalPlaces: 5}},
    {name: 'coordY', jsonmap:'geometry.coordinates.1', width: 75, sorttype: 'number',
        formatter: 'number', formatoptions: {decimalPlaces: 5}},
    {name: 'data', jsonmap:'properties.data', width: 90, align: 'center',
        sorttype: 'date', datefmt: 'm/d/Y',
        formatter: 'date', formatoptions: {newformat: 'm/d/Y'}},
    {name: 'specie', jsonmap:'properties.specie', width: 100},
    {name: 'localita', jsonmap:'properties.localita', width: 100},
    {name: 'provincia', jsonmap:'properties.provincia', width: 80}
]

因此,您将能够将 JSON 数据读取到以下网格:

(参见演示 here)。在您的 JSON 数据中,我仅将 total 值从 0 更改为 1,因为将 total 值小于 page 值是没有意义的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-03-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-02
    • 1970-01-01
    相关资源
    最近更新 更多