2011-09-02 64 views
0

我creter js文件通过插入数据ExtJS的,并添加TBAR添加按钮,当点击一个blnak行添加在网格CakePHP的与数据库

电影控制器文件我写

function ext_item($id = null) { 
     if(!empty($this->data)) { 

      if($this->Movie->save($this->data)) 
      { 
       $this->set('success','true'); 
       $this->data = array(); 
       return; 
      } 
      else { 
       $this->set('success',"false"); 
       return; 
      } 
     } 
} 

如何通过这个js数据?

如何在数据库中插入数据?


在控制器文件

function create() { 
    $newData = json_decode($this->params['form'], true); // turn the incomin json into an array 
    $this->data = array(
      'Movie' => array(
      'date_' => $newData['date_'], 
      'notes' => $newData['notes'], 
      'asset_id' => $newData['asset_id'], 
      'maint_picture' => $newData['maint_picture'], 
      'maint_condition1' => $newData['maint_condition1'], 
      'maint_condition2' => $newData['maint_condition2'], 
      'maint_condition3' => $newData['maint_condition3'], 
      'maint_condition4' => $newData['maint_condition4'], 

     ) 
    ); 

    if ($this->Movie->save($this->data)) 
    { 
     $data['success'] = true; 
    } else { 
     $data['success'] = false; 
    } 
    $this->set('data', $data); 
    //$this->layout = 'ajax'; 
    return $this->render(null, null, '/movies/ext_item'); 
} 

然后在js文件

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

并添加行电网

tbar: [{ 
       text: 'Add Movie', 
       icon: 'images/table_add.png', 
       cls: 'x-btn-text-icon', 
       handler: function() { 
        grid.getStore().insert(0, new Movie({ 
         id: 0, 
         notes: 'New Movie', 
         asset: '' 

        })); 
        rowEditor.startEditing(0, true); 
       } 

      }] 

什么错。它不是在数据库中插入数据。

+0

Mayur,我发布了一个答案,但很明显你没有理解一些Ext和CakePHP的基础知识。你需要掌握这些基础知识,然后才能冒险进入你想要做的高级内容。在StackOverflow上获得答案意味着你永远不会** ** ** .. –

回答

2

你想要做什么是添加到使用ExtJS的电网。附加到您的网格的商店(如果您按照我最后一个问题的答案)将处理与服务器的交谈。

在ExtJS中,工具栏中向网格添加一行的按钮应该有一个处理程序。

var toolbar = Ext.Toolbar({ 
    // config options 
    handler: function() { 
     // in your handler you need to create a new record and insert it into your store 
     // if you followed my answer to your last question, you'll have setup a store with proxy, jsonreader, and jsonwriter. 

     // get the store component from Ext 
     var store = Ext.getCmp('idOfYourStore'), 
      NewRecord = Ext.data.Record.create(['name', 'genre', 'length']); // this array of column names should match the fields you specified for your JsonReader's fields 

     // now that you have your store component, and your new blank record. you can fill it in and add it to the store 
     var record = new NewRecord({ 
      name: 'Name of Movie', 
      genre: 'Genre of Movie', 
      length: '1:25:22' 
     }); 

     store.add(record); 
     store.commitChanges();    
    } 
}); 

调用add(如果自动保存设置为true,您的商店)后,它会自动调用链接到您安装在您的代理的API下“创造”你的CakePHP程序。它会将这个新记录的数据发送到该动作。

因此,如果您设置了创建代理指向/movies/create比您的MoviesController内部要设置create()行动。

create操作中,您需要检查$this->params['form']以获取来自ExtJS的收到数据。

function create() { 
    $newData = json_decode($this->params['form'], true); // turn the incomin json into an array 

    $this->data = array(
     'Movie' => array(
      'name' => $newData['name'], 
      'genre' => $newData['genre'], 
      'length' => $newData['length'] 
     ) 
    ); 

    if ($this->Movie->save($this->data)) { 
     $data['success'] = true; 
    } else { 
     $data['success'] = false; 
    } 

    return json_encode($data); 
} 

ExtJS的使文章后给PHP,预计JSON对象回来的对象的根与真,或假“成功”的关键。你需要使用json,所以你不能简单地使用$this->set并将它发送到你的视图。在这种情况下,我返回json_encoding字符串。

实际上你应该做的是在你的app_controller中包含Js帮手。然后创建一个名为ajaxreturn的元素。 /views/elements/ajaxreturn.ctp将包含一行。

<?php echo $this->Js->object($data) ?> 

对象负责将$data转换为json对象。它被用来代替json_encode,因为PHP4不支持json_encode。

现在你有这样的元素,在你的控制器,你可以重写它像这样...

function create() { 
    $newData = json_decode($this->params['form'], true); // turn the incomin json into an array 

    $this->data = array(
     'Movie' => array(
      'name' => $newData['name'], 
      'genre' => $newData['genre'], 
      'length' => $newData['length'] 
     ) 
    ); 

    if ($this->Movie->save($this->data)) { 
     $data['success'] = true; 
    } else { 
     $data['success'] = false; 
    } 

    $this->set('data', $data); 
    $this->layout = 'ajax'; 
    return $this->render(null, null, '/elements/ajaxreturn'); 
} 

要返回JSON字符串,只有JSON字符串。没有布局,没有HTML,只是字符串会抛出一个错误。

一旦你这样做,你的商店就会知道电话是否成功,如果是的话,它会在你的电网中保持一排。如果不是,它会删除温度。将它放入网格中。

+0

谢谢回复。但我没有完美。我按照你的步骤,但我不能在数据库中成功保存数据。 – mayur

+0

实际上如何在js中传递链接。意味着如何调用我们的控制器文件中的函数 – mayur

+0

@Mayur,我不确定你在问什么。来自你的“call”JS是什么意思?你想做什么? –