Vue+ajax实现三级联动

如何利用Vue实现三级联动,这里我们使用Vue组件实现,废话不多说,上干货
HTML

<!--此处截取需要的部分-->
<div class="proCity" id="proCity">
<provinceCity></provinceCity>
</div>

ajax.js
这里我利用原生js实现ajax调用(当复习了)

var proList = [];
var cityList = [];
var areaList = [];
createXmlHttpRequest();
//创建XMLHttpRequest
      function createXmlHttpRequest(){
          if(window.XMLHttpRequest){
              return new XMLHttpRequest();
          }else{
              return new ActiveXObject("Microsoft.XMLHTTP");
          }
      }
      var url="<%= request.getContextPath() %>/uploadServlet";
         //调用方法创建XMLHttpRequest对象
         XmlHttpRequest = createXmlHttpRequest();
         //设置回调函数
         XmlHttpRequest.onreadystatechange=finish;
         //初始化xmlhttprequest
         XmlHttpRequest.open("GET",url,true);
         //发送请求
         XmlHttpRequest.send(null);
         
         //ajax回调函数
         function finish(){
             if(XmlHttpRequest.readyState == 4&& XmlHttpRequest.status == 200){
                 var result = XmlHttpRequest.responseText;
                 //后台数据格式为数组嵌套对象
                 var json = eval("(" + result + ")");
                  proList = json[0];
                  cityList  = json[1];
                  areaList = json[2];
             }
         }
         module.export = {
       		 proList,
       		 cityList,
       		 areaList 
         }

index.js
这里一定要把Vue的基础记牢啊,template只能有一个根节点,这个错误我第一次做的时候就犯了

var ajaxPar = require('./ajax');
var proRows = ajaxPar.proList;
var cityRows = ajaxPar.cityList;
var areaRows = ajaxPar.areaList;
Vue.component('province',{
    template:'<div>\
					    <select v-model="province">\
					    <option v-for="item in proList" :value="item.id">{{item.name}}</option>\
					    </select>\
					    <select v-model="city">\
					    <option v-for="item in cityList" :value="item.id">{{item.name}}</option>\
					    </select>\
					    <select v-model="area">\
					        <option v-for="item in areaList" :value="item.id">{{item.name}}</option>\
					    </select>\
				</div>',
    data: function () {
        return {
            proList: [],
            cityList: [],
            areaList: [],
            province: "",
            city: "",
            area: "",
        }
    },
    created: function () {
        this.proList= proRows;
        this.province= this.proList.length > 0 ? this.proList[0].id : "";

        var val = this.province;
        this.cityList= cityRows.filter(function (x) { return x.pid == val });
        this.city= this.cityList.length > 0 ? this.cityList[0].id : "";

        val = this.city;
        this.areaList= areaRows.filter(function(x){return x.pid == val});
        this.area= this.areaList.length > 0 ? this.areaList[0].id : "";
    },
    watch: {
        unitSelected: function (val) {
	        this.cityList= cityRows.filter(function (x) { return x.pid == val });
	        this.city= this.cityList.length > 0 ? this.cityList[0].id : "";
        },
        DepartmentSelected: function(val){
	        this.areaList= areaRows.filter(function(x){return x.pid == val});
	        this.area= this.areaList.length > 0 ? this.areaList[0].id : "";
        }
    }
})
new Vue({
    el: "#proCity"
})

如有错误还希望大家指正!

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值