Yii2 - DetailView搜索框

时间:2015-12-17 21:38:50

标签: search filter widget yii2 detailview

我在ProdutosController中有这个动作。它呈现了view2,它有一个指向_view2的项目视图,它有一个DetailView来显示产品。

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,
    ]);
}

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/>

和作为DetailView的_view2:

<?= 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'])),
        ],
    ],
]) ?>

在另一个项目中,我能够在_view2中通过render _search.php在其中创建一个搜索框,并修改ProdutosSearch模型。但这是通过一个GridView实现的,它直接在小部件中接受$ dataprovider和$ searchModel。

现在在我的新项目中,我正在努力制作一个搜索输入框来过滤DetailView显示的内容(它显示产品的照片,名称,类别等)但不能这样做是因为详细信息视图不接受$ dataProvider数据。

如何使用 DetailView 解决此问题并使用搜索框制作产品库。

修改

控制器操作:

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,
    ]);

}

} ?&GT;

视图2

<?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 个答案:

答案 0 :(得分:1)

您可以在视图内的部分轻松添加操作所需的字段, 搜索按钮将值提交给您用于搜索的操作,您可以将结果重定向到所需的控制器。

在操作表单中,您可以设置目标控制器/操作。以这种方式按下提交按钮,提交您所拥有的控制器/操作中的值,例如:

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,
        ]);

    }
}

答案 1 :(得分:0)

首先在该页面上添加搜索框:

<?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>

然后在模型中准备一个过滤方法。

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;
}