1

我通过 $GET-variables 的 select->order 在我的项目中添加了排序数据。但是当我通过分页器浏览页面时,这些变量当然不会传递到下一页。传递此变量并将其与分页器一起使用的最佳方法是什么?

控制器:

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我怎么能不发送对 submit=>value?

谢谢你的帮助!:)

4

2 回答 2

0

首先,您需要确保用于 Paginator 的路由具有 Query String 子路由,我建议您使用专门用于分页列表的路由,例如:

'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; ?>

您的分页链接现在应该保留查询字符串值:-)

于 2013-03-13T09:31:54.487 回答
0

您也可以创建一个将 GET 参数传递给 url 的助手。我为此创建了一个示例模块:https ://github.com/samsonasik/SanSamplePagination

于 2013-03-13T20:38:45.673 回答