我通過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 =>值?
謝謝你的幫忙! :)
另一個:省略submits'name'元素 – Waygood 2013-03-12 16:13:21
試試看這裏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
@Waygood謝謝你的回答。 Zend \ Form \ Fieldset :: add:提供的元素或字段集沒有命名,並且在標誌中沒有提供名稱 - 如果省略「名稱」。如果關於鏈接 - 沒有幫助... – Igor 2013-03-12 19:33:09