【问题标题】:Paginate and Sort bug(s)?分页和排序错误)?
【发布时间】:2016-11-13 14:17:48
【问题描述】:

在我的控制器中,我将分页设置为按 2 个字段排序。

public $paginate = [
  'limit' => 50,
  'order' => ['first_name', 'last_name']
];
$contacts = $this->paginate($this->Contacts);

这在第一页上运行良好,但由于我遗漏了默认方向=> 'ASC',因此分页器链接根本不起作用:

/contacts?page=2&sort=0&direction=first_name

当我添加方向时,它可以工作,但当然只按第一个字段排序,弄乱了排序顺序。

/contacts?page=2&sort=Contacts.first_name&direction=ASC
  1. 是否应明确要求默认方向?
  2. 是否有一种方法可以在分页期间维护两个字段以进行排序?

按虚拟字段排序(例如full_name => first_name . ' ' . last_name)不像在 2.x 中那样工作

【问题讨论】:

  • 您用来创建链接的代码在哪里?
  • 默认烘焙代码:<div class="paginator"> <ul class="pagination"> <?php echo $this->Paginator->prev('< ' . __('previous')) ?> <?php echo $this->Paginator->numbers() ?> <?php echo $this->Paginator->next(__('next') . ' >') ?> </ul> <p><?php echo $this->Paginator->counter() ?></p> </div>
  • 1.是的,它应该。 2.stackoverflow.com/questions/31763413/…可能重复
  • 我认为这是known issue。

标签: cakephp pagination cakephp-3.2


【解决方案1】:

通过以下方式解决了这两个问题:

设置默认排序顺序与虚拟字段相同:

public $paginate = [
  'order' => ['first_name', 'last_name']
];

然后只需将以下内容添加到视图中,以防止分页器覆盖默认顺序,除非用户指定:

if (empty($_GET['direction'])) { $this->Paginator->options(['url' => ['direction' => null, 'sort' => null]]); }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-07-15
    • 2012-08-23
    • 2012-03-14
    • 2012-05-10
    • 2017-02-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多