0

问题:当我有一个带有分页的搜索结果集时,下一个、上一个和数字的链接不保留搜索参数。似乎是一个普遍的问题。

我在互联网上到处搜索,最后我发现我应该把这个语句放在视图中:

$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] 
4

4 回答 4

1

$this->passedArgs 如果您使用passedArgs 来包含您正在搜索的类别和搜索词,例如“/category:XXXX/term:YYYY”等形式的url 参数,则应该使用它。

如果您只是使用没有 'category:' 或 'term:' 前缀的普通参数,那么您需要使用 $this->params['pass']

如果您没有在 url 中传递类别和术语,那么您应该这样做。

每当我有一个用户可以按搜索词或类别或任何内容过滤的结果集时,我总是将发布的表单数据传输到 url 参数并将用户重定向到该 url,然后从 url 中获取参数以填充分页条件。

这是一种常用的设计模式,被认为是最佳实践,因为它允许用户在无需填写表格的情况下深度链接到搜索结果。

如果您正在实现站点搜索功能,我的 github 上有一个相当完整的cakephp 搜索插件,但目前还没有文档,但请查看搜索控制器以了解我的意思。

于 2009-11-16T21:07:42.707 回答
0

您好我通过正确设置passedArgs解决了这个问题。发布工作代码:

控制器:

function search()
{
    $category = 'All';

    //debug( $this->passedArgs );
    //debug( $this->params);
    //debug( "form categ: " . $this->data['Cat']['category']) ;


    $conditions = array();

    // if category is passed with a submit...
    if ( isset( $this->data['Cat']['category'] ) )
      $category = $this->data['Cat']['category'];
    // if category is passed through paginating urls...
    if ( isset( $this->passedArgs['category'] ) )
      $category = $this->passedArgs['category'];

    // build conditions

    if ( $category != 'All')
      $conditions = array ( 'Cat.category' => $category );

    //set passedArgs for building paginating url in a correct way    
    if ( !isset( $this->passedArgs['category'] ) ) 
      $this->passedArgs['category'] = $category ;  
    $this->set( 'data', $this->paginate('Cat', $conditions ) );    
    $this->render( 'index_ar' );    
    return;
}

看法:

<center>
<h2>heading</h2>

<?php echo $form->create( 'Cat', array('action' => 'search')) ?>
<table border="0">
<td>Category</td>
<td valign="top">
<?php echo $form->input('category', 
array( 
'label' => false, 
'options' => 
array(
'all' => 'All',
'cat1' => 'Category1'   
)
) ); ?>
</td>
</tr>
-->
<tr><td colspan="2" valign="top"><?php echo $form->end('Search'); ?></td></tr>
</tr>
</table>
<hr />
<?php $paginator->options(array('url' => $this->passedArgs ));?>
<?php echo $paginator->counter( array('format' => 'Page %page% of %pages%, record  %current% on %count%'));?>
<br/>
Pages: <?php echo $paginator->numbers( ); ?>
<br/>
<?php
if ( count($data) == 0 )
    echo "<p style='text-align:center'>No record found.</p>";
else
{
?>
<table class='grid'>
<tr>
<th align="left"><?php echo $paginator->sort('ID', 'id'); ?></th>
<th align="left"><?php echo $paginator->sort('Name', 'name'); ?></th>
<th align="left"><?php echo $paginator->sort('Category', 'category'); ?></th>
<th align="left">Foto</th>
<th align="left"><?php echo $paginator->sort('Status', 'status'); ?></th>
<th width='17%'></th>
</tr>
[...]
于 2009-11-18T15:27:37.317 回答
0

似乎是正确的。确保 passArgs 不为空。您始终可以将测试数组传递给 url 以确保其正常工作。

$paginator->options(array('url' => array('a','b'))); 
于 2009-11-16T18:30:30.293 回答
0

刚玩了一会,也遇到了同样的问题。。。

<?php $paginator->options(array('url' => $this->params['named'])); ?>

为我工作。(我正在使用 CakePHP 1.2)希望这会有所帮助......

于 2010-03-05T12:56:03.090 回答