我正在使用 CActiveDataProvider 和 CListView 输出名称列表。仅当名称与其前面的名称相同时,我才想使用 html 将名称加粗。这些在数据库中不是相同的行,只有“名称”属性相同。
例如,我想要以下内容:
鲍勃
瑞克
史蒂夫
瑞克
瑞克
乔治
由于我使用的是 CListView,因此我使用 $data 中的信息创建部分 View ,其中包含当前列表项。如何访问前一个列表项的属性?
感谢世界各地的 Yii 老兵!
最佳答案
CListView
的每个 itemView
都是 passed a few variables automatically ,即:
$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.
(强调我的)
每个 CListView
小部件都有 property $dataProvider
,并且数据提供者具有属性 $data
which is :
<小时/>an array of the data items currently available (meaning in the current page, with the current sort etc.)
因此,在部分 itemView
中,我们可以访问 dataProvider
的整个数据集,如下所示:
$widget->dataProvider->data;
对于您的示例,类似这样的操作应该有效:
<?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; ?>
关于php - Yii 从 CListView 部分中访问先前的数据项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19760902/