CakePHP 2如何在使用分页时通过URL传递变量?

时间:2012-05-14 13:59:05

标签: cakephp-2.0

我在CakePHP应用程序中有一个项目,我想为它添加分页。我已添加分页,但当我点击页码或转到下一页时,我的应用程序会中断。

页面依赖于传递给控制器​​的变量以显示正确的数据。这是我的视图代码:

        <table class="table table-striped">
            <thead>
                <tr>
                    <th>Job ID</th>
                    <th>Date</th>
                    <th>Site</th>
                    <th>&nbsp;</th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($data as $jobsheet) { ?>
                    <tr>
                        <td>
                            <?php echo $jobsheet['Jobsheet']['jobnum']; ?>
                        </td>
                        <td>
                            <?php echo date("l, jS F Y", strtotime($jobsheet['Jobsheet']['jobdate'])); ?>
                        </td>
                        <td>
                            <span class="companyname"><strong><?php echo $jobsheet['Siteaddress']['sitename'] ?></strong></span><br />
                            <?php echo $jobsheet['Siteaddress']['addressline1']; ?>, 
                            <?php echo $jobsheet['Siteaddress']['addressline2']; ?>, 
                            <?php if ( $jobsheet['Siteaddress']['addressline3'] = '') { ?>
                                <?php echo $jobsheet['Siteaddress']['addressline3']; ?>, 
                            <?php } else { ?>

                            <?php } ?>
                            <?php echo $jobsheet['Siteaddress']['city']; ?>, 
                            <?php echo $jobsheet['Siteaddress']['county']; ?>, 
                            <?php echo $jobsheet['Siteaddress']['country']; ?>, 
                            <?php echo $jobsheet['Siteaddress']['postcode']; ?>
                        </td>
                        <td><a data-toggle="modal" href="#viewjobsheet<?php echo $jobsheet['Jobsheet']['id']; ?>" onClick="document.getElementById('jbif<?php echo $jobsheet['Jobsheet']['id']; ?>').src='/modal/customers/<?php echo $company['Company']['id']; ?>/jobs/<?php echo $jobsheet['Jobsheet']['id']; ?>/view/';" title="View" class="icon-eye-open"></a>
                        </td>
                    </tr>
                <?php } ?>
            </tbody>
        </table>
    </div>
</div>

<div class="row">
    <div class="controls">
        <?php echo $this->Paginator->prev('« Previous', null, null, array('class' => 'disabled')); ?> 

        <?php echo $this->Paginator->numbers(); ?>

        <?php echo $this->Paginator->next('Next »', null, null, array('class' => 'disabled')); ?>
    <br />
    <br />
        <?php echo $this->Paginator->counter(array(
                'format' => 'Page {:page} of {:pages}, showing {:current} records out of
                         {:count} total, starting on record {:start}, ending on {:end}'
            ));
        ?>
    </div>
</div>

这是控制器:

function index(){

    $companyid = $this->params['customer'];
    $this->set('companyid', $companyid);
    $siteid = $this->params['site'];

    $this->loadModel('Jobsheet');

    // Job Sheets
    $jobsheets = $this->Jobsheet->find('all', array('conditions' => array('Jobsheet.company' => $companyid), 'recursive' => 2, 'order' => 'Jobsheet.jobdate DESC', 'limit' => '10'));
    $this->set('jobsheets', $jobsheets);

    $this->paginate = array(
        'limit' => 10
    );

    $data = $this->paginate('Jobsheet');
    $this->set(compact('data'));    

}

虽然分页确实有效,但它最终会丢失变量,这会打破我正在尝试做的事情。我错过了什么?

1 个答案:

答案 0 :(得分:1)

一种方法是使用PaginatorHelper::options(请参阅API

例如,假设我们想要在Paginator页面更改之间传递变量$pass。所以首先将变量传递给View

public function index() {
    $pass = 'testString';
    $this->set(compact('pass'));
    $this->set('posts', $this->paginate());
}

View中,我们可以将此添加到Paginator使用Paginator::optionsurl选项更改网页时传递的参数。

<?php
    $this->Paginator->options(array(
        'url' => array(
            'pass' => $pass
        )
    ));
?>

因此,如果您从第1页移至第2页,则生成的网址类似于:

http://localhost/cake/posts/index/pass:testString/page:2

这也是Paginator传递排序顺序和排序键的方式。因此,pass:testString中的“通过”可以是您想要的任何内容,请务必不要使用sortdirectionpage

只要您通过Paginator链接更改页面,传递的变量就会保留。请注意,您可以通过属性访问ViewController中传递的参数的值:

$this->passedArgs