获取 ListView 的 itemView 中的项目计数 - Yii2

mel*_*tra 1 php web yii2

堆栈溢出

如何获取 itemView 计数。我想对 ListView 中的每第三个项目执行一些操作。

echo ListView::widget([
    'dataProvider' => $modelDataProvider,
    'itemView'     => '_item_view',
    'viewParams'   => [
        'count' => $itemcount // How to get this variable?
    ],
])

// _item_view.php

if($count % 3 == 0) {
    // this item is the third in a row, do something special
}
Run Code Online (Sandbox Code Playgroud)

任何帮助表示赞赏!

mel*_*tra 5

找到了答案。Yii2自己ListView注入参数。$index

您可以在 itemView 中使用该参数。