1
votes

Here am trying Dependent dropdown using kartik depdrop yii2 extension . the process of this dependent dropdown is, if i select a productname it will show me the dependent batchno, then if select a batchno, it will show the dependent itemid.

Actually first level is working perfectly, if i select a productname, it will show me batchno, this action is working perfectly, but the problem on the second level. if i select a batchno it need to show me a itemid, this action is not working

And am getting error as this image - Empty Result

Controller

public function actionSubcat() {
    $out = [];
    if (isset($_POST['depdrop_parents'])) {
        $parents = $_POST['depdrop_parents'];
        if ($parents != null) {
            $cat_id = $parents[0];
            $out = Productbatch::getBatchNo($cat_id);
            echo Json::encode($out);
            // the getSubCatList function will query the database based on the
            // cat_id and return an array like below:
            // [
            //    ['id'=>'<sub-cat-id-1>', 'name'=>'<sub-cat-name1>'],
            //    ['id'=>'<sub-cat_id_2>', 'name'=>'<sub-cat-name2>']
            // ]
            //echo Json::encode(['output'=>$out, 'selected'=>'']);
            return;
        }
    }
    echo Json::encode(['output'=>'', 'selected'=>'']);
    }

    public function actionProd() {
    $out = [];
    if (isset($_POST['depdrop_parents'])) {
        $ids = $_POST['depdrop_parents'];
        $cat_id = empty($ids[0]) ? null : $ids[0];
        $subcat_id = empty($ids[1]) ? null : $ids[1];
        if ($cat_id != null) {
           $data = Productbatch::getItemid($cat_id, $subcat_id);
            /**
             * the getProdList function will query the database based on the
             * cat_id and sub_cat_id and return an array like below:
             *  [
             *      'out'=>[
             *          ['id'=>'<prod-id-1>', 'name'=>'<prod-name1>'],
             *          ['id'=>'<prod_id_2>', 'name'=>'<prod-name2>']
             *       ],
             *       'selected'=>'<prod-id-1>'
             *  ]
             */
           echo Json::encode($out);
           //echo Json::encode(['output'=>$out, 'selected'=>$data['selected']]);
           return;
        }
    }
    echo Json::encode(['output'=>'', 'selected'=>'']);
}

_form

<?= $form->field($model, 'productname')->widget(Select2::classname(), [
    'data' => ArrayHelper::map(Productnames::find()->all(),'productnames_productname','productnames_productname'),
    'language' => 'en',
    'options' => ['placeholder' => 'Select Product Name', 'id' => 'cat-id'],
    'pluginOptions' => [
        'allowClear' => true
    ],
    ]); ?>

    <?= $form->field($model, 'batchno')->widget(DepDrop::classname(), [
    'options'=>['id'=>'subcat-id'],
    'pluginOptions'=>[
        'depends'=>['cat-id'],
        'placeholder'=>'Select BatchNo',
        'url'=>Url::to(['/production/productbatch/subcat'])
    ]
    ]); ?>

    <?= $form->field($model, 'itemid')->widget(DepDrop::classname(), [
    'pluginOptions'=>[
        'depends'=>['cat-id', 'subcat-id'],
        'placeholder'=>'Select ItemId',
        'url'=>Url::to(['/production/productbatch/prod'])
    ]
    ]); ?>

Model

public static function getBatchNo($cat_id)
    {
        $out = [];
        $data = Productbatch::find()
                ->where(['productname' => $cat_id])
                ->asArray()
                ->all();
        foreach ($data as $dat) {
            $out[] = ['id' => $dat['itemid'], 'name' => $dat['batchno']];
        }
        return $output = [
            'output' => $out,
            'selected' => ''
        ];
    }

    public static function getItemid($cat_id, $subcat_id)
    {
        $out = [];
        $data = Productbatch::find()
                ->where(['productname' => $cat_id])
                ->andWhere(['batchno' => $subcat_id])
                ->asArray()
                ->all();

        $selected = '';

        foreach ($data as $dat => $datas) {
            $out[] = ['id' => $datas['itemid'], 'name' => $datas['itemid']];

            if($dat == 0){
                    $aux = $datas['itemid'];
                }

            ($datas['productname'] == $cat_id) ? $selected = $cat_id : $selected = $aux;

        }
        return $output = [
            'output' => $out,
            'selected' => $selected
        ];
    }
1

1 Answers

2
votes

You need do like this in your controller. You have to create new action in controller .

public function actionState() {
        $country_id = $_POST['depdrop_parents'][0];
        $state = State::find()->where(['country_id' => $country_id])->all();
        $all_state = array();
        $i = 0;
        foreach ($state as $value) {
            $all_state[$i]['id'] = $value['state_id'];
            $all_state[$i]['name'] = $value['state_name'];
            $i++;
        }
        echo Json::encode(['output' => $all_state, 'selected' => '']);
        return;
    }

i also suffered from this problem but finally I solved this issue..