我正在使用 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/