Symfony中的分页非常简单,而且非常好。然而,我正在寻找最好的方向去添加排序表。
我的想法是排序列,方向和当前页码在uri中定义,像这样:
http://www.mysite.com/backend_dev.php/articles/author/asc/3/
然后在每个页面上,Symfony使用uri来确定当前排序列,方向和页面,然后操作所有分页链接来考虑这些事情,以便当您点击链接以更改页面或按不同列排序时,它将带您到正确的位置。
有谁能告诉我其他的方向吗?我知道jQuery的表排序插件的简单性,但当有1000+记录时,它很糟糕,因为你必须一次加载它们才能使插件工作。
生成器管理员有一个有趣的方法。它从URI获取排序,如下所示。
/backend_dev.php/pedidos?sort=status&sort_direction=asc
为了不在整个链接中携带这些get参数(这样做很麻烦),它存储在用户会话中。让我们看一个例子。在动作中,你将有
public function executeIndex(sfWebRequest $request)
{
// sorting
if ($request->getParameter('sort') && $this->isValidSortColumn($request->getParameter('sort')))
{
$this->setSort(array($request->getParameter('sort'), $request->getParameter('sort_type')));
}
// pager
if ($request->getParameter('page'))
{
$this->setPage($request->getParameter('page'));
}
$this->pager = $this->getPager();
$this->sort = $this->getSort();
}
//// more code
protected function setPage($page)
{
$this->getUser()->setAttribute('ef3Pedido.page', $page, 'admin_module');
}
protected function getPage()
{
return $this->getUser()->getAttribute('ef3Pedido.page', 1, 'admin_module');
}
protected function getSort()
{
if (null !== $sort = $this->getUser()->getAttribute('ef3Pedido.sort', null, 'admin_module'))
{
return $sort;
}
$this->setSort($this->configuration->getDefaultSort());
return $this->getUser()->getAttribute('ef3Pedido.sort', null, 'admin_module');
}
protected function setSort(array $sort)
{
if (null !== $sort[0] && null === $sort[1])
{
$sort[1] = 'asc';
}
$this->getUser()->setAttribute('ef3Pedido.sort', $sort, 'admin_module');
}
protected function isValidSortColumn($column)
{
return Doctrine::getTable('Pedido')->hasColumn($column);
}
对于最终用户和开发人员来说,这是一个很好的方法。