简体   繁体   English

Cakephp,使用Js助手删除动作并渲染Ajax

[英]Cakephp, delete action with Js helper and render ajax

I'm using a Js Helper so i can be able to delete one record from my table. 我正在使用Js Helper,因此我可以从表中删除一条记录。 To do that in my view named as 'myclients' i use a link called "Remove" so when i click it will remove one record from my table and will use ajax to update the table without refresh my page. 为此,在名为“ myclients”的视图中,我使用了一个名为“删除”的链接,因此当我单击它时,它将从我的表中删除一条记录,并将使用ajax更新表而不刷新我的页面。

<?php echo $this->Js->link(
                    'Remove', 
                    '/Users/remove/'.$u['User']['id'].'/page:'.$this->Paginator->counter(array('format' => '%page%')), 
                    array('target' => '_self', 'update' => '#my_listview', 'escape' => false, 'confirm' => 'Are you sure?')); 
                    ?>

Following my controller i use the following code: 按照我的控制器,我使用以下代码:

public function remove($id=null){

                $this->User->id = $id;
        if (!$this->User->exists()) {
            $this->Session->setFlash(__('invalid user'), 'default', array ('class' => 'primary alert'));

            throw new NotFoundException(__('invalid user'));
        }

                if ($this->User->delete()) {
            $this->Paginator->settings = array(
                'limit' => 10,
                'order' => array(
                    'User.id' => 'asc'
                )
            );          
            $this->set('Users', $this->paginate('User'));
            $this->render('myclients', 'ajax');
        }

This seems to work fine, but just only one time,this it will remove and update my table, but when i click the second time in other record, it will remove but it wont update my table it will refresh my page and show a page blank page only with my table all css doesn't seems to load. 这似乎工作正常,但是只有一次,它将删除并更新我的表,但是当我第二次单击其他记录时,它将删除但不会更新我的表,它将刷新我的页面并显示一个页面只与我的表的空白页所有css似乎没有加载。 Notice that the confirm question does it show up only in the first time. 请注意,确认问题仅在第一次出现。

Thanks in advance. 提前致谢。

You can see my view on github . 您可以在github上看到我的观点。 Notice that the code that i put on stackoverflow is translated with diferent names, but follow the same logic. 请注意,我放在stackoverflow上的代码已翻译为不同的名称,但遵循相同的逻辑。

Maybe I'm wrong but let's see if I'm guessing what's happening. 也许我错了,但让我们看看我是否正在猜测正在发生的事情。

  1. You click on an ajax link 您单击一个ajax链接
  2. User is deleted 用户被删除
  3. you update a table or a list of users with the new data coming from myclients view 您使用来自myclients视图的新数据更新表或用户列表
  4. the update table (or list) has delete links, but once updated the links stop working 更新表(或列表)具有删除链接,但是一旦更新,链接将停止工作

If so the problem is that when you load a content via ajax cake does not put the javascript in it unless you tell it to do so. 如果是这样的话,那么问题是,当您通过ajax cake加载内容时,除非您告诉它这样做,否则不会将javascript放入其中。

In your myclient.ctp file put this code 在您的myclient.ctp文件中输入以下代码

if($this->layout == 'ajax')
    echo $this->Js->writeBuffer();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM