2011-09-02 81 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是什麼意思?你想做什麼? –