Cake php 与 Extjs 通过可编辑网格更新、插入和删除

发布于 2024-12-02 02:15:34 字数 212 浏览 0 评论 0原文

我想通过可编辑网格使用 extjs 3.2 开发蛋糕 php。

在网格中我们可以进行插入、更新和删除操作。

当我们从“电影”模型文件获取数据并在

json 文件中的 view/movie/json 中创建一个 json 文件时,我得到了数据,但

如果我想在网格中显示数据,那么这个 json 文件数据如何在网格存储中获取。

谢谢。

I want to develop cake php with extjs 3.2 through editable grid.

in grid we can insert and update and delete operation.

when we get data from "movie" model file and create one json file in view/movie/json

in json file i got data but

if i want to show data in grid then this json file data how can i take in grid store.

Thankyou.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

初见你 2024-12-09 02:15:34

在工作中,我们使用 CakePHP 和 ExtJS 进行了大量工作。

我们单独使用 ExtJS 的视图只调用包含该视图的特定 javascript 文件,以及一个用于渲染 ExtJS 模块的 div。

如果您在布局的头部使用 您可以使用

<?php echo $this->Html->script('relative/path/from/js/folder.js', array('inline' => false)) ?>

In Ext 轻松地从视图内部附加单个 javascript 文件,您可以需要将网格设置为使用 Ext.data.Store 的实例,通过存储及其依赖项(您还需要代理、读取器和写入器)您可以发送数据来回你的 CakePHP控制器。

我们更喜欢使用 JSON 来存储数据,因为它很容易在我们需要的地方进行操作和保存。

我们的基本设置通常如下所示。

在 ExtJS

var proxy = new Ext.data.HttpProxy({
    api: {
        // these will map to cakephp controller actions
        create:  { url: '/cake_controller/create',  method: 'POST' },
        read:    { url: '/cake_controller/read',    method: 'GET'  },
        update:  { url: '/cake_controller/update',  method: 'POST' },
        destroy: { url: '/cake_controller/destroy', method: 'POST' }
    }
});

var fields = Ext.data.Record.create([
    // this will map to your database fields (or at least the ones your
    // returning from cakephp to put in your grid
    { name: 'id', type: 'int' },
    { name: 'another_model_field' },
    { name: 'created', type: 'date', dateFormat: 'Y-m-d H:i:s' },
    { name: 'modified', type: 'date', dateFormat: 'Y-m-d H:i:s' },
]);

var reader = Ext.data.JsonReader({
    root: 'controllerName' // this is the root node of the json cakephp is replying with
}, fields);

var writer = Ext.data.JsonWriter({
    encoded: true,
    writeAllFields: true
});

var store = Ext.data.Store({
    storeId: 'myStore',
    proxy: proxy,
    reader: reader,
    writer: writer,
    autoSave: true,
    autoLoad: true
});

然后在你的网格中,将你的商店设置为你刚刚创建的商店

var grid = Ext.grid.GridPanel({
    store: store,
    ...
    ...
    ...
});

任何时候商店发生变化,它都会调用相关的 api 方法(创建、读取、更新、销毁),该方法映射到你的 CakePHP 控制器操作。在您的操作中,您可以通过 $this->params['form'] 访问数据。如果您使用 Json,就像我们一样,您可以使用 json_decode($this->params['form'], true) 将其解码为关联数组,以便在 CakePHP 端轻松操作/持久化事物。

您可以从 CakePHP 操作中返回某些内容,以便 ExtJS 知道保存/更新/等是否成功。我们通常只返回以 Json 编码的 array('success' => true) 。

我希望这可以帮助您入门,需要理解的内容很多,但并不是大量的工作,只是大量的打字。做过几次之后就很容易记住了。

每当您使用 ExtJS 时,请记住 docs 是您的朋友!

At work we do extensive work with CakePHP and ExtJS.

Our views that use ExtJS alone only have calls to include the specific javascript file for that view, and one div to render the ExtJS module to.

If you are using <?php echo $scripts_for_layout ?> in the head of your layout you can easily append single javascript files from inside your view using

<?php echo $this->Html->script('relative/path/from/js/folder.js', array('inline' => false)) ?>

In Ext, you'll want to set up your grid to use an instance of Ext.data.Store, through the store and it's dependencies (you'll also need a proxy, reader, and writer) you can send data back and forth to your CakePHP controllers.

We prefer to use JSON for our data as it's easy to manipulate and persist where we need it.

Our basic setup usually looks like this.

In ExtJS

var proxy = new Ext.data.HttpProxy({
    api: {
        // these will map to cakephp controller actions
        create:  { url: '/cake_controller/create',  method: 'POST' },
        read:    { url: '/cake_controller/read',    method: 'GET'  },
        update:  { url: '/cake_controller/update',  method: 'POST' },
        destroy: { url: '/cake_controller/destroy', method: 'POST' }
    }
});

var fields = Ext.data.Record.create([
    // this will map to your database fields (or at least the ones your
    // returning from cakephp to put in your grid
    { name: 'id', type: 'int' },
    { name: 'another_model_field' },
    { name: 'created', type: 'date', dateFormat: 'Y-m-d H:i:s' },
    { name: 'modified', type: 'date', dateFormat: 'Y-m-d H:i:s' },
]);

var reader = Ext.data.JsonReader({
    root: 'controllerName' // this is the root node of the json cakephp is replying with
}, fields);

var writer = Ext.data.JsonWriter({
    encoded: true,
    writeAllFields: true
});

var store = Ext.data.Store({
    storeId: 'myStore',
    proxy: proxy,
    reader: reader,
    writer: writer,
    autoSave: true,
    autoLoad: true
});

Then inside your grid, set your store to the one you've just created

var grid = Ext.grid.GridPanel({
    store: store,
    ...
    ...
    ...
});

Any time the store is changed, it will call the related api method (create, read, update, destroy) which is mapped to your CakePHP controller actions. Inside your actions you can access the data via $this->params['form']. If you use Json, like we do you can decode that into an associative array using json_decode($this->params['form'], true) for easy manipulation/persistence on the CakePHP side of things.

You can return certain things from your CakePHP actions so ExtJS knows whether the save/update/etc was successful. We usually just return array('success' => true) encoded in Json.

I hope this helps you get started, it's a lot to take in but it's not a ton of work just a lot of typing. It's easy to remember once you've done it a few times.

Whenever you're working with ExtJS, remember the docs are your friend!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文