ThinkPhP。 异步实现地区三级联动

控制器区

    <?php
    namespace app\index\controller;
    use think\Controller;
    use app\index\model\Region as regionModel;
    class Index extends Controller
    {
        /**
         * 城市三级联动
         * @return array|mixed
         */
        public function index()
        {
            if(request()->isPost())
            {
                $regionModel=new regionModel();
                $type=input('post.type');
                $parent=input('post.parent');
                $province=$regionModel->region($type,$parent);
                $position=$regionModel->position();
                return ['province'=>$province,'position'=>$position];
            }
            return $this->fetch();
        }
        /**
         * 市
         * @return array
         */
        public function searchCity()
        {
            if(request()->isPost())
            {
                $regionModel=new regionModel();
                $id=input('post.id');
                $city=$regionModel->searchCity($id);
                return ['city'=>$city];
            }
            return ['error'=>'异常错误'];
        }
        /**
         * 县
         * @return array
         */
        public function searchCounty()
        {
            if(request()->isPost())
            {
                $regionModel=new regionModel();
                $id=input('post.id');
                $county=$regionModel->searchCounty($id);
                return ['county'=>$county];
            }
            return ['error'=>'异常错误'];
        }
        /**
         * 乡镇
         * @return array
         */
        public function searchTown()
        {
            if(request()->isPost())
            {
                $regionModel=new regionModel();
                $id=input('post.id');
                $town=$regionModel->searchTown($id);
                return ['town'=>$town];
            }
            return ['error'=>'异常错误'];
        }
        /**
         * 村庄
         * @return array
         */
        public function searchVillage()
        {
            if(request()->isPost())
            {
                $regionModel=new regionModel();
                $id=input('post.id');
                $village=$regionModel->searchVillage($id);
                return ['village'=>$village];
            }
            return ['error'=>'异常错误'];
        }

html区


    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
        <link rel="stylesheet" href="/static/admin/js/bootstrap.min.css">
        <script type="text/javascript" src="/static/admin/layui/layui.js"></script>
        <link rel="stylesheet" href="/static/admin/layui/css/layui.css">
        <script type="text/javascript" src="/static/vue/vue.js"></script>
        <title>Title</title>
    </head>
    <body>
    <div id="app" class="form-inline" style="margin-top: 20px;margin-left: 20px;">
        <div class="form-group searchMargin">
            <label for="type1">省份:</label>
            <select class="form-control"  id="type1" name="region_id" @change="provinceSearch('#type1')">
                <option value="">选择省份</option>
                <option v-for="province in provinceModel" value="{{province.region_id}}">{{province.region_name}}</option>
            </select>
        </div>
        <div class="form-group searchMargin" id="city">
            <label for="type2">市区:</label>
            <select class="form-control" id="type2"  name="region_id" @change="citySearch('#type2')" >
                <option value="">选择市区</option>
                <option v-for="city in cityModel" value="{{city.region_id}}">{{city.region_name}}</option>
            </select>
        </div>
        <div class="form-group searchMargin" id="country">
            <label for="type3">乡/县:</label>
            <select class="form-control" id="type3"  name="region_id" >
                <option value="">选择乡/县</option>
                <option v-for="country in countryModel" value="{{country.region_id}}">{{country.region_name}}</option>
            </select>
        </div>
        <div class="form-group searchMargin" id="img">
        </div>
        <div class="form-group searchMargin">
            <input type="file" name="file" lay-type="file" class="layui-upload-file">
        </div>
        <!--五级联动start-->
        <br/>
        <br/>
        <div class="form-group searchMargin" id="province">
            <label for="type4">省份:</label>
            <select class="form-control" id="type4"  name="province_id" @change="positionSearch('#type4')">
                <option value="">选择省份</option>
                <option v-for="province in positionModel" value="{{province.province_id}}">{{province.province_name}}</option>
            </select>
        </div>
        <div class="form-group searchMargin" id="municipal">
            <label for="type5">市:</label>
            <select class="form-control" id="type5"  name="city_id" @change="countySearch('#type5')">
                <option value="">选择市</option>
                <option v-for="municipal in municipalModel" value="{{municipal.city_id}}">{{municipal.city_name}}</option>
            </select>
        </div>
        <div class="form-group searchMargin" id="county">
            <label for="type6">县:</label>
            <select class="form-control" id="type6"  name="county_id" @change="townSearch('#type6')">
                <option value="">选择县</option>
                <option v-for="county in countyModel" value="{{county.county_id}}">{{county.county_name}}</option>
            </select>
        </div>
        <div class="form-group searchMargin" id="town">
            <label for="type7">镇:</label>
            <select class="form-control" id="type7"  name="town_id" @change="villageSearch('#type7')">
                <option value="">选择镇</option>
                <option v-for="town in townModel" value="{{town.town_id}}">{{town.town_name}}</option>
            </select>
        </div>
        <div class="form-group searchMargin" id="village">
            <label for="type8">村:</label>
            <select class="form-control" id="type8"  name="village_id" >
                <option value="">选择村</option>
                <option v-for="village in villageModel" value="{{village.village_id}}">{{village.village_name}}</option>
            </select>
        </div>
        <!--五级联动end-->
        <div class="form-group searchMargin" id="timimg">
            <label for="type3">定时发布时间:</label>
            <div class="layui-inline">
                <input class="layui-input" @blur="send()" name="timing" placeholder="自定义日期格式" onclick="layui.laydate({elem: this, istime: true, format: 'YYYY-MM-DD hh:mm:ss'})">
            </div>
            <button class="layui-btn" type="submit" @click="send()">确定</button>
        </div>
    </div>
    </body>
    <script type="text/javascript">
        layui.use(['layer','jquery','upload','laydate'],function () {
            let layer=layui.layer,$=layui.jquery;
            layui.upload({
                url:'/index/index/upload',
                method:'post',
                success:function (res) {
                    $("#img").append('<img src="/upload/image/'+res.info+'">');
                    console.log(res);
                }
            });
            let app=new Vue({
                el:'#app',
                data:{
                    //三级联动
                    provinceModel:'',   //省
                    cityModel:'',     //市
                    countryModel:'',   //县
                    //五级联动
                    positionModel:'',  //省份
                    municipalModel:'', //市
                    countyModel:'',  //县
                    townModel:'',   //镇
                    villageModel:'',  //村
                    type:1,
                    start:'',
                    end:'',
                    parent:1,
                    times:'',
                    cgi:{
                        list:'/',
                        searchCity:'/index/index/searchCity'
                    }
                },
                methods:{
                    showData:function () {
                        layer.msg('加载中...',{icon:16,time:1000});
                        let self=this;
                        $.ajax({
                            type:'post',
                            url:self.cgi.list,
                            data:{type:this.type,parent:this.parent},
                            dataType:'json',
                            success:function (result) {
                                console.log(result);
                                self.positionModel=result.position;
                                self.provinceModel=result.province;
                            }
                        })
                    },
                    provinceSearch:function (index) {
                        let parent=$(index).val();
                        let self=this;
                        $.ajax({
                            type:'post',
                            url:self.cgi.list,
                            data:{parent:parent,type:2},
                            dataType:'json',
                            success:function (result) {
                                console.log(result);
                                self.cityModel=result.province;
                            }
                        })
                    },
                    citySearch:function (index) {
                        let parent=$(index).val();
                        let self=this;
                        $.ajax({
                            type:'post',
                            url:self.cgi.list,
                            data:{parent:parent,type:3},
                            dataType:'json',
                            success:function (result) {
                                console.log(result);
                                self.countryModel=result.province;
                            }
                        })
                    },
                    /* <!--五级联动-->*/
                    positionSearch:function (index) {
                        let id=$(index).val();
                        let self=this;
                        $.ajax({
                            type:'post',
                            url:'/index/index/searchCity',
                            data:{id:id},
                            dataType:'json',
                            success:function (result) {
                                console.log(result);
                                self.municipalModel=result.city;
                            }
                        })
                    },
                    countySearch:function (index) {
                        let id=$(index).val();
                        let self=this;
                        $.ajax({
                            type:'post',
                            url:'/index/index/searchCounty',
                            data:{id:id},
                            dataType:'json',
                            success:function (result) {
                                console.log(result);
                                self.countyModel=result.county;
                            }
                        })
                    },
                    townSearch:function (index) {
                        let id=$(index).val();
                        let self=this;
                        $.ajax({
                            type:'post',
                            url:'/index/index/searchTown',
                            data:{id:id},
                            dataType:'json',
                            success:function (result) {
                                console.log(result);
                                self.townModel=result.town;
                            }
                        })
                    },
                    villageSearch:function (index) {
                        let id=$(index).val();
                        let self=this;
                        $.ajax({
                            type:'post',
                            url:'/index/index/searchVillage',
                            data:{id:id},
                            dataType:'json',
                            success:function (result) {
                                console.log(result);
                                self.villageModel=result.village;
                            }
                        })
                    },
                    send:function(){
                        let timing=$("input[name=timing]").val();
                        layer.msg(timing);
                    }
                },
                ready:function () {
                    this.showData();
                }
            });
        });
    </script>
    </html>

方法区

    <?php
    /**
     * 城市联动Model
     * Created by PhpStorm.
     * User: 尔等已死
     * Date: 2017/1/14
     * Time: 13:03
     * Description:Region
     */
    namespace app\index\model;
    use think\Model;
    class Region extends Model
    {
        /**
         * 地区三级联动
         * @param $type
         * @param $parent
         * @return false|\PDOStatement|string|\think\Collection
         */
        public function region($type,$parent)
        {
            return db('region')->where(array('region_type'=>$type,'parent_id'=>$parent))->cache(true)->select();
        }
        /**
         * 地区五级联动 省份
         * @return false|\PDOStatement|string|\think\Collection
         */
        public function position()
        {
            return db('position')->field('province_name,province_id')->group('province_id')->cache(true)->select();
        }
        /**
         * 地区五级联动 市
         * @param $id
         * @return false|\PDOStatement|string|\think\Collection
         */
        public function searchCity($id)
        {
            return db('position')->field('city_name,city_id')->group('city_id')->where(array('province_id'=>$id))->cache(true)->select();
        }
        /**
         * 地区五级联动 县
         * @param $id
         * @return false|\PDOStatement|string|\think\Collection
         */
        public function searchCounty($id)
        {
            return db('position')->field('county_name,county_id')->group('county_id')->where(array('city_id'=>$id))->cache(true)->select();
        }
        /**
         * 地区五级联动 镇
         * @param $id
         * @return false|\PDOStatement|string|\think\Collection
         */
        public function searchTown($id)
        {
            return db('position')->field('town_name,town_id')->group('town_id')->where(array('county_id'=>$id))->cache(true)->select();
        }
        /**
         * 地区五级联动 村
         * @param $id
         * @return false|\PDOStatement|string|\think\Collection
         */
        public function searchVillage($id)
        {
            return db('position')->field('village_name,village_id')->group('village_id')->where(array('town_id'=>$id))->cache(true)->select();
        }
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
提供的源码资源涵盖了Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 适合毕业设计、课程设计作业。这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。 所有源码均经过严格测试,可以直接运行,可以放心下载使用。有任何使用问题欢迎随时与博主沟通,第一时间进行解答!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值