4

現在、ajax のページ分割されたリストを機能させようとしています。私は近いと思います。前、次、または個々のページ番号をクリックすると、正しい結果が表示されますが、div.postsPaging は Cake の default.ctp レイアウトのヘッダーとフッターと共にページ分割された結果と共にロードされます。displayPosts ビューまたは displayPosts アクションでレイアウトを ajax に設定しようとしましたが、デフォルトのレイアウトがまったく表示されません (div の内側または外側)。

私の質問は、ヘッダー、フッター、またはレイアウト内の他のものではなく、ページ分割された結果だけで postsPaging div をロードするにはどうすればよいかということです。デフォルトレイアウトのヘッダーとフッターを含まないように ajax リクエストの結果を取得するにはどうすればよいですか?

ページ インデックスの 1 つと [次へ] ボタンに関連付けられている 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 アクションのビューは次のようになります。

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

編集: CakePHP 2.2.5 を使用しています。

4

2 に答える 2

5

ajaxレイアウトを使用する必要があります。

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

アプリケーション全体のAjax応答用に、このコードスニペットをAppControllerに配置することもできます。

public function beforeRender() {
    $this->layout = ($this->request->is("ajax")) ? "ajax" : "default";
}
于 2013-03-08T09:16:06.323 に答える
0

また、RequestHandler コンポーネントをロードしていることを確認してください。

var $components = array('RequestHandler');

上記を Controller または AppController に配置すると、レイアウトを ajax に設定できます。

于 2013-08-02T14:28:12.803 に答える