Answer the question
In order to leave comments, you need to log in
Yii2 gridviews (SqlDataProvider) how to organize search?
In view:
GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $modelSearch,
'columns' => [
'id',
[
'label' => 'Логин',
'attribute' => 'username',
],
[
'label' => 'Имя',
'attribute' => 'firstname',
],
[
'label' => 'Фамилия',
'attribute' => 'lastname',
],
[
'label' => 'E-Mail',
'attribute' => 'email',
],
],
]);
$totalCount = Yii::$app->db->createCommand('SELECT COUNT(*)
FROM Users
Where validation_email=:status', [':status' => 'confirmed'])
->queryScalar();
$sql = 'SELECT * FROM Users Where validation_email=:status';
$dataProvider = new SqlDataProvider([
'sql' => $sql,
'params' => [':status' => 'confirmed'],
'totalCount' => (int)$totalCount,
'sort' => [
'attributes' => [
'id',
'username',
'lastname',
'firstname',
'email',
],
],
'pagination' => [
'pageSize' => 10,
],
]);
Answer the question
In order to leave comments, you need to log in
Through modelsearch. When generating crud through gii, even a file with filters should have been created (If serchmodel was specified). The _search.php file in the views folder
with something like the following code:
<?php $form = ActiveForm::begin([
'action' => ['index'],
'method' => 'get',
]); ?>
<div class="col-xs-3"><?= $form->field($model, 'text') ?></div>
<div class="col-xs-3"><?= $form->field($model, 'cat') ?></div>
<div class="col-xs-3"><?= $form->field($model, 'status')->dropDownList($model->getStatusList(),['prompt'=>''])
?></div>
<div class="col-xs-3"><?= $form->field($model, 'email') ?></div>
<div class="form-group col-xs-12">
<?= Html::submitButton(Yii::t('app', 'Search'), ['class' => 'btn btn-primary']) ?>
<?= Html::resetButton(Yii::t('app', 'Reset'), ['class' => 'btn btn-default']) ?>
<?= Html::a(Yii::t('app', 'Создать Запрос'), ['create'], ['class' => 'btn btn-success']) ?>
</div>
<?php ActiveForm::end(); ?>
public function actionIndex()
{
$searchModel = new RequestSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
}
The same problem of creating a search in the SQLdataProvider+GridViews bundle. I
reviewed a bunch of articles, but I still don't understand how to organize a search or a filter.
The controller and the view were redone after gii by hand, there is a _search file.
Maybe someone will show a normal, working example (controller view model). I so understood with SQLdataProvider in general the filter cannot be made.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question