I
I
Ivan Pashin2017-02-23 23:10:38
Yii
Ivan Pashin, 2017-02-23 23:10:38

How to display N records in reverse order in Yii2 using ListView?

Good day!
Please tell me how to implement the output of the last 10 messages on the page so that the last message is displayed at the bottom, and the first of these 10 - at the beginning.
It is also required to load another 10 previous messages upon request.
Sorting is carried out by the date of creation of the message in the database.
controller:

$dataProvider = new ActiveDataProvider([
            'query' => Message::find()->orderBy('created_at DESC'),
            'pagination' => [
                'pageSize' => 10,
            ],
        ]);
return $this->render('view', ['dataProvider' => $dataProvider]);

view:
echo \yii\widgets\ListView::widget([
            'dataProvider' => $dataProvider,
            'itemView' => '_message',
            'layout' => "{items}",
        ]);

The problem is that now I get either the 10 oldest messages in the correct order (if you remove DESC from orderBy() ) or the 10 latest messages in the wrong order (the last message is on top, but should be on the bottom).

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Abdula Magomedov, 2017-02-23
@Avarskiy

www.php.su/array_reverse

M
Maxim Timofeev, 2017-02-26
@webinar

It seems to me that it is easier to solve this problem in js. Since playing with sorting in the dataProvider, you can get a very crutch solution and pagination that has gone wrong.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question