public function actionMultiGrid() { $query = Reservation::find(); $searchModel = new ReservationSearch(); if (isset($_GET['ReservationSearch'])) { $searchModel->load(\Yii::$app->request->get()); $query->joinWith(['customer']); $query->andFilterWhere(['LIKE', 'customer.surname', $searchModel->getAttribute('customer.surname')]); $query->andFilterWhere(['id' => $searchModel->id, 'customer_id' => $searchModel->customer_id, 'room_id' => $searchModel->room_id, 'price_per_day' => $searchModel->price_per_day]); } $allAvgPc = $query->average('price_per_day'); $dataProvider = new ActiveDataProvider(['query' => $query, 'pagination' => ['pageSize' => 2]]); $Roomquery = \app\models\Room::find(); $RoomsearchModel = new \app\models\Room(); if (isset($_GET['Room'])) { $RoomsearchModel->load(\Yii::$app->request->get()); $Roomquery->andFilterWhere(['id' => $RoomsearchModel->id, 'floor' => $RoomsearchModel->floor, 'room_number' => $RoomsearchModel->room_number, 'has_conditioner' => $RoomsearchModel->has_conditioner, 'has_phone' => $RoomsearchModel->has_conditioner, 'has_tv' => $RoomsearchModel->has_conditioner, 'available_from' => $RoomsearchModel->has_conditioner]); } $RoomdataProvider = new ActiveDataProvider(['query' => $Roomquery, 'sort' => ['sortParam' => 'rooms-sort'], 'pagination' => ['pageSize' => 2, 'pageParam' => 'rooms-page']]); return $this->render("multiGrid", ['reservationsDataProvider' => $dataProvider, 'reservationsSearchModel' => $searchModel, 'roomsDataProvider' => $RoomdataProvider, 'roomsSearchModel' => $RoomsearchModel]); }
<?php use yii\grid\GridView; use yii\helpers\Html; use app\components\GridViewReservation; $roomsFilterData = yii\helpers\ArrayHelper::map(app\models\Room::find()->all(), 'id', function ($model, $defaultValue) { return sprintf('Floor: %d - Number: %d', $model->floor, $model->room_number); }); $sumPc = 0; $avgPc = 0; if (sizeof($dataProvider->getModels()) > 0) { foreach ($dataProvider->getModels() as $m) { $sumPc += $m->price_per_day; } $avgPc = $sumPc / sizeof($dataProvider->getModels()); } ?> <h2>Reservations</h2> <?php echo GridViewReservation::widget(['dataProvider' => $dataProvider, 'filterModel' => $searchModel, 'showFooter' => true, 'columns' => ['id', ['header' => 'Room', 'filter' => Html::activeDropDownList($searchModel, 'room_id', $roomsFilterData, ['prompt' => '--- all']), 'content' => function ($model) { return $model->room->floor; }], ['header' => 'Customer', 'attribute' => 'customer.surname'], ['attribute' => 'price_per_day', 'footer' => Yii::$app->formatter->asCurrency($allAvgPc, 'CNY')], 'date_from', 'date_to']]);