Katik Dependent Dropdown



Category Model

public static function getCategories()
{
    return Self::find()->select(['name', 'id'])->indexBy('id')->column();
}


Sub Category Model

public static function getSubCatList($categoryID, $isAjax = false)
{
    $subCategory = self::find()
        ->where(['category_id' => $categoryID]);

    if ($isAjax == true) {
        return $subCategory->select(['id', 'name'])->asArray()->all();
    } else {
        return $subCategory->select(['name'])->indexBy('id')->column();
    }

}

Product model

public static function getProductList($categoryID, $subCatID, $isAjax = false)
{
    $product = self::find()
        ->where(['category_id' => $categoryID])
        ->andWhere(['sub_category_id' => $subCatID]);

    if ($isAjax) {
        return $product->select(['id', 'name'])->asArray()->all();
    } else {
        return $product->select(['name'])->indexBy('id')->column();
    }

}

_Form


use yii\helpers\Html;
use yii\widgets\ActiveForm;
use kartik\depdrop\DepDrop;
use app\models\Category;
use app\models\SubCategory;
use app\models\Product;
use yii\helpers\Url;

/* @var $this yii\web\View */
/* @var $model app\models\DepDemo */
/* @var $form yii\widgets\ActiveForm */
?>


<?php $form = ActiveForm::begin(); ?>

    <?= $form->field($model, 'category_id')->dropDownList(Category::getCategories(), ['id' => 'cat-id', 'prompt' => 'Select Category...']) ?>

    <?= $form->field($model, 'sub_category_id')->widget(DepDrop::classname(), [
        'data' => SubCategory::getSubCatList($model->category_id),
        'options' => ['id' => 'subcat-id', 'prompt' => 'Select Sub Category...'],
        'pluginOptions' => [
            'depends' => ['cat-id'],
            'placeholder' => 'Select Sub Category...',
            'url' => Url::to(['/dependent-demo/subcat'])
        ]
    ]) ?>

    <?= $form->field($model, 'product_id')->widget(DepDrop::classname(), [
        'data' => Product::getProductList($model->category_id, $model->sub_category_id),
        'options' => ['id' => 'product-id', 'prompt' => 'Select Prodcut...'],
        'pluginOptions'=> [
            'depends' => ['cat-id', 'subcat-id'],
            'placeholder' => 'Select Product...',
            'url' => Url::to(['/dependent-demo/product'])
        ]
    ]) ?>

    <div class="form-group">
        <?= Html::submitButton($model->isNewRecord ? 'Create' : 'Update', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
    </div>

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


DependentDemo Controller

public function actionSubcat() {
    \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
    if (isset(Yii::$app->request->post()['depdrop_parents'])) {
        $parents = Yii::$app->request->post('depdrop_parents');
        if ($parents != null) {
            $cat_id = $parents[0];
            return [
                'output' => SubCategory::getSubCatList($cat_id, true),
                'selected' => '',
            ];
        }
    }
    return ['output' => '', 'selected' => ''];
}

public function actionProduct() {
    \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
    if (isset(Yii::$app->request->post()['depdrop_parents'])) {
        $ids = Yii::$app->request->post('depdrop_parents');
        $cat_id = empty($ids[0]) ? null : $ids[0];
        $subcat_id = empty($ids[1]) ? null : $ids[1];
        if ($cat_id != null && $subcat_id != null) {
            return [
                'output' => Product::getProductList($cat_id, $subcat_id, true),
                'selected' => 1,
            ];
        }
    }
    return ['output' => '', 'selected' => ''];
}

Comments

  1. can u send me the database of this ??

    ReplyDelete
    Replies
    1. Are you sure i have just 4 fields in each table, it just a demo. If you still need it. Ping on hangout.

      Delete
  2. Hi. I tried to implement this but the subcategory stays at loading.
    Console says 500 (Internal Server Error)
    I hope you can help me.
    Thanks

    ReplyDelete
    Replies
    1. Did you follow the error?

      Delete
    2. Yes, It's already fixed. You can check my post here.

      https://thestoryyoudonotknow.wordpress.com/2018/05/27/depdrop-down/

      Delete
  3. Error in Sub Category Model
     // Error -> public static function getSubCatList($categoryID, $dependent = false)
     /*Correct->*/    public static function getSubCatList($categoryID, $isAjax = false)
        {

    Error in Product model
    // Error ->public static function getProductList($categoryID, $subCatID, $dependent = false)
     /*Correct->*/    public static function getProductList($categoryID, $subCatID, $isAjax = false)

    ReplyDelete

Post a Comment