我正试图从另一个视图正在使用的表中获取数据,我想知道是否可以在我的Yii2 Index
页面视图中显示这些数据,就像每个带有卡片视图和详细信息的项目一样。
就像我之前的回答一样: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>
关于在索引中使用它并在索引中显示表中的数据,我应该了解什么?
在这个示例中,我假设卡片都包含在dataProvider $dataProvider->models
中,并且每个模型都包含一个图像和一个标题
这可能是视图/项目的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>