2013-03-12 172 views
1

我通過select-> order by $ GET-variables在我的項目中添加了排序數據。但是,當我通過paginator瀏覽頁面時,當然這些變量不會傳遞到下一頁。傳遞這個變量並將其與paginator一起使用的最佳方式是什麼?

控制器:

public function indexAction() 
{ 
    $sortForm = new \Records\Form\SortingForm(); 
    $field = 'date'; 
    $order = 'desc'; 
    $request = $this->getRequest(); 

    if ($request->isGet()){ 
     $sortForm->setValidationGroup(array('field', 'order')); 
     $sortData = $request->getQuery()->toArray(); 
     $sortForm->setData($sortData); 

     if($sortForm->isValid()) { 
      $sortForm->getData($sortData); 
      $field = (string) $this->params()->fromQuery('field', 'date'); 
      $order = (string) $this->params()->fromQuery('order', 'desc'); 
     } 
    } 
    $query = $this->getRecordsTable()->fetchAll($field, $order); 

    $paginator = new Paginator\Paginator(new Paginator\Adapter\Iterator($query)); 
    $paginator->setCurrentPageNumber($this->params()->fromRoute('page', 1)); 
    $paginator->setItemCountPerPage(25); 

    $vm = new ViewModel(array('records' => $paginator)); 

型號:

public function fetchAll($field, $order) 
    { 
     $this->field = $field; 
     $this->order = $order; 
     $resultSet = $this->tableGateway->select(function (Select $select) { 
     $select->columns(array('date', 'name', 'email', 'homepage', 'text', 'image', 'file')); 
     $select->order($this->field.' '.$this->order);   
     }); 
     $resultSet->buffer(); 
     $resultSet->next(); 

     return $resultSet; 
    } 

形式:

public function __construct($name = null) 
    { 
     parent::__construct('records'); 
     $this->setAttribute('method', 'get'); 

     $this->add(array(
      'name' => 'field', 
      'required' => false, 
      'type' => 'Zend\Form\Element\Select', 
      'options' => array(
       'label' => 'Sort by: ', 
       'value_options' => array(
        'date' => 'Date', 
        'email' => 'E-mail', 
        'name' => 'Username', 
      ), 
     ))); 
     $this->add(array(
      'name' => 'order', 
      'required' => false, 
      'type' => 'Zend\Form\Element\Select', 
      'options' => array(
       'value_options' => array(
        'asc' => 'ascending', 
        'desc' => 'descending', 
      ), 
     ))); 
     $this->add(array(
      'name' => 'submit', 
      'attributes' => array(
       'type' => 'submit', 
       'value' => 'Go', 
       'id' => 'submitbutton', 
      ), 
     )); 
    } 

分頁程序視圖:

<!-- Numbered page links --> 
<?php foreach ($this->pagesInRange as $page): ?> 
    <?php if ($page != $this->current): ?> 
    <a href="<?php echo $this->url($this->route, array('page' => $page)); ?>"> 
     <?php echo $page; ?> 
    </a> | 
    <?php else: ?> 
    <?php echo $page; ?> | 
    <?php endif; ?> 
<?php endforeach; ?> 

另一個:當我傳遞get變量時,查詢字符串看起來像http://guest-book.me/page/7?field=date&order=asc& submit =去我怎麼能不發送pair submit =>值?

謝謝你的幫忙! :)

+0

另一個:省略submits'name'元素 – Waygood 2013-03-12 16:13:21

+0

試試看這裏http://stackoverflow.com/questions/8852374/zend-paginator-make-other-links- and-action-calling-concatinating-with-the-url?rq = 1 – Waygood 2013-03-12 16:20:03

+0

@Waygood謝謝你的回答。 Zend \ Form \ Fieldset :: add:提供的元素或字段集沒有命名,並且在標誌中沒有提供名稱 - 如果省略「名稱」。如果關於鏈接 - 沒有幫助... – Igor 2013-03-12 19:33:09

回答

0

首先,你需要確保你使用的分頁程序的路徑有一個查詢字符串的孩子的路線,我會建議有一個路線專門爲使用與分頁列表,一個例子:

'router' => array(
    'routes' => array(
     'paginator_route' => array(
      'type' => 'segment', 
      'options' => array(
       'route' => '/list/:controller/:action[/page/:page][/]', 
       'constraints' => array(
        //'__NAMESPACE__' => 'Application\Controller', 
        'controller' => '[a-zA-Z][a-zA-Z0-9_-]*', 
        'page'   => '[0-9]+', 
       ), 
       'defaults' => array(
        'controller' => 'index', 
        'action'  => 'list', 
        'page'  => 1, 
       ), 
      ), 
      'may_terminate' => true, // with or without Query string 
      'child_routes' => array(
       'query' => array(// allows us to match query string 
        'type' => 'Query', 
        'options' => array(
         'defaults' => array(
          // Query string defaults here.. 
         ) 
        ) 
       ), 
      ), 
     ), 

現在,您需要修改分頁視圖,使我們能夠生成新的鏈接時,使用電流的參數作爲基礎:

<!-- Numbered page links --> 
<?php foreach ($this->pagesInRange as $page): ?> 
    <?php if ($page != $this->current): ?> 
    <?php // the last param allows us to reuse the matched params ?> 
    <a href="<?php echo $this->url($this->route, array('page' => $page), TRUE); ?>"> 
     <?php echo $page; ?> 
    </a> | 
    <?php else: ?> 
    <?php echo $page; ?> | 
    <?php endif; ?> 
<?php endforeach; ?> 

分頁鏈接現在應該保留查詢字符串值:-)

+0

謝謝,我會嘗試 – Igor 2013-03-13 13:38:01

+0

謝謝,它的工作原理!但有些問題:1)現在當我通過paginator傳遞頁面時,我的查詢字符串看起來像/ page/3 /?field = date&order = asc&controller = Records \ Controller \ Records&action = index。但是如果我想隱藏url中的'controller'和'action'變量呢?我的路由器:'route'=>'[/ page /:page] [/]','defaults'=> array('controller'=>'Records \ Controller \ Records','action'=>'index', 'page'=> 1,),在「可以終止」>>「查詢」>>「選項」: 'defaults'=> array('field'=>'date','order'=>'desc' )。 – Igor 2013-03-15 20:50:40

+0

和2)有沒有mod_rewrite可以重寫/ page/3 /?field = date&order = asc到/ page/3/date/asc,但是php/zf2?非常感謝 – Igor 2013-03-15 20:50:55

相關問題