CakePHP的Ajax的分页 [英] CakePHP Ajax Pagination

查看:136
本文介绍了CakePHP的Ajax的分页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前我正在试图让一个ajax分页列表的工作。我想我接近。当我点击了previous,下一步或个人页码,正确的结果表明,但div.postsPaging装有沿分页结果与蛋糕的default.thtml中布局的页眉和页脚。我试过的布局设置为AJAX在displayPosts视图或displayPosts动作,但随后的默认布局不会出现在所有的(内部或外部的DIV)。

I am currently trying to get an ajax paginated list working. I think I'm close. When I click on the Previous, Next or individual page numbers, the correct results show but div.postsPaging is loaded with the paginated results along with Cake's default.ctp layout's header and footer. I've tried setting the layout to ajax in the displayPosts view or in the displayPosts action, but then the default layout doesn't appear at all (either inside or outside the div).

我的问题是,我怎么可以加载postsPaging DIV只用分页结果,而不是页眉,页脚或其他任何东西的布局。我怎样才能获得Ajax请求的结果,不包含的默认布局的页眉和页脚?

My question is, how can I load the postsPaging div with just the paginated results and not the header, footer or anything else in the layout. How can I get the results of the ajax request to not include the header and footer of the default layout?

附加到页索引和下一步按钮之一jQuery的事件是:

The jQuery events attached to one of the page indexes and the Next button are:

$("#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", function (event) {$.ajax({dataType:"html", success:function (data, textStatus) {$("#postsPaging").html(data);}, url:"\/AuthAdminCakeApp\/posts\/displayPosts\/page:2"});
return false;});`

我的PostsController code是这样的:

My PostsController code looks like this:

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)看起来是这样的:

My paging element (paging.ctp) looks like this:

<?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(), null, array('class' => 'prev disabled'));
echo $this->Paginator->numbers(array('separator' => ''));
echo $this->Paginator->next(__('next') . ' >', array(), null, array('class' => 'next disabled'));
?>

视图的displayPosts动作看起来是这样的:

The view for the displayPosts action looks like this:

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

编辑: 我使用CakePHP 2.2.5。

I'm using CakePHP 2.2.5.

推荐答案

您必须使用AJAX的布局。

you have to use an ajax layout.

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

您也可以放置在AppController的这个code段,为Ajax响应应用广泛。

You can also place this code snippet in AppController, for Ajax responses application wide.

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

这篇关于CakePHP的Ajax的分页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆