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'));    

}

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

4

1 回答 1

1

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

例如,假设我们要$pass在 Paginator 页面更改之间传递变量。所以首先将变量传递给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 页,则生成的 url 将类似于:

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

这也是 Paginator 传递排序顺序和排序键的方式。因此,虽然“传入”pass:testString可以是任何你想要的,但一定不要使用sort,directionpage.

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

$this->passedArgs
于 2012-05-19T07:49:58.633 回答