Yii2:依赖下拉列表

时间:2016-01-25 10:07:33

标签: php drop-down-menu yii2 yii2-advanced-app

我想在yii2-advanced app中使用依赖下拉列表,使用2个模型 - BusinessMainCategories(bmc_id, bmc_name) & BusinessSubCategories(bsc_id, bsc_name, bmc_id)。我已经使用位于前端的页面上的下拉列表。 我尝试过以下操作,但每次都会显示db中的所有子类别。

我的观看代码为 -

<?php $form = ActiveForm::begin(['options'=>['enctype'=>'multipart/form-data']]); ?>
    <?php
        $dataBMC = ArrayHelper::map(\backend\models\BusinessMainCategories::find()->asArray()->all(), 'bmc_id', 'bmc_name');
        echo $form->field($model, 'bmc_id')->dropDownList($dataBMC, 
         ['prompt'=>'Choose a Main Category',
            'style'=>'width:75%',
          'onchange'=>'
            $.post("index.php?r=business-sub-categories/lists&id='.'"+$(this).val(), 
                function(data) {
              $("select#business_sub_categories-bsc_id" ).html( data );
            });
        ']); 
        echo "<br>";
        $dataBSC = ArrayHelper::map(\backend\models\BusinessSubCategories::find()->asArray()->all(), 'bsc_id', 'bsc_name');
        echo $form->field($model, 'bmc_id')
            ->dropDownList(
                $dataBSC,           
                ['prompt'=>'Choose a Sub Category',
                'style'=>'width:75%',
                'id'=>'bmc_id']
            );
    ?>
    <div class="form-group"><br>
        &nbsp;&nbsp;&nbsp;&nbsp;<input type="submit" value="Submit" class='btn btn-success'>
    </div>
<?php ActiveForm::end(); ?>

我对应该在哪里编写列表功能代码感到困惑?我将它写在后端\ controllers \ BusinessSubCategoriesController中,如下所示 -

public function actionLists($id)
{
    $countBSCategories = BusinessSubCategories::find()->where(['bmc_id' => $id])->count();
    $businessSubCategories = BusinessSubCategories::find()->where(['bmc_id' => $id])->all();
    if ($countBSCategories > 0) {
        foreach ($businessSubCategories as $businessSubCategory) {
            echo "<option value='" . $businessSubCategory->bsc_id . "'>" . $businessSubCategory->bsc_name . "</option>";
        }
    } else {
        echo "<option> - </option>";
    }
}

我认为该功能没有得到&#39; bmc_id&#39;正常。请告诉我一个通过纠正我的错误使其依赖的解决方案......

2 个答案:

答案 0 :(得分:1)

我建议使用get方法,并且您对Dropdown使用相同的字段。所以,改变它。

echo $form->field($model, 'bmc_id')->dropDownList($dataBMC, 
                     ['prompt'=>'Choose a Main Category',
                        'style'=>'width:75%',
                      'onchange'=>'
          $.get( "'.Url::toRoute('business-sub-categories/lists').'", { id: $(this).val() })
         .done(function( data ) { $( "#'.Html::getInputId($model, 'attribute').'" ).html( data ); } );'
]); 

答案 1 :(得分:0)

相同的情景 -

<?= $form->field($model, 'category_id')->dropDownList(ArrayHelper::map(ItemCategory::find()->all(),'id','ic_code'),
    ['prompt'=>'Select Category',
    'onchange' => '
    $.post("index.php?r=receiving-order-details/lists&id=' . '"+$(this).val(),function(data){
      $("select#receivingorderdetails-sub_category_id").html(data);
    });']) ?>

<?= $form->field($model, 'sub_category_id')->dropDownList(ArrayHelper::map(ItemSubCategory::find()->all(),'id','isc_code'),['prompt'=>'Select Sub Category']) ?>