SirPereira
(Ivo Pereira)
January 23, 2015, 7:55pm
1
Hello guys, I’m using Yii2 and I’m struggling to get a GridView working beside an ActiveForm.
I’m looking to use an ActiveForm not really to create any new model entry, but to filter GridView data. Yeah, I need now GridView already has filters, but I need them to be outside of the form, being hidden and only visible on a button click, that’s why I opted for an ActiveForm solution.
Any idea how should I implement this?
Thanks
Pasman
(Pasman P)
January 23, 2015, 9:56pm
2
Use normal form:
echo Html::beginForm();
...fields...
echo Html::endForm();
echo GridView::widget([
...options...
]);
SirPereira
(Ivo Pereira)
January 23, 2015, 11:30pm
3
That would not work directly with GridView.
This is my current try:
<?php
use yii\grid\GridView;
use yii\widgets\Pjax;
use app\models\PagePost;
use yii\helpers\Url;
use yii\helpers\Html;
use yii\widgets\ActiveForm;
$baseUrl = Url::base(true);
$searchModel = new PagePost();
?>
<?php
Pjax::begin();
$form = ActiveForm::begin(['options' => ['data-pjax' => true ]]);
$_post = Yii::$app->request->post();
$searchModel->page_id = $page_id;
$searchModel->attributes = $_post;
if (!$searchModel->validate()) {
echo '<pre>';
print_r($searchModel->getErrors());
}
?>
<?=$form->field($searchModel,'message'); ?>
<?= Html::submitButton('Submit', ['class'=> 'btn btn-primary']) ;?>
<div class="bg-white p-10 text-center">
<?php $dataProvider = $searchModel->search( $_post ); ?>
<?= GridView::widget([
'dataProvider' => $dataProvider,
'summary' => '<div class="text-left text-small m-b-10">A mostrar <strong>{begin}-{end}</strong> de <strong>{totalCount}</strong> resultados</div>',
'headerRowOptions' => [
'class' => 'text-small'
],
'tableOptions' => [
'class' => 'table table-condensed text-left'
],
'rowOptions' => [
'class' => 'v-a-middle'
],
'footerRowOptions' => [
'class' => 'text-center'
],
'columns' => [
'message',
'total_interaction',
],
]);
ActiveForm::end();
Pjax::end(); ?>
</div>
This does search correctly in the model, and filter the data in it, however I would like to have something to this without needing to press a button (like GridView filter) and I would like the value to be kept in the input when form is submit.
exe810
(Exe810)
March 25, 2017, 2:15pm
4
In the View:
<?php $form = ActiveForm::begin([
'action' => ['index'],
'method' => 'get',
]); ?>
...fields...
<?php ActiveForm::end(); ?>
<?= GridView::widget([
//...options...
]);?>
In the Controller:
$model = new Matchs();
$q = ...some empty/initial query...
if ( $model->load(Yii::$app->request->get()) ) {
$q = //...Your query with form filters...
}
$dataProvider = new ArrayDataProvider([
'key'=> 'id',
'allModels' => $q,
'sort' => ['attributes' => ['id',
'matchdate', 'schedule','teams', 'goals','comments']],
'pagination' => [
'pageSize' => 15,
],
]);
return $this->render('index', [
'dataProvider' => $dataProvider, 'model' => $model]);