微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

CakePHP Ajax分页

我目前正在尝试使用ajax分页列表.我想我很亲密.当我单击PrevIoUs,Next或单个页码时,正确的结果显示但div.postsPaging加载了分页结果以及Cake的default.ctp布局的页眉和页脚.我已尝试在displayPosts视图或displayPosts操作中将布局设置为ajax,但认布局根本不显示(在div内部或外部).

我的问题是,如何只使用分页结果而不是页眉,页脚或布局中的任何其他内容来加载postsPaging div.如何让ajax请求的结果不包括认布局的页眉和页脚?

附加到其中一个页面索引和Next按钮的jQuery事件是:

$("#link-196981051").bind("click",function (event) {$.ajax({dataType:"html",success:function (data,textStatus) {$("#postsPaging").html(data);},url:"\/AuthAdminCakeApp\/posts\/displayPosts\/page:2"});
return false;});`

$("#link-296431922").bind("click",url:"\/AuthAdminCakeApp\/posts\/displayPosts\/page:2"});
return false;});`

我的PostsController代码如下所示:

class PostsController extends AppController {

public $helpers = array('Js' => 'Jquery');    
public $paginate = array(
    'limit' => 3,'order' => array(
        'Post.title' => 'asc'
   )
);

public function displayPosts() {
    $this->set('posts',$this->paginate());
}

// other actions
}

我的分页元素(paging.ctp)如下所示:

<?PHP
$this->Paginator->options(
        array('update'=>'#postsPaging','url'=>array('controller'=>'posts','action'=>'displayPosts')));
?>
<table cellpadding="0" cellspacing="0">
<tr>
    <th><?PHP echo $this->Paginator->sort('id'); ?></th>
    <th><?PHP echo $this->Paginator->sort('user_id'); ?></th>
    <th><?PHP echo $this->Paginator->sort('title'); ?></th>
    <th><?PHP echo $this->Paginator->sort('body'); ?></th>
    <th><?PHP echo $this->Paginator->sort('created'); ?></th>
    <th><?PHP echo $this->Paginator->sort('modified'); ?></th>
    <th class="actions"><?PHP echo __('Actions'); ?></th>
</tr>

<?PHP
foreach ($posts as $post): ?>
<tr>
    <td><?PHP echo h($post['Post']['id']); ?>&nbsp;</td>
    <td>
        <?PHP echo $this->Html->link($post['User']['id'],array('controller' => 'users','action' => 'view',$post['User']['id'])); ?>
    </td>
    <td><?PHP echo h($post['Post']['title']); ?>&nbsp;</td>
    <td><?PHP echo h($post['Post']['body']); ?>&nbsp;</td>
    <td><?PHP echo h($post['Post']['created']); ?>&nbsp;</td>
    <td><?PHP echo h($post['Post']['modified']); ?>&nbsp;</td>

    <td class="actions">
        <?PHP echo $this->Html->link(__('View'),array('action' => 'view',$post['Post']['id'])); ?>
        <?PHP echo $this->Html->link(__('Edit'),array('action' => 'edit',$post['Post']['id'])); ?>
        <?PHP echo $this->Form->postLink(__('Delete'),array('action' => 'delete',$post['Post']['id']),null,__('Are you sure you want to delete # %s?',$post['Post']['id'])); ?>
    </td>
</tr>
<?PHP endforeach;  ?>
</table>
<?PHP
echo $this->Paginator->prev('< ' . __('prevIoUs'),array(),array('class' => 'prev disabled'));
echo $this->Paginator->numbers(array('separator' => ''));
echo $this->Paginator->next(__('next') . ' >',array('class' => 'next disabled'));
?>

displayPosts操作的视图如下所示:

<div id="postsPaging">
<?PHP echo $this->element('Posts/paging'); ?>
</div>

编辑:
我正在使用CakePHP 2.2.5.

解决方法

你必须使用ajax布局.

$this->layout = ($this->request->is("ajax")) ? "ajax" : "default";

您还可以将此代码段放在AppController中,以用于应用程序范围内的Ajax响应.

public function beforeRender() {
    $this->layout = ($this->request->is("ajax")) ? "ajax" : "default";
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐