まず、私の英語が下手で申し訳ありません。
問題: zf2 を使用して extJS グリッドの例を実行しようとしています。この例を使用しています。
私は何をする必要がありますか:
- PHPでオブジェクトの配列を作る
- json_encode(この配列)
- extjs スクリプトで json データを簡単に受け入れる! しかし、私には問題がありました。
Zend Framework2でjsonデータをどのように正しく送信しますか? 例では、json出力が作成されて出力された「data.php」ファイルがありました。zf2で同じにする方法は?
次のステップを試しました。jsonAction()
このコンテンツでコントローラーに新しいアクションを作成しました:
public function jsonAction()
{
$view = new ViewModel();
$view->setTerminal(true); // turn off layout
$arr = array();
// creating data array to encode
$arr[] = new Student('Vlad', 'Sharikov');
$arr[] = new Student('Alina', 'Zaja');
$arr[] = new Student('Kam', 'Nurm');
$arr[] = new Student('Seva', 'Paren');
$arr[] = new Student('Dima', 'Glush');
//json output
echo '({"total":"'.count($arr).'","results":'.json_encode($arr).'})';
return $view;
}
ビュー json.phtml は空です:
したがって、json の出力は zf2/grid/json (zf2 は localhost ドメイン) で利用できます。
Json出力:
({"total":"5","results":[{"firstname":"Vlad","lastname":"Sharikov"},{"firstname":"Alina","lastname":"Zaja"},{"firstname":"Kam","lastname":"Nurm"},{"firstname":"Seva","lastname":"Paren"},{"firstname":"Dima","lastname":"Glush"}]})
ここで、extjs スクリプトを構成する必要があります。
Ext.onReady (function () {
// create the data store
var store = new Ext.data.SimpleStore({
totalProperty: 'total', // total data, see json output
root: 'results', // see json output
url: 'http://zf2/grid/json',
fields: [
{name: 'firstname'},
{name: 'lastname'}
]
});
// load data
store.loadData({params:{start: 0, limit: 5}});
// create the grid
var grid = new Ext.grid.GridPanel({
store: store,
columns: [
{header: 'Firstame', width: 200, dataIndex: 'firstname'},
{header: 'Lastname', width: 200, dataIndex: 'lastname'}
],
stripeRows: true,
height:180,
width:450,
title:'I504 group',
bbar: new Ext.PagingToolbar({
pageSize: 5,
store: store,
displayInfo: true,
displayMsg: 'Displaying topics {0} - {1} of {2}',
emptyMsg: "No topics to display"
})
});
// render grid to the grid-example element (see p array-grid.html)
grid.render('grid-example');
});
6行にurlフィールドがあります。グリッドを正しく描画するには、何を配置する必要がありますか? それが問題です。
または、json は私が行ったように作成する必要がある (コントローラーを作成するなど) という私の仮定が正しくない可能性があります。確かに、私は初心者です。したがって、これを行う正しい方法を提案してください。