php - Yii 从 CListView 部分中访问先前的数据项

标签 php listview yii partial cactivedataprovider

我正在使用 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/

相关文章:

facebook - 在 Hybridauth 中为 Yii 框架配置 Facebook

php - 通过 php 和 seo 重定向外部广告链接

java - 调用 setOnItemClickListener 时出现空指针异常

php - 我的大数字格式化功能表现异常

java - ListView内存问题

java - 里面有 listview 的 Viewpager

php - 如何在yii2中使用contact_ws?

php - Yii Booster 日期选择器无法正常工作

php - 在 Web 项目中,dist 和 build 目录(传统上)有什么区别?

javascript - 使用 jquery 填充下拉列表