直接使用分页器无法识别配置 [英] Using the paginator directly is not recognising the configuration

查看:55
本文介绍了直接使用分页器无法识别配置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

CakePHP版本:4.0.1

CakePHP Version: 4.0.1

简介

我有2个同时使用索引视图的方法 index search 。在索引上,可以从选择列表中选择列,并且可以通过允许按列和值搜索的输入表单控件输入值。此数据通过GET发送到搜索方法,在该方法中检查空值并执行查询并呈现索引视图。

I have 2 methods that both use the index view, index and search. On index the column can be selected from a select list and a value can be inputted via an input form control enabling a search by column and value. This data is sent via GET to the search method where empty values are checked and the query is executed and the index view is rendered.

在随后的3x版本中,如下所示

In the later 3x versions with the below configuration the index view had the sort on the selected column which is what it is meant to do.

IE:索引视图在初始加载时已按Due_date排序,因此我选择task_name然后将表单提交给搜索方法。渲染视图时,task_name具有排序。

IE: Index view has due_date sorted on the initial load and I select task_name then submit the form to the search method. The task_name has the sort when the view is rendered.

任务控制器

公共分页属性:

public $paginate = [
    'sortWhitelist' => [
       'Tasks.due_date',
       'Tasks.task_name',
       'Tasks.type',
       'Tasks.priority',
       'Tasks.related_to_name',
       'Contacts.first_name',
       'Contacts.last_name',
       'Accounts.account_name',
       'Tasks.task_desc'
    ]
];

搜索方法

我初始化从索引方法接收的数据,并将配置应用于分页属性,并将查询对象发送到视图。

I initialise the data received from the index method and apply the config to the pagination property and send the query object to the view.

$this->setPage('');
$this->setSort($this->request->getQuery('column'));
$this->setDirection('asc');

// Validation of the page, sort, direction and limit is done here.
// IE: The $this->getSort() must be a string and not be numeric and has a strlen check
// and the $this->getDirection() can only be a string with values 'asc' or 'desc' etc. 

if (!empty($this->getPage())) {
    $this->paginate['page'] = $this->getPage();
}
$this->paginate['sort'] = $this->getSort();
$this->paginate['direction'] = $this->getDirection();
$this->paginate['limit'] = $this->getLimit();

debug($this->paginate);

$tasks = $this->paginate($query);
$this->set(compact('tasks'));

调试结果为:

[
    'sortWhitelist' => [
        (int) 0 => 'Tasks.due_date',
        (int) 1 => 'Tasks.task_name',
        (int) 2 => 'Tasks.type',
        (int) 3 => 'Tasks.priority',
        (int) 4 => 'Tasks.related_to_name',
        (int) 5 => 'Contacts.first_name',
        (int) 6 => 'Contacts.last_name',
        (int) 7 => 'Accounts.account_name',
        (int) 8 => 'Tasks.task_desc'
    ],
    'sort' => 'Tasks.task_name',
    'direction' => 'asc',
    'limit' => (int) 25
 ]

结果

排序在task_name上。

Result
The sort is on the task_name.

几个月前,我升级到4,并且刚刚进行了更新查找排序的此功能位于索引上存在的列上,而不是所选的列上。我尝试通过以下方法解决该问题:

A couple of months ago I upgraded to 4 and have just revisted this functionality to find the sort is on the column that was present on index and not the column that was selected. I tried the below to fix the problem:

我引用了信息。并且

$config = $this->paginate = [
    'page' => $this->getPage(),
    'sort' => $this->getSort(),
    'direction' => $this->getDirection(),
    'limit' => $this->getLimit()
];

debug($config);

$tasks = $this->Paginator->paginate($query, $config);

debug($this->Paginator);

$this->set(compact('tasks'));

调试$ config的结果是:

[
    'page' => '',
    'sort' => 'Tasks.task_name',
    'direction' => 'asc',
    'limit' => (int) 25
]

debug $ this-> Paginator的结果是:

object(Cake\Controller\Component\PaginatorComponent) {

    'components' => [],
    'implementedEvents' => [],
    '_config' => [
        'page' => (int) 1,
        'limit' => (int) 20,
        'maxLimit' => (int) 100,
        'whitelist' => [
            (int) 0 => 'limit',
            (int) 1 => 'sort',
            (int) 2 => 'page',
            (int) 3 => 'direction'
        ]
    ]

}

注意:白名单包含限制,排序,页面和方向?限制是20,我什至没有选择20?

NOTE: The whitelist contains limit, sort, page and direction? And the limit is 20 and I don't even have a selection of 20?

结果

Result
The sort is on the due_date and I need it on the task_name.

其他信息

如果是我,请单击排序在task_name上,排序在task_name上。所有排序都不能在初始负载上起作用?

Extra Info
If I then click the sort on task_name the sort is on the task_name. All the sorts work just not on the initial load?

问题

如何配置分页属性,以便从搜索方法的初始加载开始对task_name进行排序。

Question
How can I configure the pagination property so the sort is on the task_name from the initial load of the search method.

感谢Z。

推荐答案

该修复程序成本较高,并不理想,但确实可以。我对初始负载进行重定向。基本上提交要搜索的表单,然后重定向回搜索。 IE:

The fix is a bit costly and not ideal but it does work. I do a redirect on the initial load. Basically submit the form to search then redirect back to search. IE:

if ($this->request->getQuery('initial') === 'yes') {

    $redirect = $this->request->getQuery('redirect', [
        'action' => 'search',
         '?' => [
             'method' => 'search',
             'column' => $this->getColumn(),
             'input' => $this->getInput(),
             'page' => $this->getPage(),
             'sort' => $this->getSort(),
             'direction' => $this->getDirection(),
             'limit' => $this->getLimit(),
             'filter' => $this->getFilter(),
         ]
    ]);

    return $this->redirect($redirect);
    exit(0);

}

$config = $this->paginate = [
    'sortWhitelist' => [
       'Tasks.due_date',
       'Tasks.task_name',
       'Tasks.type',
       'Tasks.priority',
       'Tasks.related_to_name',
       'Contacts.first_name',
       'Contacts.last_name',
       'Accounts.account_name',
       'Tasks.task_desc'
     ],
     'page' => $this->getPage(),
     'sort' => $this->getSort(),
     'direction' => $this->getDirection(),
     'limit' => $this->getLimit()
 ];

 $tasks = $this->Paginator->paginate($query, $config);
 $this->set(compact('tasks'));

排序现在在task_name上。

The sort is now on the task_name.

这消除了最初的加载问题,并在页面最初加载到我知道排序工作的地方后模拟了用法。

This negates the initial load problem and simulates usage after the page initially loads where I know the sorts work.

这篇关于直接使用分页器无法识别配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆