Yii2-ListView搜索表单 [英] Yii2 - ListView search form

查看:90
本文介绍了Yii2-ListView搜索表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题: 下面的所有代码都是使用dropDownList表单在ListView小部件中进行过滤的.表单以DB模型获取类别表,并且应该按照类别表中的内容从ListView $ dataProvider中过滤产品.

The problem: All the code below was made to filter in a ListView widget using a dropDownList form. The form fetch categorias table in DB model and it was supposed to filter products from the ListView $dataProvider by what is in categorias table.

结果: 这没用.当我按表单中的类别按钮时,什么也没有发生.它只是闪烁并再次显示所有产品.

Results: It does not work. Nothing happens when i press the categorias button in the form. It just flickers and show all Products again as it was before.

具有表单和ListView的屏幕,其中在detailView中显示了产品:

Produtos Controller操作:

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

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

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

ProdutosSearch模型:

class ProdutosSearch extends Produtos
{
public $procuraglobal;
/**
 * @inheritdoc
 */
public function rules()
{
    return [
        [['id', 'promo', 'maisvendido'], 'integer'], 
        [['foto', 'categoria', 'nome', 'valor', 'descricao', 'dummy1', 'dummy2', 'dummy3', 'dummy4', 'dummy5', 'procuraglobal'], 'safe'],
        [['foto', 'categoria', 'nome', 'valor', 'dummy1', 'dummy2', 'dummy3', 'dummy4', 'dummy5'], 'string', 'max' => 255]     
    ];
}

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

/**
 * Creates data provider instance with search query applied
 *
 * @param array $params
 *
 * @return ActiveDataProvider
 */
public function search($params)
{
    $query = Produtos::find();

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

    $this->load($params);

    if (!$this->validate()) {
        // uncomment the following line if you do not want to return any records when validation fails
        // $query->where('0=1');
        return $dataProvider;
    }

    $query->andFilterWhere([
        'id' => $this->id,
        'promo' => $this->promo,
        'maisvendido' => $this->maisvendido,
    ]);

    $query->andFilterWhere(['like', 'foto', $this->foto])
        ->andFilterWhere(['like', 'categoria', $this->categoria])
        ->andFilterWhere(['like', 'nome', $this->nome])
        ->andFilterWhere(['like', 'valor', $this->valor])
        ->andFilterWhere(['like', 'descricao', $this->descricao])
        ->andFilterWhere(['like', 'dummy1', $this->dummy1])
        ->andFilterWhere(['like', 'dummy2', $this->dummy2])
        ->andFilterWhere(['like', 'dummy3', $this->dummy3])
        ->andFilterWhere(['like', 'dummy4', $this->dummy4])
        ->andFilterWhere(['like', 'dummy5', $this->dummy5]);

    return $dataProvider;
}

View2:

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

    <!--<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(); ?>

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

view2的itemView-> _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'])),
        ],
    ],
]) ?>

推荐答案

您未使用ProdutosSearch模型的dataProvider ..将控制器代码更改为:-

You are not using dataProvider of ProdutosSearch model .. Change your controller code as: -

public function actionView2()
{

    $searchModel = new ProdutosSearch();
    $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

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

这篇关于Yii2-ListView搜索表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆