2013-01-24 152 views
0

我試圖完成搜索表單,其中一個部分是分頁。接下來的問題是: 我不知道如何將GET參數傳遞給paginator鏈接(例如:/ page/2?search = query)。 我已經在應用/ scripts目錄中創建「controls.phtml」和Bootsrap加入Zend Framework:將GET參數傳遞給paginator

Zend_View_Helper_PaginationControl::setDefaultViewPartial('controls.phtml'); 

,並在視圖渲染分頁程序:

echo $this->paginationControl($this->paginator, 'Elastic', 'controls.phtml');

可能有人寫一步一步的行動,我需要做什麼來決定這個問題? (使用ZF我只是第二天)

回答

0

我會在控制器上做更多的事情,只是將$paginator本身傳遞給視圖。像這樣的事情在你的控制器:

Zend_Paginator::setDefaultScrollingStyle("Elastic"); 
Zend_View_Helper_PaginationControl::setDefaultViewPartial("controls.phtml"); 

$paginator = Zend_Paginator::factory($result); 
$paginator->setItemCountPerPage(5); 
$paginator->setPageRange(5); 
//this is where you collect the GET param and use as parameter. 
$paginator->setCurrentPageNumber((int) $this->getRequest()->getParam("page", 1)); 

$this->view->paginator = $paginator; //pass it from Controller into View 

然後在視圖代碼:

<?= $this->paginator; ?> 
0

好吧,你不會將查詢參數發送到分頁程序,你會送他們到paginator adapter。然後,選定的適配器將執行所需的任何操作... 數據庫查詢或解析數組。 ..然後,您將數據庫適配器傳遞給paginator。

這裏是一個控制器的動作,要麼建立一個分頁程序或重定向基於一個GET查詢:

public function displayAction() 
    { 
     //get all parameters from requested url 
     $request = $this->getRequest()->getParams(); 
     //build switch based GET query 
     switch ($request) { 
      //if ?query=value 
      case (isset($request['query'])): 
       //model to use for database query 
       $model = new Music_Model_Mapper_Track(); 
       //paginator adapter to use 
       $adapter = $model->fetchPagedTracks(null, $request['query']); 
       //intiate new paginator 
       $paginator = new Zend_Paginator($adapter); 
       //provide paginator page values 
       $paginator->setItemCountPerPage(20)->setPageRange(5); 
       //?page=1 is default 
       $page = $this->getRequest()->getParam('page', 1); 
       //current page number 
       $paginator->setCurrentPageNumber($page); 
       //pass paginator to view 
       $this->view->paginator = $paginator; 
       break; 
      //if ?id=value 
      case (isset($request['id'])): 
       $this->_forward('artist', null, null, array('id' => $request['id'])); 
       break; 
      //if neither query value present. Should never get here. 
      default: 
       $adapter = $model->fetchPagedTracks(); 
       //[...] 
       break; 
     } 
    } 

我使用修改後的適配器爲我的分頁程序適配器。它是從DbTableSelect適配器擴展和簡單地替換在數據庫表中的映射器:在映射我使用分頁程序適配器以完全相同的方式

class Music_Model_Paginator_Track extends Zend_Paginator_Adapter_DbTableSelect 
{ 
    /** 
    * Override getItems() to use mapper model instead of database table 
    */ 
    public function getItems($offset, $itemCountPerPage) 
    { 
     $rows = parent::getItems($offset, $itemCountPerPage); 

     $albums = array(); 
     foreach ($rows as $row) { 
      $album = new Music_Model_Mapper_Track(); 
      $albums[] = $album->createEntity($row); 
     } 
     return $albums; 
    } 
} 

默認適配器將用於:

/** 
    * Creates Zend_Paginator_Adapter_DbTableSelect object, 
    * based on artist_id or query string. 
    * 
    * @param string $id 
    * @param string $query 
    * @return Music_Model_Paginator_Track 
    */ 
    public function fetchPagedTracks($id = null, $query = null) 
    { 
     //if $id is not null where=$id 
     if (!is_null($id)) { 
      $select = $this->getGateway()->select(); 
      $select->where('track.artist_id = ?', $id); 
     } 
     //if $query is not null join on the artist table and query track title and artist name for match. 
     if (!is_null($query)) { 
      $select = $this->getGateway() 
       ->select(Zend_Db_Table::SELECT_WITH_FROM_PART); 
      $select->setIntegrityCheck(false); 
      $select->join('artist', 'artist.id = track.artist_id'); 
      $select->where("track.title LIKE '%$query%'"); 
      $select->orWhere("artist.name LIKE '%$query%'"); 
     } 
     //default select query if $id and $query are null, appended to query otherwise. 
     $select->order('track.artist_id ASC'); 
     $select->order('track.album_id ASC'); 
     $select->order('track.track_number ASC'); 
     //instantiate paginator adapter 
     //$adapter = new Zend_Paginator_Adapter_DbTableSelect() would also work 
     $adapter = new Music_Model_Paginator_Track($select); 
     //return paginator adapter 
     return $adapter; 
    } 

最後的觀點是沒有什麼特別的:

<div><?php 
echo $this->paginationControl(
     $this->paginator, 'Sliding', '_paginatorControl.phtml' 
) 
?></div> 
//[..] 

對於一些適配器(在某些情況下),你可能必須堅持你的查詢值與Zend_RegistryZend_Session傳回到網址。

希望這會有所幫助。