Cakephp 1.2 分页器和 PassedArgs
问题:当我有一个带分页的搜索结果集时,下一个、上一个和数字的链接不会保留搜索参数。似乎是一个常见问题。
我在互联网上到处搜索,最后我发现我应该把这个 视图中的声明:
$paginator->options(array('url' => $this->passedArgs));
但是,我无法使其工作,我应该在控制器中对 $this->passedArgs 执行某些操作吗?
请帮助
谢谢
控制器代码:
function search($category=null)
{
$this->paginate['Cat'] = array(
'limit' => 10,
'order' => array ('Cat.id' => 'desc')
);
$conditions = array('Cat.category' => $this->data['Cat']
['category']);
$this->set( 'data', $this->paginate('Cat', $conditions ) );
$this->render( 'index_ar' );
return;
}
查看代码:
<?php
$paginator->options(array('url' => $this->passedArgs));
echo $paginator->numbers( );
?>
<table class='grid'>
<tr>
<th><?php echo $paginator->sort('ID', 'id'); ?></th>
<th><?php echo $paginator->sort('Nome', 'name'); ?></th>
<th><?php echo $paginator->sort('Categoria', 'category'); ?></th>
<th>Foto</th>
<th><?php echo $paginator->sort('Stato', 'status'); ?></th>
<th width='25%'></th>
</tr>
<?php $i = '0'; $count = '1';?>
<?php foreach ($data as $cats): ?>
<?php $class = (is_int($i/2)) ? 'data-grid-row-1' : 'data-grid-
row-2';?>
<tr class="<?php echo $class?>">
<td><?php echo $cats['Cat']['id'] ?></td>
<td><?php echo $cats['Cat']['name'] ?></td>
<td><?php echo $cats['Cat']['category'] ?></td>
<td style='width:25px'>
[cut]
Problem: when i have a search resultset with pagination, the links next, prev and numbers do not keep the search parameters. Seems to be a common problem.
I searched everywhere on the internet, and at last i found that i should put this
statement in the view:
$paginator->options(array('url' => $this->passedArgs));
However, i can't make it work, Should i do something on $this->passedArgs in the controller?
Please help
Thanks
controller code:
function search($category=null)
{
$this->paginate['Cat'] = array(
'limit' => 10,
'order' => array ('Cat.id' => 'desc')
);
$conditions = array('Cat.category' => $this->data['Cat']
['category']);
$this->set( 'data', $this->paginate('Cat', $conditions ) );
$this->render( 'index_ar' );
return;
}
view code:
<?php
$paginator->options(array('url' => $this->passedArgs));
echo $paginator->numbers( );
?>
<table class='grid'>
<tr>
<th><?php echo $paginator->sort('ID', 'id'); ?></th>
<th><?php echo $paginator->sort('Nome', 'name'); ?></th>
<th><?php echo $paginator->sort('Categoria', 'category'); ?></th>
<th>Foto</th>
<th><?php echo $paginator->sort('Stato', 'status'); ?></th>
<th width='25%'></th>
</tr>
<?php $i = '0'; $count = '1';?>
<?php foreach ($data as $cats): ?>
<?php $class = (is_int($i/2)) ? 'data-grid-row-1' : 'data-grid-
row-2';?>
<tr class="<?php echo $class?>">
<td><?php echo $cats['Cat']['id'] ?></td>
<td><?php echo $cats['Cat']['name'] ?></td>
<td><?php echo $cats['Cat']['category'] ?></td>
<td style='width:25px'>
[cut]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您使用 passArgs 来包含您正在搜索的类别和搜索词,即“/category:XXXX/term:YYYY”等形式的 url 参数,则应使用 $this->passedArgs。
如果您仅使用普通参数如果没有 'category:' 或 'term:' 前缀,那么您需要使用 $this->params['pass']
如果您没有在 url 中传递类别和术语,那么您应该这样做。
每当我有一个用户可以按搜索词或类别或任何内容进行过滤的结果集时,我总是将发布的表单数据传输到 url 参数中并将用户重定向到该 url,然后从 url 中获取参数来填充分页条件。
这是一种常用的设计模式,被认为是最佳实践,因为它允许用户深层链接到搜索结果,而无需填写表单。
如果您正在实现站点搜索功能,我的 github 上有一个相当完整的 cakephp 搜索插件,但还没有文档,但请查看搜索控制器以了解我的意思。
$this->passedArgs should be used if you are using passedArgs to contain the category and search term you are searching for i.e. url params in the form "/category:XXXX/term:YYYY" etc.
If you are just using normal params without the 'category:' or 'term:' prefixes, then you need to use $this->params['pass']
If you are not passing the category and term around in the url, then you should be.
Whenever I have a result set that users can filter by a search term or category or anything I always transfer the posted form data into url params and redirect the user to that url, then take the params from the url to populate the conditions for pagination.
This is a commonly used design pattern and considered best practice as it allows users to deep link to search results without having to fill in a form.
If you are implementing a site search functionality, I have a fairly complete cakephp search plugin available on my github, but no documentation just yet, but have a look in the searches controller for an idea what I mean.
似乎是正确的。确保 passArgs 不为空。
您始终可以将测试数组传递给 url 以确保其正常工作。
seems correct. make sure passedArgs is not empty.
you can always pass a test array to url to make sure it's working.
您好,我通过正确设置 passArgs 解决了问题。发布工作代码:
控制器:
视图:
Hello I solved the problem by setting passedArgs in a correct way. Posting working code:
Controller:
View:
我刚刚遇到了同样的问题,玩了一段时间后......
对我有用。 (我使用的是 CakePHP 1.2)
希望这有帮助...
I just had the same problem, after a bit of playing...
worked for me. (I'm using CakePHP 1.2)
Hope this helps...