2012-12-01 65 views
2

我正在爲分頁而苦苦掙扎。 我想使用默認功能。 問題是,分頁按鈕(數字,上一頁,下一頁)的鏈接不起作用。 我不知道如何將URL中的「page:2」傳遞給paginator,讓他知道他必須顯示/呈現下一個5結果的表格。 例如:我點擊頁面2上的「2」,網站將被刷新,但根本沒有顯示結果表。CakePHP分頁顯示結果問題

其他信息: 輸入字段的URL位於cakephp/Posts/index。當我點擊一個分頁按鈕(例如prev)時,分頁程序會轉到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」的視圖文件代碼(調試轉儲顯示了恰當的陣列應該如何):

如果你們中的任何人都能解決我的問題,我會非常感激。

更新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:在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> 

+0

一開始,你不需要用分頁助手類禁用選項,只生成並輸出環節適當。 – Daniel

+0

謝謝丹尼爾,我改變了這一點。但行爲沒有改變。 – Karl

+0

我可以看到的另一件事 - 如果你在ajax更新之後,你必須在$ this中設置'update'=>'#content''(或類似的)和'evalscripts => true' - > Paginator-> options',然後在所述#content div中包圍search_all.ctp。 – Daniel

回答

0

我發現,工作正常的解決方案:)

That's拯救其在每一頁上點擊閱讀會話參數。最後,在視圖中帶有ajax分頁代碼的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 // 
    } 
} 

在視圖中必須有一些調整了。首先是包裝整個頁面的div,以便了解更新哪些內容(感謝Daniel的提示)。第二個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(); 
?> 

0

的方法,我使用的,節省具有兩個不同的觀點:

// 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是我的默認佈局,它只是環繞視圖。

+0

感謝您的提示。我會按照自己的方式嘗試,也許添加會話參數以通過搜索條件很重要?! ... – Karl