2013-03-12 2 views
1

$ GET 변수로 select-> order로 데이터를 정렬하는 프로젝트를 추가했습니다. 그러나 페이지를 통해 페이지를 탐색 할 때 물론이 변수는 다음 페이지로 넘어 가지 않습니다. 이 변수를 전달하고 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-variables를 전달할 때 쿼리 문자열은 다음과 같습니다. http://guest-book.me/page/7?field=date&order=asc& submit = go 어떻게 값을 보내지 않고 보낼 수 있습니까? => 값?

도움 주셔서 감사합니다. :)

+0

그리고 또 다른 하나 : 'name'엘리먼트를 생략하십시오. – Waygood

+0

여기를보십시오 http://stackoverflow.com/questions/8852374/zend-paginator-make-other-links- url? rq = 1 – Waygood

+0

@Waygood, 답변 해 주셔서 감사합니다. Zend \ Form \ Fieldset :: add : 제공된 요소 또는 fieldset의 이름이 지정되지 않고 플래그에 제공된 이름이 없습니다. 'name'을 생략하면됩니다. 링크가 도움이 안되면 ... – Igor

답변

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

+0

감사합니다. 작동합니다! 하지만 몇 가지 질문 : 1) 내 질의 문자열은 paginator를 통해 페이지를 전달할 때/page/3 /? field = date & order = asc & controller = Records \ Controller \ Records & action = index와 같습니다. 하지만 '컨트롤러'와 '액션'변수를 URL에서 숨기려면 어떻게해야할까요? 내 라우터 : 'route'=> '[/ page/: 페이지] [/]', 'defaults'=> 배열 ('컨트롤러'=> '레코드 \ 컨트롤러 \ 레코드', '동작'=> '인덱스' 'default'=> 배열 ('field'=> 'date', 'order'=> 'desc') : "쿼리">> "옵션"). – Igor

+0

및 2) mod_rewrite를 사용하지 않고/page/3 /? field = date & order = asc를/page/3/date/asc로 다시 쓰는 것이 가능하지만 php/zf2? 고맙습니다. – Igor