【问题标题】:Yii2 - ListView search formYii2 - ListView 搜索表单
【发布时间】:2016-03-26 00:19:00
【问题描述】:

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

结果: 这是行不通的。当我按下表格中的类别按钮时,什么也没有发生。它只是闪烁并像以前一样再次显示所有产品。

带有表单和 ListView 的屏幕在 detailView 中显示产品:

产品控制器操作:

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

【问题讨论】:

    标签: php listview search filter yii2


    【解决方案1】:

    您没有使用dataProviderProdutosSearch 模型.. 将您的控制器代码更改为:-

    public function actionView2()
    {
    
        $searchModel = new ProdutosSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
    
        // render
        return $this->render('view2', [
            'dataProvider' => $dataProvider,
            'searchModel' => $searchModel
        ]);
    }
    

    【讨论】:

    • 更改并更新了问题,但它也不起作用。
    • 好的。我想通了。它现在正在工作。我会接受你的回答是正确的。非常感谢。这很简单。我只需要删除 -> $dataProvider = new ActiveDataProvider([...... 因为它再次从数据库中抛出所有字段。非常感谢。
    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-12
      • 2016-03-24
      • 2018-07-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多