M
M
Maxim2018-05-30 20:08:49
Yii
Maxim, 2018-05-30 20:08:49

Yii ListView - how to split data by dates?

Good evening everyone! There is a ListView that displays a list of records. It is necessary to separate the data on the page by year.
Example
2018
Document date
document date
document date
document date
2017
document date
document date
And now this is
2018 Document date
2018 document date
2018 document date
2018 document date
2017 document date
2017 document date
In a loop, this can be done something like this:

$year = 0;
if ($row['year'] != $year) {
  $year = $row['year'];
  echo '<h2>'.$row['year'].'</h2>';
}

Is it possible to do something like this in a ListView?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Bay, 2018-05-30
@myks92

Maxim Vorozhtsov , I would refuse listview. why do you need it? Work with dataProvider (getModels()).
And the solution to your case, it is possible to pass the entire dataProvider array through additional parameters, compare the date with the previous element of the array, and display it.
UPD. Two more options came up while drinking tea:
2) inherit from listView, and store the year variable in it
3) store the year in some kind of shared storage
By the way, a few questions to think about
- what is the point of sorting by years like this? can initially build a data provider based on the year?
- perhaps it will be easier to check the boxes, and output as a regular array without a listview?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question