首先,对不起我的英语不太好。
问题:我正在尝试使用 zf2 运行 extJS 网格示例。我正在使用这个例子。
我需要做什么:
- 在php中创建对象数组
- json_encode(这个数组)
- 在 extjs 脚本中轻松接受 json 数据!但我有问题。
如何在 zend framework2 中正确发送 json 数据?在示例中,有一个“data.php”文件,其中创建并输出了 json 输出。如何在zf2中制作相同的内容?
我尝试了接下来的步骤。jsonAction()
我使用以下内容在 Controller 中创建了新操作:
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(制作控制器等)的假设可能是不对的。当然,我是新手。所以请建议正确的方法来做到这一点。