簡體   English   中英

Yii2-ListView搜索表單

[英]Yii2 - ListView search form

問題:下面的所有代碼都是使用dropDownList表單在ListView小部件中進行過濾的。 表單以DB模型獲取類別表,並且應該按照類別表中的內容從ListView $ dataProvider過濾產品。

結果:它不起作用。 當我按表單中的類別按鈕時,什么也沒有發生。 它只是閃爍並再次顯示所有產品。

帶有表單和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,
    ]);
}

產品搜索模型:

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-> _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 。將控制器代碼更改為:-

public function actionView2()
{

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

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

將此行添加到索引視圖文件

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM