2

我正在 尝试WorkersJob.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。

4

1 回答 1

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

希望这对您有所帮助。

于 2013-11-11T05:22:07.567 回答