1

首先,对不起我的英语不太好。

问题:我正在尝试使用 zf2 运行 extJS 网格示例。我正在使用这个例子。

我需要做什么:

  1. 在php中创建对象数组
  2. json_encode(这个数组)
  3. 在 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(制作控制器等)的假设可能是不对的。当然,我是新手。所以请建议正确的方法来做到这一点。

4

2 回答 2

1

似乎你得到了它的工作,但是,如果你有兴趣以更 ZF2 的方式做事,你可以执行以下操作:

1) 将 ViewJsonStrategy 添加到您的 view_manager 配置 module.config.php:

<?php
'view_manager' => array(
     // ...
    'strategies' => array(
        'ViewJsonStrategy',
    )
),

2)在您的控制器中,只需构建您想要的结构,并返回一个 JsonModel:

<?php
namespace Application\Controller;

use Zend\View\Model\JsonModel;

class SomeController {

    public function someAction(){
        $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');

        return new JsonModel(array('total'=>count($arr), 'results' => $arr));

    }
}
于 2013-12-08T22:00:52.003 回答
0

问题解决了。谢谢!

首先,创建商店:

var store = new Ext.data.JsonStore({
    totalProperty: 'total', // total data, see json output
    root: 'results',    // see json output
    url: './json',
    fields: [
        'firstname', 'lastname'
    ]
});

1) JsonStore,不是 SimpleStore;2)字段:'firstname','lastname'是engouht(不需要{...})

其次,不是!:store.loadData({params:{start: 0, limit: 5}}); 这个没问题:store.load({params:{start: 0, limit: 5}});

于 2014-04-08T19:35:27.497 回答