2

ページネーションに苦労しています。デフォルトの機能を使いたい。問題は、ページネーション ボタン (数字、前、次) のリンクが機能しないことです。次の 5 つの結果を含むテーブルを表示/レンダリングする必要があることをページネーターに知らせるために、URL の「page:2」をページネーターに戻す方法がわかりません。例: page:2 の「2」をクリックすると、サイトが更新されますが、結果テーブルがまったく表示されません。

その他の情報: 入力フィールドの URL は Cakephp/Posts/index にあります。ページネーター ボタン (前へなど) をクリックすると、ページネーターは cakephp/Posts/index/page:2 に移動します。

ここで、PostsController コードを取得します。

class PostsController extends AppController {    
    var $name = 'Posts';
    public $helpers = array('Html', 'Form', 'Js', 'Paginator');
    public $components = array('RequestHandler');
    public $paginate = array(
    'limit' => 5,
    'maxLimit' => 50,
    'order' => array(
        'Post.INCOME' => 'asc',
    ),
);
function index() {
    //debug($this->request->data);
    //var_dump($this->params);
    if(!empty($this->request->data['ajaxSearchAll'])){
        if($this->RequestHandler->isAjax()){
            //$data = $this->Post->find('all', array('conditions' => array('ZIPCODE LIKE' => '%' . $this->data['ajaxSearchAll'] . '%')));
            $posts = $this->paginate('Post', array('ZIPCODE LIKE' => '%' . $this->data['ajaxSearchAll'] . '%')); 
            //$this->set('posts', $data);
            $this->set(compact('posts')); 
            $this->render('SearchAll', 'ajax');         
        }   
    } 
/// GOES ON WITH IRRELEVANT STUFF ///

以下は、「search_all.ctp」のビュー ファイル コードを示しています (デバッグ ダンプは、本来あるべき適切な配列を示しています)。

<?php $this->paginator->settings = array(
    'limit' => 5,
    'maxLimit' => 25
); 
$this->Paginator->options(array('url' => array('controller' => 'Posts', 'action' => 'index')));
?>
<table>
    <tr>
        <th><?php echo $this->Paginator->sort('CITY', 'Stadt'); ?></th>

        <th><?php echo $this->Paginator->sort('DATE', 'Lieferdatum'); ?></th>
        <th><?php echo $this->Paginator->sort('TIME', 'Lieferzeit'); ?></th>        
    </tr>
    <!-- Hier iterieren wir in einer Schleife durch den $posts Array und geben die Daten des aktuellen Elements aus -->

    <?php foreach ($posts as $post): ?>
    <tr>
        <td><?php echo $this->Html->link($post['Post']['CITY'],"/Posts/view/".$post['Post']['id']); ?></td>
        <td><?php echo $post['Post']['DATE']; ?></td>
        <td><?php echo $post['Post']['TIME']; ?></td>
    </tr>
    <?php endforeach;} ?>
</table>
<?php 
if(!empty($posts)){ 
    echo $this->Paginator->first(__('Erste Seite', true), array('class' => 'disabled'));
    echo " ";
    echo $this->Paginator->prev('« Vorherige Seite', null, null, array('class' => 'disabled'));
    echo " ";
    echo $this->Paginator->numbers();
    echo " ";
    echo $this->Paginator->next('Nächste Seite »', null, null, array('class' => 'disabled'));
    echo " ";
    echo $this->Paginator->last(__('Letzte Seite', true), array('class' => 'disabled'));
} ?>
<?php debug($this->Paginator->params()); 
debug($this->request->params['paging']);
?>

あなたの誰かが私の問題を解決できるなら、私はとても感謝しています.

更新 1: の配列を提供します

debug($this->request->params['paging']);

それは絶対に問題ないように見えます。名前付きパラメーターで何かをする必要がありますか? クックブックには適切なものが見つかりませんでした:-(

array(
    'Post' => array(
        'page' => (int) 1,
        'current' => (int) 5,
        'count' => (int) 11,
        'prevPage' => false,
        'nextPage' => true,
        'pageCount' => (int) 3,
        'order' => array(
            'Post.INCOME' => 'asc'
        ),
        'limit' => (int) 5,
        'options' => array(
            'conditions' => array()
        ),
        'paramType' => 'named'
    )
)

更新 2: ajax 検索が開始される index.ctp のコード。ページネーターのクエリと最初のページは完全に機能しています。しかし、ページネーターのボタンはまったくありません:

<div id="courierQuery">
    <?php echo $this->Form->create(false, array('type' => 'post', 'default' => false)); ?>
    <?php echo $this->Form->input('ajaxSearchAll', array('type' => 'text', 'id' => 'ajaxSearchAll', 'name' => 'ajaxSearchAll', 'label' => false, 'placeholder' => 'Bitte PLZ eingeben'))?>
    <?php $before = $this->Js->get('#loading')->effect('fadeIn', array('buffer' => False)); ?>
    <?php $success = $this->Js->get('#loading')->effect('fadeOut', array('buffer' => False)); ?>
    <?php echo $this->Js->get('#ajaxSearchAll')->event('keyup', $this->Js->request(
        array('controller'=>'Posts', 'action'=>'index'),
            array(
                'update'=>'#erfolgreich_ajax',
                'before' => $before,
                'success' => $success,
                'async' => true,
                'dataExpression' => true,
                'method' => 'post',
                'data'=>$this->Js->serializeForm(array('isForm'=>'false', 'inline'=>'true'))
            )   
        )
    ); ?>
    <?php echo $this->Form->end();?>        
</div>
/// IRRELEVANT STUFF ///
<div id="loading" style="display: none;">
    <div style="float:left;margin-top: 14px"><?php echo $this->Html->image('ajax-loader.gif', array('id' => 'busy-indicator')); ?></div>
    <div style="float:left;margin: 22px 0 0 10px;color: #fff"><p>Suche nach Inseraten l&auml;uft ...</p></div>
</div>

4

2 に答える 2

1

うまくいく解決策を見つけました:)

それは、ページクリックごとに読み取られるセッションパラメーターを保存することです。最後に、ビューに ajax-pagination-code を含む Daniel のヒントにより、非 ajax 呼び出しと ajax 呼び出しを区別できるようになりました。そのため、すべての検索とページネーション リンクはコントローラー コードの ajax パスで処理されますが、セッション パラメーターの削除は外部で処理できます (これは以前の私の主な問題の一部でした)。

コントローラーコード:

   function index() {
    if(!$this->RequestHandler->isAjax()){
        $this->Session->delete('Ajax.searchA');
        $this->Session->delete('Ajax.searchS');
        $this->Session->delete('Ajax.searchB');
    }
    if($this->RequestHandler->isAjax()){
        if(!empty($this->request->data['ajaxSearchAll'])){
            $this->Session->write('Ajax.searchA', $this->request->data['ajaxSearchAll']);
            $this->Session->delete('Ajax.searchS');
            $this->Session->delete('Ajax.searchB');
            $posts = $this->paginate('Post', array('ZIPCODE LIKE' => '%' . $this->Session->read('Ajax.searchA') . '%')); 
            $this->set(compact('posts')); 
            $this->render('SearchAll', 'ajax');         
        } else{
            if($this->Session->read('Ajax.searchA') != null){
                $posts = $this->paginate('Post', array('ZIPCODE LIKE' => '%' . $this->Session->read('Ajax.searchA') . '%')); 
                $this->set(compact('posts'));   
                $this->render('SearchAll', 'ajax'); 
            }
        }
    // OTHER 2 BLOCKS //
    }
}

ビューでは、いくつかの調整も必要でした。最初に、ajax-update が更新するコンテンツを知るためにページ全体をラップする div (ヒントをくれた Daniel に感謝します)。2 番目の echo $this->Js->writeBuffer(); チェーンリンクを取得するための下部にあることは、適切に機能するために不可欠です。ビュー コード (search_all.ctp):

<div id="searchAll">
<?php if(empty($posts)){ ?>
        <p class="error">Leider keine Inserate gefunden :-(</p>
        <p>Sei der erste in Deinem PLZ-Bereich und erstelle ein Inserat:</p>
        <?php echo $this->Html->link('Inserat erstellen', array('controller' => 'Posts', 'action' => 'add')); ?>
    <?php } else { ?>
    <p class="message">Wir haben <?php echo $this->Paginator->counter('{:count}');?> Inserate gefunden :-)</p>
    <?php
    $this->Paginator->options(array(
                  'update'=>'#searchAll',
                  'evalScripts'=>true,
                  'before' => $this->Js->get('#loading')->effect('fadeIn', array('buffer' => false)),
                  'complete' => $this->Js->get('#loading')->effect('fadeOut', array('buffer' => false))
                  ));
    ?>
    <table>
        <tr>
            <th><?php echo $this->Paginator->sort('ZIPCODE', 'Postleitzahl'); ?></th>
        </tr>
        <!-- Hier iterieren wir in einer Schleife durch den $posts Array und geben die Daten des aktuellen Elements aus -->

        <?php foreach ($posts as $post): ?>
        <tr>
            <td><?php echo $post['Post']['ZIPCODE']; ?></td>
        </tr>
        <?php endforeach;} ?>
    </table>
    <?php 
    if(!empty($posts)){ 
        echo $this->Paginator->numbers();
    } ?>
<?php 
//debug($this->Paginator->params()); 
//debug($this->request->params['paging']);
debug($this->Session->read('Ajax.searchA'));
echo $this->Js->writeBuffer();
?>

于 2012-12-05T13:28:24.987 に答える
0

私が使用している方法では、2つの異なるビューを持つことができます。

// UsersController.php
public function admin_index() {
    $this->User->recursive = 0;
    $this->set('users', $this->paginate());
}

// admin_index.ctp
<h1>Users</h1>
<?php
$this->Paginator->options(array(
                  'update'=>'#content',
                  'evalScripts'=>true
                  ));
?>

<table id="content-table">
<tr>
    <th><?php echo $this->Paginator->sort('username', 'Username', array('escape'=> false)); ?></th>
    <th><?php echo $this->Paginator->sort('created', 'Created', array('escape'=> false)); ?></th>
</tr>

<?php
foreach ($users as $user) {
    echo "<tr>";
    echo "<td>";
    echo $user['User']['username'];
    echo "</td><td>";
    echo $user['User']['created'];
    echo "</td>";
    echo "</tr>";
}
?>
</table>

<div id="leftcontrols" class="left">
    <?php echo $this->Paginator->first(' <<'); ?>
    <?php echo $this->Paginator->prev(' <'); ?>
</div>
<div id="rightcontrols" class="right">
    <?php echo $this->Paginator->next(' >'); ?>
    <?php echo $this->Paginator->last(' >>'); ?>
</div>
<br/>

<div id="controls" style="text-align:center;">
    <?php echo $this->Paginator->counter(); ?>
</div>

#content私のデフォルトのレイアウトでは、とにかくビューを囲んでいます。

于 2012-12-03T01:24:36.720 に答える