M
M
MouseZver2019-12-27 14:03:28
Yii
MouseZver, 2019-12-27 14:03:28

How to insert one gridview view into another view?

I have this same code found in different places

<div class="history-balance-index">

    <?= GridView::widget([
        'dataProvider' => $dataProvider,
        'filterModel' => $searchModel,
        'columns' => [
            ['class' => 'yii\grid\SerialColumn'],

            ['attribute' => 'user_id',
                'value' => function ($model) {
                    return $model->user->username;
                }
            ],
            [
                'attribute' => 'balance',
                'format' => 'raw',
                'value' => function ($model) {
                    return $model->balance . '<br>
                        <span class="text-success">' . $model->credit . '</span>';
                }
            ],
            [
                'attribute' => 'balance_up',
                'format' => 'raw',
                'value' => function ($model) {
                    return Html::tag('span', $model->balance_up . '<br>' . $model->credit_up . (($model->credit_up >= 0)), [
                        'class' => ($model->credit_up >= 0) ? 'text-success' : 'text-danger'
                    ]);
                }
            ],
            [
                'attribute' => 'type',
                'filter' => Html::activeDropDownList(
                    $searchModel,
                    'type',
                    HistoryBalance::getSortLabels(),
                    [
                        'everyday' => 'каждый день',
                        'class' =>
                            'form-control form-control-sm'
                    ]
                ),
            ],
            'comment',
        ],
    ]); ?>
</div>

and here I insert
<?php

use common\models\HistoryBalance;
use yii\grid\GridView;
use yii\helpers\ArrayHelper;
use yii\helpers\Html;
use yii\widgets\ListView;

/* @var $this yii\web\View */
/* @var $searchModel backend\models\HistoryBalanceSearch */
/* @var $dataProvider yii\data\ActiveDataProvider */

$this->title = Yii::t('app', 'History Balances');
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="history-balance-index">
<!---->
<!--    <h1>--><?//= Html::encode($this->title) ?><!--</h1>-->
<!---->
<!--    <p>-->
<!--        --><?//= Html::a(Yii::t('app', 'Create History Balance'), ['create'], ['class' => 'btn btn-success']) ?>
<!--    </p>-->
<!---->
<!--    --><?//= GridView::widget([
//        'dataProvider' => $dataProvider,
//        'filterModel' => $searchModel,
//        'columns' => [
//            ['class' => 'yii\grid\SerialColumn'],
//
//            ['attribute' => 'user_id',
//                'value' => function ($model) {
//                    return $model->user->username;
//                }
//            ],
//            [
//                'attribute' => 'balance',
//                'format' => 'raw',
//                'value' => function ($model) {
//                    return $model->balance . '<br>
//                        <span class="text-success">' . $model->credit . '</span>';
//                }
//            ],
//            [
//            'attribute' => 'balance_up',
//            'format' => 'raw',
//            'value' => function($model) {
//                return \yii\helpers\Html::tag('span', $model->balance_up . '<br>' . $model->credit_up . (($model->credit_up >= 0)), [
//                  'class' => ($model->credit_up >= 0) ? 'text-success' : 'text-danger'
//                ]);
//              }
//            ],
//            [
//                'attribute' => 'type',
//                'filter' => Html::activeDropDownList(
//                    $searchModel,
//                    'type',
//                    HistoryBalance::getSortLabels(),
//                    [
//                        'everyday' => 'каждый день',
//                        'class' =>
//                        'form-control form-control-sm'
//                    ]
//                ),
//            ],
//            'comment',
//
//        ],
//    ]); ?>
<!--    --><?//= GridView::widget([
//        'dataProvider' => $dataProvider,
//        'pager' => [
//            'hideOnSinglePage' => true,
////            'firstPageLabel' => Yii::t('main', 'First'),
//            'lastPageLabel'  => Yii::t('new', 'new'),
//        ],
////        'itemView' => 'new',
//        'layout' => '<div class="box box-solid"><div class="box-header"><div class="pull-right">{summary}</div></div></div><div class="row">{items}</div>{pager}',
//    ])?>
    <?= $this->render('new') ?>

</div>

tried this
<?= $this->render('new') ?>
and that
<?= GridView::widget([
        'dataProvider' => $dataProvider,
        'pager' => [
            'hideOnSinglePage' => true,
            'firstPageLabel' => Yii::t('main', 'First'),
            'lastPageLabel'  => Yii::t('new', 'new'),
        ],
        'itemView' => 'new',
        'layout' => '<div class="box box-solid"><div class="box-header"><div class="pull-right">{summary}</div></div></div><div class="row">{items}</div>{pager}',
    ])?>

not working so far

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim, 2019-12-27
@MouseZver

The same is true, only you name your variables. In controller:

$userDataProvider = new ()
$userSeachModel = $userDataProvider->...

$postDataProvider = new ()
$postSeachModel = $postDataProvider->...

You create everything in the controller and pass these variables to the view.
The result will look like this:
<?= GridView::widget([
        'dataProvider' => $userDataProvider ,
        'filterModel' => $userSeachModel,
        'columns' => []
]?>
....

<?= GridView::widget([
        'dataProvider' => $postDataProvider ,
        'filterModel' => $postSeachModel,
        'columns' => []
]?>

You can also use it without a search model if it's just displaying records and DataProvider on arrays.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question