cakephp中的Ajax没有按预期呈现页面

时间:2014-04-05 03:40:36

标签: javascript php jquery ajax cakephp

我是ajax / javascript / jquery的新手,我正在尝试将页面呈现到包含我的表单的页面上。我得到它来渲染,但它什么时候进入我的表单页面,除了它实际加载页面,忽略布局并删除我的表单。

这是我的控制器代码:

<?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));      

我一直在试图弄清楚如何在视图索引中实际呈现视图listall而不实际重新加载页面。现在不知所措。

1 个答案:

答案 0 :(得分: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');       
                   }
            }
      }
  }?>