Yii2 - ListView搜索表单

时间:2015-12-19 11:12:59

标签: php listview search filter yii2

问题: 下面的所有代码都是使用dropDownList表单在ListView小部件中进行过滤。数据库模型中的表单获取分类表,它应该通过分类表中的内容过滤ListView $ dataProvider中的产品。

结果: 这是行不通的。当我按下表单中的分类按钮时没有任何反应。它只是闪烁并再次显示所有产品。

具有表单的屏幕和ListView在detailView中显示产品: enter image description here

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

视图2:

<?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 - &gt; _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'])),
        ],
    ],
]) ?>

2 个答案:

答案 0 :(得分:4)

您没有使用dataProvider ProdutosSearch模型。将您的控制器代码更改为: -

public function actionView2()
{

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

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

答案 1 :(得分:-1)

将此行添加到索引视图文件中

echo $this->render('_search', ['model' => $searchModel]);