3

I am using ArrayDataProvider and i want to know how to make the sort links in view like a $sort->link('date') in yii/data/Sort

1 Answer 1

11

Follow this (yii\data\sort) and this (yii\data\ArrayDataProvider) documentation

what you can do is make sort like this:

$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
    ],
]);

after that you can put it in your array data provider

$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();

and finally in your view:

// any attribute you defined in your sort defination
echo $sort->link('name') . ' | ' . $sort->link('age');
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.