This is an old revision of the document!
Yii 2 Report Filtering
Some reports need to reflect the filtered records from a search (eg. index view). For these filters, we need to take advantage of the search model.
Index View
In the view, add the query param filter to the action parameters:
<?php $curParams = Yii::$app->request->getQueryParams('SystemSearch') ?> <?= Html::a('<span class="glyphicon glyphicon-print" aria-hidden="true"></span> ' . Yii::t('app', 'Report'), ['report', 'SystemSearch' => (!empty($curParams['SystemSearch']) ? $curParams['SystemSearch'] : []) ], ['class' => 'btn btn-default'] ) ?>
And in the controller action, query data as follows:
public function actionReport() { $searchModel = new SystemSearch(); $dataProvider = $searchModel->search(Yii::$app->request->queryParams); $dataProvider->sort = ['defaultOrder' => ['vendor'=>SORT_ASC, 'name'=>SORT_ASC]]; $rows = $dataProvider->query->All(); Yii::$app->response->format = yii\web\Response::FORMAT_RAW; // Raw for PDF output return $this->render('report', [ 'data' => $rows, ]); }
See the full example:
<?php use yii\helpers\Html; use yii\grid\GridView; /* @var $this yii\web\View */ /* @var $searchModel app\models\ComplaintSearch */ /* @var $dataProvider yii\data\ActiveDataProvider */ $this->title = Yii::t('app', 'Complaints'); $this->params['breadcrumbs'][] = $this->title; ?> <div class="complaint-index"> <h1><?= Html::encode($this->title) ?></h1> <?php // echo $this->render('_search', ['model' => $searchModel]); ?> <p> <?php //echo '<div class="btn-group">' . //Html::a(Yii::t('app', 'Create Complaint'), ['create'], ['class' => 'btn btn-success']) . //'</div>'; ?> <div class="btn-group"> <?= Html::a('<span class="glyphicon glyphicon-erase" aria-hidden="true"></span> ' . Yii::t('app', 'Reset Search'), ['index'], ['class' => 'btn btn-default']) ?> </div> <?php $curParams = Yii::$app->request->getQueryParams('ComplaintSearch') ?> <!-- Generate Report Options --> <div class="btn-group"> <?= Html::a('<span class="glyphicon glyphicon-print" aria-hidden="true"></span> ' . Yii::t('app', 'Report') . ' <span class="caret">', '#', ['class' => 'btn btn-default dropdown-toggle', 'data-toggle'=>"dropdown", 'role'=>"button", 'aria-haspopup'=>"true", 'aria-expanded'=>"false" ]) ?> <ul class="dropdown-menu"> <li><?= Html::a(Yii::t('app', 'Report All'), ['report'], ['class' => '']) ?></li> <li><?= Html::a(Yii::t('app', 'Report Selected'), ['report', 'ComplaintSearch' => (!empty($curParams['ComplaintSearch']) ? $curParams['ComplaintSearch'] : []) ], ['class' => '']) ?></li> </ul> </div> </p> <?= GridView::widget([ 'dataProvider' => $dataProvider, 'filterModel' => $searchModel, 'columns' => [ ['class' => 'yii\grid\SerialColumn'], //... ], ]); ?> </div>
Controller
namespace app\controllers; use Yii; use app\models\Complaint; use app\models\ComplaintSearch; use yii\web\Controller; use yii\web\Response; use yii\web\NotFoundHttpException; use yii\filters\AccessControl; use yii\filters\VerbFilter; /** * ComplaintController implements the CRUD actions for Complaint model. */ class ComplaintController extends Controller { //... public function actionReport($title='') { date_default_timezone_set('America/New_York'); $searchModel = new ComplaintSearch(); $dataProvider = $searchModel->search(Yii::$app->request->queryParams); $rows = $dataProvider->query->All(); Yii::$app->response->format = Response::FORMAT_RAW; // Raw for PDF output return $this->render('report', [ 'data' => $rows, 'title' => $title, ]); } }
The data stored in $rows
will be filtered, so when we create a report view, it only shows these records.