gpt4 book ai didi

ajax - CakePHP Ajax 分页

转载 作者:行者123 更新时间:2023-12-03 22:52:40 25 4
gpt4 key购买 nike

我目前正在尝试使 ajax 分页列表工作。我想我很接近了。当我单击上一页、下一页或单个页码时,会显示正确的结果,但 div.postsPaging 加载了分页结果以及 Cake 的 default.ctp 布局的页眉和页脚。我已经尝试在 displayPosts View 或 displayPosts 操作中将布局设置为 ajax,但是默认布局根本没有出现(在 div 内部或外部)。

我的问题是,如何仅使用分页结果而不是页眉、页脚或布局中的任何其他内容加载 postsPaging div。如何获取ajax请求的结果不包含默认布局的页眉和页脚?

附加到页面索引之一和“下一步”按钮的 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", function (event) {$.ajax({dataType:"html", success:function (data, textStatus) {$("#postsPaging").html(data);}, 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(), null, array('class' => 'prev disabled'));
echo $this->Paginator->numbers(array('separator' => ''));
echo $this->Paginator->next(__('next') . ' >', array(), null, array('class' => 'next disabled'));
?>

displayPosts 操作的 View 如下所示:
<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";
}

关于ajax - CakePHP Ajax 分页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15289391/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com