Laravel-admin选择框联动 改变时候从2个表取数据
情景
Laravel-admin 选择框 1次联动用load实现,如果一个选择框的change事件,改变了第二个的选择框,但是第二个选择框需要改变第三个选择框时,第三个选择框需要第一个和第二个选择框里的数据。这个时候应该怎么做呢? 我就喜欢碰到这样绕的问题,于是我用了JS的方式。
代码展示
CommonController文件
是我用来统一放Ajax调用的文件,用AjaxController.php的名字好像更好一些
    //查找优惠券
    public function getCouponsList(Request $request)
    {
        $merchant_id = $request->get('q');
        if ($merchant_id)
        {
            return MerchantCoupons::where('merchant_id', $merchant_id)->get(['coupons_id as id', DB::raw('name as text')]);
        }else{
            return [];
        }
    }
    //查找商品列表
    public function getGoodsList(Request $request)
    {
        $merchant_id = $request->get('q');
        if ($merchant_id)
        {
            return MerchantGoods::where('merchant_id', $merchant_id)->get(['goods_id as id', DB::raw('goods_name as text')]);
        }else{
            return [];
        }
    }
路由
    $router->get('/ajax/couponsList', 'CommonController@getCouponsList');                       //获取优惠券
    $router->get('/ajax/goodsList', 'CommonController@getGoodsList');                           //获取商品列表
form
控制器的form我是这么写的。
因为我是多商户的系统,单独封装了一套商户的权限,
$this->showMerchantName`` $this->checkIsMy($form)这些是我封装的商户权限的函数。有兴趣可以看另一篇商户权限的封装代码。
protected function form()
    {
        $form = new Form(new MerchantRun());
        $this->showMerchantName($form,'所属属商户');
        $form->select('run_type', __('类型'))->options(config('adminCommon.run_type'))->required();
        $form->datetime('start_time', __('开始时间'))->default(date('Y-m-d H:i:s'));
        $form->datetime('end_time', __('结束时间'))->default(date('Y-m-d H:i:s'));
        $form->select('complimentary_type', __('赠品类型'))->options(config('adminCommon.complimentary_type'))->required();
        $form->select('complimentary_id', __('赠品Id'))->required();
        $form->switch('status','是否启用')->states(config('adminCommon.switch_using'))->default(0);
        Admin::script('
            //改变商户时
            $(".merchant_id").on("change", function(){
                $(".complimentary_id").html("");
                change_complimentary();
            });
            
            //改变赠品类型时
            $(".complimentary_type").on("change", function(){
                change_complimentary();
            });
           
            //改变赠品ID
            function change_complimentary()
            {
                var complimentary_type = $(".complimentary_type").val();
                var merchant_id = $(".merchant_id").val();
                if(complimentary_type == 1)
                {
                    $.ajax({
                        method: \'get\',
                        url: "/admin/ajax/couponsList",
                        data: {
                            _token:LA.token,
                            q:merchant_id,
                        },
                        success: function (data) {
                            $(".complimentary_id").html("");
                             $.each( data, function(i, row){
                              $(".complimentary_id").append("<option value=\'"+row["id"]+"\'>"+row["text"]+"<\/option>");
                            });
                        }
                    });
                }
                else if (complimentary_type == 2)
                {
                    $.ajax({
                        method: \'get\',
                        url: "/admin/ajax/goodsList",
                        data: {
                            _token:LA.token,
                            q:merchant_id,
                        },
                        success: function (data) {
                            $(".complimentary_id").html("");
                             $.each( data, function(i, row){
                              $(".complimentary_id").append("<option value=\'"+row["id"]+"\'>"+row["text"]+"<\/option>");
                            });
                        }
                    });
                }
            }
            
        ',false);
        return $this->checkIsMy($form);
    }