在kartik DepDrop Am获得二级下拉列表的错误

时间:2015-10-03 12:43:39

标签: php yii2

这里使用kartik depdrop yii2扩展尝试Dependent下拉列表。 这个依赖下拉列表的过程是,如果我选择一个company_name,它将显示依赖的employee_code,然后如果选择一个employee_code,它将显示依赖的employee_name。

实际上第一级工作正常,如果我选择一个company_name,它会显示employee_code,这个动作工作正常,但问题就在二级。如果我选择一个employee_code,它需要向我显示一个employee_name,这个动作不起作用

我的错误是

PHP Notice 'yii\base\ErrorException' with message 'Undefined variable: out' 

in C:\wamp\www\fiducial\backend\models\Employee.php:140

我在这里粘贴了以下代码,请帮我解决问题

<?= $form->field($model, 'company_name')->dropDownList($data1,
                ['prompt' => 'Select Company Name..', 'id' => 'cat-id']
            ) ?>

<?= $form->field($model, 'employee_id')->widget(DepDrop::classname(), [
                'options'=>['id'=>'subcat-id'],
                'pluginOptions'=>[
                    'depends'=>['cat-id'],
                    'placeholder'=>'Select...',
                    'url'=>yii\helpers\Url::to(['claim/subcat'])
                ]
            ]);  ?>

 <?php $form->field($model, 'employee_name')->widget(DepDrop::classname(), [
            'pluginOptions'=>[
                'depends'=>['cat-id', 'subcat-id'],
                'placeholder'=>'Select...',
                'url'=>yii\helpers\Url::to(['claim/claimername'])
            ]
        ]); ?>

控制器

   public function actionSubcat() 
    {
        $out = [];
        if (isset($_POST['depdrop_parents'])) {
            $parents = $_POST['depdrop_parents'];
            if ($parents != null) {
                $cat_id = $parents[0];
                $out = Employee::getSubCatList($cat_id);
                echo Json::encode($out);
                return;
            }
        }
        echo Json::encode(['output'=>'', 'selected'=>'']);
    }

public function actionClaimername() 
    {
        $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 = Employee::getClaimerNameList($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);
               return;
            }
        }
        echo Json::encode(['output'=>'', 'selected'=>'']);
    }

模型

public static function getSubCatList($cat_id)
    {
        $data = Employee::find()
                ->where(['importcompany_id' => $cat_id])
                ->andWhere(['!=', 'status', 'Deleted'])
                ->groupBy(['employee_id'])
                ->asArray()
                ->all();
        foreach ($data as $dat) {
            $out[] = ['id' => $dat['id'], 'name' => $dat['employee_id']];
        }
        return $output = [
            'output' => $out,
            'selected' => ''
        ];
    }

    public static function getClaimerNameList($cat_id, $subcat_id)
    {
        $data = Employee::find()
                ->where(['importcompany_id' => $cat_id])
                ->andWhere(['employee_id' => $subcat_id])
                ->asArray()
                ->all();

        $selected = '';

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

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

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

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

1 个答案:

答案 0 :(得分:0)

您忘记声明out变量。

您应该在outgetSubCatList()函数中声明getClaimerNameList()变量。就像数组变量的$out = [];一样。