I'm using CActiveDataProvider and CListView to output a list of names. I want to bold the name using html only when the name is the same as the name directly previous to it. These are not identical rows in the database, only the 'name' attribute is the same.

For example, I want the following:
Bob
Rick
Steve
Rick
Rick
George

Since I'm using CListView, I'm creating my partial view using information from $data, which contains the current list item. How do I access attributes from the previous list item?

Thanks Yii veterans of the world!

有帮助吗?

解决方案

Each itemView of a CListView is passed a few variables automatically, namely:

$this: refers to the owner of this list view widget. For example, if the widget is in the view of a controller, then $this refers to the controller.
$data: refers to the data item currently being rendered.
$index: refers to the zero-based index of the data item currently being rendered.
$widget: refers to this list view widget instance.

(emphasis mine)

And each CListView widget has the property $dataProvider, and a data provider has the property $data which is:

an array of the data items currently available (meaning in the current page, with the current sort etc.)


So in a partial itemView we can access the entire dataset of the dataProvider, like so:

$widget->dataProvider->data;

For your example, something like this should work:

<?php
     if ($index > 0 && $widget->dataProvider->data[$index-1]->name == $data->name):
?>
     <b><?php echo $data->name; ?></b>
<?php else:
     echo $data->test_message;
?>
<?php endif; ?>
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top