簡體   English   中英

如何在下拉菜單中添加all選項以查詢yii2中的所有類別?

[英]How can I add an all option to a dropdown to query all categories in yii2?

因此,我創建了一個簡單的搜索表單,並在控制器中創建了查詢(我知道這不是最好的方法,應該暫時在我的模型/搜索模型中完成)。

我想知道如何在兩個帶有標簽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,
    ]);

}

希望這會提供一些參考。

<?= $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'); ?>

暫無
暫無

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

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