开发者

Cake php with Extjs Through editable grid update,insert and delete

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

开发者_StackOverflow社区

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.


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!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜