unclead/yii2-multiple-input

Adding new row with DepDrop suddenly not working.

sayed201201180 opened this issue · 0 comments

I used to be able to add new rows with fully functional dependent dropdowns. I messed around with other plugins for multiple input, but I didn't change any of the code for this page. Now it is not working. It is very strange. Could it be all because I installed the other packages and they modified something? Any help is appreciated. Thanks.

Console error:

image

View:

        [
            'options'=>['id'=>'select-attributes']
        ])->widget(MultipleInput::className(), [
            'id' => 'my_id',
            'min' => 1,
            'cloneButton' => false,
            'enableError' => true,
            'rowOptions' => [
                'id' => 'row{multiple_index_my_id}',
            ],
            'columns' => [
                [
                    'name'  => 'id',
                    'type'  => 'hiddenInput',
                    'options' => [
                        'id'=>'id_{multiple_index_my_id}',
                    ]
                ],
                [
                    'name'  => 'attribute_id',
                    'type'  => 'dropDownList',
                    'title' => 'Attribute',
                    'items' => ArrayHelper::map($attributes,'id','attribute_name'),
                    'options' => [
                        'id'=>'attribute_id_{multiple_index_my_id}',
                    ],
                ],
                
                [
                    'name'  => 'attribute_value_id',
                    'type'=> DepDrop::classname(),
                    'title' => 'Value',
                    'options'=> [
                        'id' => 'attribute_value_id',
                        
                        'pluginOptions' => [
                            'depends' => ['attribute_id_{multiple_index_my_id}'],
                            'url'=>Url::to(['shop-item/get-attribute-values']),
                            'initialize' => true,
                            'initDepends'=>['attribute_id_{multiple_index_my_id}'],
                            
                        ],
                        'options' => ['placeholder' => 'Choose ...']
                    ],                    
                ],
                
                [
                    'name'  => 'additional_charge',
                    'type'  => 'textInput',
                    'title' => 'Additional Charge',
                ], 
            ]
            
        ])->label('Attributes');

Controller:

public function actionSubItems($id)
    {
        $item = ShopItem::find()->where(['id'=>$id, 'shop_id'=>Yii::$app->params['shop_id'] ])->one();
        $model = new SubItem();
        $attributes = Attribute::find()->where(['shop_id'=>Yii::$app->params['shop_id']])->orderBy(['attribute_name' => SORT_ASC])->all();
                    
        if ($model->load(Yii::$app->request->post())) {
        
            $selected_attributes = Yii::$app->request->post("SubItem")["selected_attributes"];
            ...            
        }

        return $this->render('sub-items', [
            'model' => $model,
            'item'=>$item,
            'attributes' => $attributes,
        ]);
    }

    public function actionGetAttributeValues() {
        Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
        
        if (isset($_POST['depdrop_parents'])) {
            $parents = $_POST['depdrop_parents'];
            if ($parents != null) {
                $attr_id = $parents[0];
                
                $attribute = Attribute::find()->where(['id'=>$attr_id])->one();
                $data = null;
                foreach ($attribute->attributeValues as $value){
                    $data[] = ["id"=>$value->id, "name"=>$value->value];
                }
    
                return ['output'=>$data, 'selected'=>''];
            }
        }
        return ['output'=>'', 'selected'=>''];
    }