Symfony: Pagination + Sorting?
The pagination in Symfony is pretty straight开发者_C百科forward and pretty good. However I'm looking for the best direction to go for adding in Sorting to the table.
My thoughts are that the sorting column, direction and current page number are defined in the uri, like this:
http://www.mysite.com/backend_dev.php/articles/author/asc/3/
And then on each page, Symfony uses the uri to determine the current sorting column, direction and page and then manipulates all the pagination links to take those things into account so that when you click on a link to change pages or sort by a different column it takes you to the proper place.
Does anyone have any other directions I could go with this? I know about the simplicity of jQuery's tablesorter plugin but it sucks when there are 1000+ records because you have to load them all at once to make that plugin work.
The generator admin has an interesting approach. It gets the sorting from URI as well like below.
/backend_dev.php/pedidos?sort=status&sort_direction=asc
In order not to carry those get parameters throughout the links (it's a pain to do that), it stores in the user session. Let's see an example. In the action you'll have
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);
}
It's a nice approach for both, the end user and the developer.
精彩评论