1
votes

I have this action inside ProdutosController. It renders view2 that has an itemview pointing to _view2 which has a DetailView to show products.

public function actionView2()
{   
    $model = new Produtos();
    $dataProvider = new ActiveDataProvider([
    'query' => Produtos::find(),
    'pagination' => false,
    ]);

    // get the posts in the current page
    $posts = $dataProvider->getModels();

    // render
    return $this->render('view2', [
        'dataProvider' => $dataProvider,
        'model' => $model,
    ]);
}

the view2

<?php

use yii\helpers\Html;
use yii\widgets\ListView;
use yii\helpers\Url;

/* @var $this yii\web\View */
/* @var $model app\models\Atividades */

// $this->title = implode('', $cat);
?>
<div class="produtos-view2">
    <?php
    //use kartik\social\FacebookPlugin;
    //echo FacebookPlugin::widget(['type'=>FacebookPlugin::SHARE, 'settings' `=> []]);`
    ?>
</div>

<?= ListView::widget([
    'dataProvider' => $dataProvider,
    'itemView' => '_view2',
]); ?>

<br/><br/>

and the _view2 that as DetailView inside:

<?= DetailView::widget([
    'model' => $model,
    'options' => ['class' => 'detail1-galeria-view2'],
    'attributes' => [
        // cria um array com a fotografia, em que carrega a path no campo fieldName da bd
        [
            'attribute'=>'',
            //'value'=>$model->foto,
            'value'=>Html::a(Html::img(Yii::$app->getUrlManager()->getBaseUrl() . "/" .$model->foto, ['width'=>'192', 'height' => "256"]), $model->foto),
            'format' => 'raw',
        ],
        [
        'attribute'=>'',
        'value'=>$model->nome,
        ],
        [
        'attribute'=>'',
        'value'=>$model->categoria,
        ],
        [
        'attribute'=>'',
        'value'=>$model->descricao,
        ],
        [
        'attribute'=>'',
        'value'=>$model->valor.' '.'€',
        ],
        // info
        [
        'attribute'=>'',
        'format' => 'raw',
        'value'=> Html::a(Yii::t('app','Comprar'), Url::toRoute(['contacto/create2'])),
        ],
    ],
]) ?>

In another project i was able to make a search box in _view2 by render _search.php inside it and by modifying the ProdutosSearch model. But this was achieved with a GridView that accepts $dataprovider and $searchModel directly inside the widget.

Now in my new project, i'm trying hard to make a search input box to filter what the DetailView shows (it shows the foto, name, category, etc... of products) but cannot do it because the detail view doesn't accept $dataProvider data.

How can i solve this and make a products gallery with search box using a DetailView.

EDIT

Controller action:

public function actionMySearchFunction()
{   
    $model = new Produtos();
    $searchModel = new CategoriasSearch();
    $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
    $dataProvider = new ActiveDataProvider([
    'query' => Produtos::find(),
    'pagination' => false,
    ]);

    // get the posts in the current page
    $posts = $dataProvider->getModels();

    // render
    return $this->render('view2', [
        'dataProvider' => $dataProvider,
        'searchModel' => $searchModel,
        'model' => $model,
    ]);
}

MySearchFunction.php

<?php
public function MySearchFunction()

{
$post = Yii::$app->request->post(); 

if (!empty($post)){

    $postParam1 = $post['categoria']; // get the value ffrom the filter field submitted) 

    $query = ProdutosSearch::find()->where(['categoria' =>  $postParam1]);

    $dataProvider = new \yii\data\ActiveDataProvider([
        'query' => $query,
    ]);

    $dataProvider->pagination->pageSize=15;

    return $this->render('view2', [
        'dataProvider' => $dataProvider,
    ]);

}

} ?>

VIEW2

<?php $form = ActiveForm::begin([
    'action' => Url::to(['/produtos/my-search-function']),
    'method' => 'get',
    'options' => ['class' => 'form-inline form-group form-group-sm col-xs-12'],
    'fieldConfig' => [
        'template' => "{input}",
    ],
]); ?>
</div>
<?php echo var_dump(Url::to(['/produtos/my-search-function'])); ?>

<!--<nobr><?= $form->field($model, 'nome')->textInput(['placeholder' => 'Nome']) ?>-->

<nobr>
    <?= $form->field($searchModel, 'categoria')->dropDownList(ArrayHelper::map(Categorias::find()->all(), 'categoria','categoria'), ['prompt'=>Yii::t('yii', 'Escolha a categoria...')])  ?>
    <?= Html::submitButton(Yii::t('app', 'Pesquisar'), ['class' => 'btn btn-warning']) ?>
</nobr>
<?php ActiveForm::end(); ?>

<div class="user-view">

    <?= ListView::widget([
        'dataProvider' => $dataProvider,
        'itemView' => '_view2',
    ]); ?>

    <br/><br/>

</div>
2

2 Answers

1
votes

You can easily add the fields you need in an action from section inside the view, the search button submit the value to an action you use for search and the you can redirect the resulto to the controller you need.

In the action Form you can set the target controller/action. In this way pressing the submit botton the value in the submitted the the controller/action you have eg:

public function actionMySearchFunction()
{

    $post = Yii::$app->request->post(); 

    if (!empty($post)){

        $postParam1 = $post['name_field_Filter1']; // get the value ffrom the filter field submitted) 

        $postParam2 = $post['name_field_Filter2'];

        $query = YourModel::find()->where(['field1' =>  $postParam1, 'field2' =>  $postParam2]);

        $dataProvider = new \yii\data\ActiveDataProvider([
            'query' => $query,
        ]);

        $dataProvider->pagination->pageSize=15;

        return $this->render('your_view', [
            'dataProvider' => $dataProvider,
        ]);

    }
}
0
votes

first add search box on that page :

<?php

use yii\helpers\Html;
use yii\widgets\ActiveForm;

/* @var $form yii\widgets\ActiveForm */
?>

<div class="student-form">
    <?php $form = ActiveForm::begin(); ?>

    <?= $form->field($searchModel, 'nome') ?>

    <?= $form->field($searchModel, 'categoria') ?>

    <div class="form-group">
        <?= Html::submitButton('Apply', ['class' => 'btn btn-success']) ?>
    </div>

    <?php ActiveForm::end(); ?>
</div>

then in model prepare a filter method.

public function rules()
{
    return [
        ['nome', 'string'],
        ['categoria', 'string'],
    ];
}

public function scenarios()
{
    // bypass scenarios() implementation in the parent class
    return Model::scenarios();
}

public function search($params)
{
    $query = Produtos::find();

    $dataProvider = new ActiveDataProvider(['query' => $query]);

    if (!($this->load($params) && $this->validate())) {
        return $dataProvider;
    }

    $query->andFilterWhere(['like', 'nome', $this->nome])
        ->andFilterWhere(['like', 'categoria', $this->categoria]);

    return $dataProvider;
}