2014-04-05 80 views
0

我是新來的ajax/javascript/jquery,並且試圖將頁面渲染到包含我的窗體的頁面上。我得到它來渲染,但當它做到它的時候,它會進入我的表單頁面,除非它實際加載頁面,忽略佈局並刪除我的表單。在cakephp中的Ajax沒有按預期渲染頁面

這裏是我的控制器代碼:

<?php 
class PlayersController extends AppController{ 
     public $components = array('RequestHandler'); 
     public function listall() { 
      $this->set ('players', $this->Player->find ('all')); 
     } 

     public function index() { 
      if (!empty($this->request->data)) { 
       $this->Player->create(); 
        if ($this->Player->save ($this->request->data)){  
         $this->set ('players', $this->Player->find ('all')); 
         $this->render('listall', 'ajax');   
        } 
      } 
     } 
    }?> 

這裏是我的表單視圖:

<?php 
     echo $this->Form->create(); 
     echo $this->Form->input('name',array('id' => 'name')); 
     echo $this->Js->submit('Save', array(
     'before' => $this->Js->get('#inprogress')->effect('fadeIn'), 
     'success' => $this->Js->get('#inprogress')->effect('fadeOut'), 
     'update' => '#success' 

     ));  
     echo $this->Form->submit('normal submit'); 
     echo $this->Form->end(); 
?> 
     <div id="inprogress" style="display: none;background-color: 
       lightblue">Saving in progress...</div> 

這是我的觀點被渲染:

 <p style="background-color: lightblue">Players</p> 
     <table> 
     <tr> 
      <td>Name</td> 
     </>tr 
       <?php foreach ($players as $players): ?> 
       <tr> 
        <td><?php echo h($players['Player']['name']); ?>&nbsp;</td> 
       </tr> 
        <?php endforeach; ?> 
     </table> 

這是我在我的佈局中帶來的jquery:

echo $this->Html->script('jquery-2.1.0.min'); 
    echo $this->fetch('meta'); 
    echo $this-Js->writeBuffer(array('cache' => TRUE));  

我一直在試圖弄清楚如何實際渲染視圖列表中的視圖列表而不實際重新加載頁面。現在虧本了。

回答

0

很難理解您在使用您提供的信息時實際上在做什麼。試試在你這個 -

控制器

<?php 
class PlayersController extends AppController{ 
     public $components = array('RequestHandler'); 
     public function listall() { 
      $this->layout = 'ajax'; 
      $this->set ('players', $this->Player->find ('all')); 
     } 

     public function index() { 
      if (!empty($this->request->data)) { 
       $this->Player->create(); 
        if ($this->Player->save ($this->request->data)){  
         $this->Session->setFlash('Congrats');  
        } 
      } 
     } 
    }?> 
+0

謝謝,這是什麼一樣,雖然是呈現索引視圖,所以我最終有兩種形式。 我想要做的是:我現在在我的表單上列出了所有玩家(listall),我想將玩家添加到列表中,然後呈現索引中所有玩家的列表。 因此,當我使用js保存而不是提交時,基本上將列表視圖顯示在索引視圖上。 – user3298823