2012-06-12 82 views
1

我知道这将是一个重复。但我尽力解决这个问题,一切都是徒劳的。我甚至无法填充一个简单的网格面板!显然我是extjs的新手。EXTJS JsonStore不加载

我的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" 
      } 
     ] 

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

回答

1

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

尝试这样:

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

你的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" 
    },{ 
     ... 
    }] 
}