【问题标题】:EXTJS JsonStore do not loadEXTJS JsonStore 不加载
【发布时间】:2012-06-12 15:57:08
【问题描述】:

我知道这将是重复。但我已经尽力解决它,一切都是徒劳的。我什至无法填充一个简单的网格面板!!!显然我是 extjs 的新手。

我的js代码如下:

Ext.onReady(function(){
    var store = new Ext.data.JsonStore({        
        url: 'compare.php',             
                fields: ['name', 'area']
               });      

    store.load();
    var grid = new Ext.grid.GridPanel({
        store: store,
        columns: [
        {header: 'Name', width: 100, sortable: true, dataIndex: 'name'},
        {header: 'Position', width: 100, sortable: true, dataIndex: 'area'}          
        ],
        stripeRows: true,
        height:250,
        width:500,
        title:'DB Grid'
    });
    grid.render('db-grid');

});

我的php如下:

<html>
<head>
</head>
 <body bgcolor="white">
  <?php
  $link = pg_Connect('host=my_host port=5432 dbname=da_name user=postgres password=my_password');
  $sql = "SELECT name, area FROM \"TABLE\" LIMIT 10";
    if (!$link) {
        echo "error";
    } else {
    $result = pg_query($link, $sql);
    $rows = array();
    $totaldata = pg_num_rows($result);
    while($r = pg_fetch_assoc($result)) {
        $rows[] = $r;
    }
    //echo json_encode($rows);
    //echo json_encode(array('country' => $rows));
    echo '({"total":"'.$totaldata.'","country":'.json_encode($rows).'})';
    }
  ?>
  </body>
</html>

而我的json如下:

[
    {
        "total": "10",
        "country": [
            {
                "name": "CULTIV",
                "area": "1.10584619505971e-006"
            },
            {
                "name": "CULTIV",
                "area": "2.87818068045453e-006"
            },
            {
                "name": "CULTIV",
                "area": "6.96120082466223e-007"
            },
            {
                "name": "CULTIV",
                "area": "1.17171452984621e-007"
            },
            {
                "name": "CULTIV",
                "area": "1.25584028864978e-006"
            },
            {
                "name": "CULTIV",
                "area": "5.86309965910914e-007"
            },
            {
                "name": "CULTIV",
                "area": "4.12220742873615e-007"
            },
            {
                "name": "CULTIV",
                "area": "7.59690840368421e-006"
            },
            {
                "name": "CULTIV",
                "area": "2.47360731009394e-007"
            },
            {
                "name": "CULTIV",
                "area": "4.04940848284241e-005"
            }
        ]

我需要为此设置任何代理吗?实际上,我的应用程序运行在与数据库不同的服务器上。

【问题讨论】:

    标签: php json extjs extjs3


    【解决方案1】:

    我没有使用PostgreSQL 的经验,但我建议您在这种情况下使用pg_fetch_object() 函数而不是pg_fetch_assoc()

    试试这样的:

    while($obj = $result->pg_fetch_object()) {
    $rows = $obj;
    }    
    echo '({"total":"'.$totaldata.'","country":'.json_encode($rows).'})';
    

    【讨论】:

      【解决方案2】:

      您的 JSON 格式不正确。 Ext.data.JsonReader(使用 JsonStore 时自动配置)需要一个带有 root 节点的 JSON 对象,defined in your reader config。如果 root 未定义,读者应该抛出异常。

      所以为你的 JsonReader 定义一个根:

      var store = new Ext.data.JsonStore({        
          url: 'compare.php',
          root: 'country', // required!
          fields: ['name', 'area']
      });
      

      您的 JSON 应该如下所示:

      {
          "total": 10,
          "country": [{
              "name": "CULTIV",
              "area": "6.96120082466223e-007"
          },{
              ...
          }]
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-12-11
        • 2011-06-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-10-17
        • 2012-11-03
        相关资源
        最近更新 更多