0

所以我创建了一个简单的搜索表单,并在我的控制器中创建了查询(我知道这不是最好的方法,应该在我的模型/搜索模型中完成,但暂时它会这样做)。

我想知道如何在带有标签 All 的两个下拉列表中向我的表单添加一个选项,如果通过将意味着查询的该部分未应用。

下面是我的看法

        <?php $form = ActiveForm::begin(['id' => 'home-search','method' => 'post', 'action' => Url::to(['productitem/search'])]); ?>

    <?= $form->field($productitem, 'name')->textInput(array('placeholder' => 'What are you looking for?'))->label(false) ?>

    <?= $form->field($productitem, 'brand_id')->dropDownList(

        ArrayHelper::map(ProductBrand::find()->all(),'id','name'),
        ['prompt'=>'Select Brand']

    )->label(false) ?>

    <?= $form->field($productitem, 'category_id')->dropDownList(

        ArrayHelper::map(ProductCategory::find()->all(),'id','name'),
        ['prompt'=>'Select Department']

    )->label(false) ?>

    <div class="form-group search-button">
        <button type="submit" class="btn btn-primary" name="login-button">Search <i class="fa fa-lg fa-arrow-circle-o-right"></i></button>
    </div>

    <?php ActiveForm::end(); ?>

下面是我的控制器/查询

    public function actionSearch()
{
    $query = ProductItem::find()
    ->andFilterWhere(['like', 'name', $_POST['ProductItem']['name']])
    ->andFilterWhere(['in', 'brand_id', $_POST['ProductItem']['brand_id']])
    ->andFilterWhere(['in', 'category_id', $_POST['ProductItem']['category_id']]);

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

    return $this->render('search', [
        'dataProvider' => $dataProvider,
    ]);

}
4

1 回答 1

0

希望这能提供一些参考。

<?= $form->field($model, 'categoryid1')->widget(Select2::className(), [
    'id' => 'categoryid1',
    'name' => 'categoryid1',
    'data' => ArrayHelper::map(Category::getCategoryByPid(0), 'id', 'name'),
    'options' => [
        'placeholder' => 'choose first',
    ],
    'pluginOptions' => [
        'allowClear' => true
    ],
])->label('first') ?>

<?= $form->field($model, 'categoryid2')->widget(DepDrop::className(), [
    'type' => DepDrop::TYPE_SELECT2,
    'id' => 'categoryid2',
    'name' => 'categoryid2',
    'data' => $model->categoryid1 ? ArrayHelper::map(Category::getCategoryByPid($model->categoryid1), 'id', 'name') : [],
    'pluginOptions' => [
        'depends' => ['product-categoryid1'],
        'placeholder' => 'choose second',
        'url' => Url::to(['/category/second'])
    ]
])->label('second'); ?>

<?= $form->field($model, 'categoryid')->widget(DepDrop::className(), [
    'type' => DepDrop::TYPE_SELECT2,
    'data' => $model->categoryid2 ? ArrayHelper::map(Category::getCategoryByPid($model->categoryid2), 'id', 'name') : [],
    'pluginOptions' => [
        'depends' => ['product-categoryid1', 'product-categoryid2'],
        'placeholder' => 'third',
        'url' => Url::to(['/category/third'])
    ]
])->label('third'); ?>
于 2017-04-06T05:19:54.100 回答