在CakePHP Web应用程序中自动排序表格

vohkndzv  于 2022-11-11  发布在  PHP
关注(0)|答案(2)|浏览(165)

我有一个表显示在我的CakePHP网页应用程序中的页面设置分页器。表总是出现在 'id' 升序排序(我真的不知道为什么),我希望它也是 'id' 排序,但降序。你知道是否有任何简单的函数,使这件事发生吗?请找到下面的代码。

<table>
    <thead>
        <tr>
            <th scope="col"><?= $this->Paginator->sort('id') ?></th>
            <th scope="col"><?= $this->Paginator->sort('contact_id') ?></th>
            <th scope="col"><?= $this->Paginator->sort('sum_price') ?></th>
        </tr>
    </thead>
    <tbody>
        <?php foreach ($orders as $order): ?>            
            <tr>
                <td><?= $this->Number->format($order->id) ?></td>
                <td><?= $this->Html->link($order->Contacts["name"] , ['controller' => 'Contacts', 'action' => 'view', $order->Contacts["id"]]) : '' ?></td>              
                <td><?= $this->Number->format($order->sum_price) ?></td>
            </tr>
        <?php endforeach; ?>
    </tbody>
</table>

到目前为止paginator工作正常,所以我可以点击列标题来对数据进行排序。无论如何,我希望它在第一次加载时能正确显示。
请告诉我是否还有其他重要的代码。
先谢谢你!

编辑

如果有帮助的话,我在我的应用控制器中有这两个函数:

public function all()
    {
        $orders = $this->Orders->find("all")->contain(["contacts"]);
        $orders = $this->paginate($orders);
        $this->set(compact('orders'));
        $this->viewBuilder()->setLayout("admin");
    }

public function view($id = null)
    {
        $this->loadModel('ContactAdresses');
        $this->loadModel('Contacts');
        $order = $this->Orders->get($id, ['contain' => ['Contacts']]);
        $contactId = $order->contact->id;
        $contact = $this->Contacts->get($contactId, ['contain' => ['ContactAdresses']]);
        $this->set('order', $order);
        $this->set('contact_adress', $contact->contact_adress);
        $this->viewBuilder()->setLayout("admin");
    }
8e2ybdfx

8e2ybdfx1#

public function all()
    {
        $orders = $this->Orders->find("all", ['order' => ['id' => 'DESC']])->contain(["contacts"]);
        $orders = $this->paginate($orders);
        $this->set(compact('orders'));
        $this->viewBuilder()->setLayout("admin");
    }

public function all()
     {
        $this->paginate = [
            'contain' => ['contacts',],
            'order'   => ['id' => 'DESC'],
            ],
        ];

        $query = $this->Orders->find('all');
        $this->set('orders', $this->paginate($query));
        $this->viewBuilder()->setLayout("admin");
     }
fkaflof6

fkaflof62#

下面是我编写的一个函数代码片段的示例。

public function index() {
        $this->paginate = [
            'contain' => ['Ctrs', 'States', 'Stages'],
            'order' => ['created' => 'DESC']            
        ];
        $movements = $this->paginate($this->Movements);

        $this->set(compact('movements'));
    }

相关问题