简体   繁体   English

如何在Yii2 ListView中呈现具有不同视图的项目?

[英]How to render items with different views in Yii2 ListView?

I need to display the news with different views. 我需要以不同的视角来显示新闻。 Now I use this code for a presentation: 现在,我使用以下代码进行演示:

 <?= ListView::widget([
                   'dataProvider' => $dataProvider,
                   'itemView' => '_news',
                   'viewParams' => [
                       'fullView' => true,
                       'context' => 'main-page'
                   ]
               ]);
                ?>

My problem is this: The first 3 news show with large images, the other display with small images. 我的问题是:前三个新闻显示的图像较大,另一个显示的图像较小。 Pagination should work necessarily. 分页应该有效。 See this example 看这个例子 在此处输入图片说明

Please, help me. 请帮我。

Each item view of List view has a variable called $index which can be used to achieve your desired result. 列表视图的每个项目视图都有一个名为$index的变量,可用于实现所需的结果。 In _news.php you can do the following, _news.php中,您可以执行以下操作:

if($index < 3)
    $this->render('_news_big', ['model' => $model]);

else
    $this->render('_news_small', ['model' => $model]);

And have the actual code for the big blocks in _news_big.php and the code for small blocks in _news_small.php . 并有在_news_big.php大块的实际代码,并在_news_small.php小块代码。

The other way of doing it is to place the code for both the blocks inside _news.php , (although i don't prefer this method) 另一种方法是将代码放置在_news.php内的两个块中(尽管我不喜欢这种方法)

if($index < 3)
{
    //Code for big block
}
else
{
    //Code for small blocks
}

For more information about itemView, please refer http://www.yiiframework.com/doc-2.0/yii-widgets-listview.html#$itemView-detail 有关itemView的更多信息,请参考http://www.yiiframework.com/doc-2.0/yii-widgets-listview.html#$itemView-detail

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM