我试图从另一个视图使用这些数据的表中获取数据,我想知道我是否可以在我的Yii2 Index
页面视图中显示它们,就像每个带有cardview和详细信息的项目一样
完全像我之前的回答: https://stackoverflow.com/a/33847647/4960200
这可能吗?
我可以在Yii2 index
?
老实说,我不能在views/site/index.php
这样做:
<div class="container" style="margin-top: 10px; background-color: white">
<div class="row">
<a class="btn btn-raised btn-sm btn-success pull-right" style="margin-right: 20px;margin-top: 20px">MORE</a>
<h3 style="margin-left: 20px">Something</h3>
<div class="container" style="margin-top: 20px">
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
[
'attribute' => 'yuor_attibute', // you can use a dummy attribute in this case
'label' => 'your label',
'format' => 'raw',
'value' => function ($model) {
return "<a href='./yourPath/view?id=". $model->your_column ."' class = 'btn btn-success glyphicon glyphicon-user ' > </a>";
},
'contentOptions' => ['style' => 'width:80px; text-align: center;'],
'headerOptions' => ['style' => 'text-align: center;'],
],
[
'attribute' => 'yuor_attibute', // you can use a dummy attribute in this case
'label' => 'your 2 label',
'format' => 'raw',
'value' => function ($model) {
return "<img src='./yourPath/image.jpg">";
},
'contentOptions' => ['style' => 'width:400; height 400 px;'],
'headerOptions' => ['style' => 'text-align: center;'],
],
[
'attribute' => 'yuor_attibute', // you can use a dummy attribute in this case
'label' => 'your 3 label',
'format' => 'raw',
'value' => function ($model) {
return "< ****the html you prefer ***>";
},
'contentOptions' => ['style' => 'width:400; height 400 px;'],
'headerOptions' => ['style' => 'text-align: center;'],
],
</div>
</div>
我应该在索引中使用它并从索引中的表中显示这些数据,我应该学到什么?
答案 0 :(得分:1)
在此示例中,我假设这些卡都包含在dataProvider $dataProvider->models
中,并且每个模型都包含图像和标题
这可能是views / project
的index.php <?php
use yii\helpers\Html;
// title and breadcrumbs not for the moment
?>
<div class="project-index">
<h2>CARD INDEX TITLE</h2>
<?php // echo $this->render('_search', ['model' => $searchModel]); ?>
<?php
$nCardXRow = 4;
$cnt = 0;
echo "<div class='row'>"; // open a new row of 4 card
foreach ($dataProvider->models as $card) {
echo "div class='card-container col-md-3'><div><img src='" . $card->header.jpg . "'></div><div>" . $card->title . "</div></div>";
$cnt++;
if ($cnt == $nCardXRow) {
echo "</div><!-- close row -->";
echo "<div class='row'>"; // open a new row
$cnt = 0;
}
echo "</div><!-- close row last row-->";
}
?>
</div>