cakephp 2x不同型号的分页

时间:2013-11-11 00:42:04

标签: cakephp pagination

我正在尝试将属于Workers的{​​{1}}分页到Job内的JobsController

    class JobsController extends AppController {

var $name = 'Jobs';
var $helpers = array('Html', 'Form', 'Js');
var $paginate = array(
    'Worker' => array(
        'limit' => 5, 
        'recursive' => 0, 
        'model' => 'Worker', 
        'order' => array('age' => 'ASC')
    ),      
);

function view($id = null) {
    if (!$id) {
        $this->Session->setFlash(__('Invalid Job.'));
        $this->redirect(array('action'=>'index'));
        return;
    }

    $this->Job->id = $id;

    $workers = $this->paginate('Worker', array('Worker.job_id' => $id));
    if ($workers) {
        $this->set('workers', $workers);
    }
}

在view.ctp中:

<?php
$this->Html->script(array('jquery.min'), array('inline' => false));

$this->Paginator->options(array(
    'update' => '#content',
    'evalScripts' => true,
));
?>

<?php if (isset($workers)): ?>

    <?php echo $this->Paginator->numbers(array('model' => 'Worker')); ?>

    <table>
        <tr>
            <th>Age</th>
            <th>Info</th>
        </tr>
        <?php foreach ($workers as $worker): ?>
            <tr>
                <td>
                    <?php echo $worker['Worker']['age']; ?>
                </td>
                <td>
                    <?php echo $worker['Worker']['info']; ?>
                </td>
            </tr>
        <?php endforeach; ?>
    </table>

    <?php echo $this->Paginator->numbers(array('model' => 'Worker')); ?>
    <p>
    <?php
    echo $this->Paginator->counter(array(
        'model' => 'Worker',
        'format' => __('Page %page% of %pages%, showing %current% records out of %count% total.')
    ));
    ?></p>

<?php endif; ?>

<?php echo $this->Js->writeBuffer(); ?>

我得到了正确的工人名单。但numbers生成的链接不起作用。它们看起来像/view/2/page:2/sort:Worker.age/direction:ASC 我究竟做错了什么? cakephp版本是2.4.1。

1 个答案:

答案 0 :(得分:0)

在视图操作中尝试此操作,方法是在运行时设置顺序。

$this->paginate['Worker']['order'] = array('Worker.age' => 'ASC')

现在你的功能看起来像这样

function view($id = null) {
    if (!$id) {
        $this->Session->setFlash(__('Invalid Job.'));
        $this->redirect(array('action'=>'index'));
        return;
    }

    $this->Job->id = $id;

    $this->paginate['Worker']['order'] = array('Worker.age' => 'ASC');
    $workers = $this->paginate('Worker', array('Worker.job_id' => $id));
    if ($workers) {
        $this->set('workers', $workers);
    }
}

希望这会对你有所帮助。