dream07769
dream07769
2015-12-08 17:18
浏览 61
已采纳

使用另一个视图在Yii2中的索引(或另一个视图)上的GridView

I'm trying to get the datas from a table which those datas are using by another view and i was wondering if i could show them in my Yii2 Index page view like each item with a cardview and details.

Exactly like my previous answer about that: https://stackoverflow.com/a/33847647/4960200

Is this possible?

Where or how can I use the datas from that table in my Yii2 index?

Or honestly, I couldn't do that in my views/site/index.php like this:

<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>

What should I learn about using this in my index and showing those datas from a table in index?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongluanguang4774
    dongluanguang4774 2015-12-08 20:53
    已采纳

    In this sample i suppose that the cards are all contained in the dataProvider $dataProvider->models and for each model contain an image and a title

    this could be the index.php for views/project

        <?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>
    
    点赞 评论

相关推荐