php - Yii2 并在 ArrayDataProvider 中排序

标签 php yii yii2

我正在使用 ArrayDataProvider,我想知道如何在 View 中创建排序链接,就像 $sort->link('date')yii/data/Sort

最佳答案

关注 this (yii\data\sort)this (yii\data\ArrayDataProvider)文档

你可以做这样的排序:

$sort = new Sort([
    'attributes' => [
        'age',
        'name' => [
            'asc' => ['first_name' => SORT_ASC, 'last_name' => SORT_ASC],
            'desc' => ['first_name' => SORT_DESC, 'last_name' => SORT_DESC],
            'default' => SORT_DESC,
            'label' => 'Name',
        ],
        // or any other attribute
    ],
]);

之后,您可以将其放入您的数组数据提供程序中

$query = new Query;
$provider = new ArrayDataProvider([
    'allModels' => $query->from('post')->all(),
    'sort' => $sort, // HERE is your $sort
    'pagination' => [
        'pageSize' => 10,
    ],
]);
// get the posts in the current page
$posts = $provider->getModels();

最后在你看来:

// any attribute you defined in your sort defination
echo $sort->link('name') . ' | ' . $sort->link('age');

关于php - Yii2 并在 ArrayDataProvider 中排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37085204/

相关文章:

php - 服务器上 Laravel 文件夹的正确权限

php - 根据其他表信息从表中检索行

php - Mysql 一个父多个子结果不重复父行

javascript - Ajax 提交错误

email - Yii2没有电子邮件确认用户无法登录

php - 多个产品(购物车) strip API

bizrule 的 Yii 权限参数/数据

php - Yii Rest api 在数据库中插入数据

yii2.如何在dataProvider中设置场景?

Yii2 主题集成不起作用?