vue 追加下拉框,三级联动

在这里插入图片描述
例图。结构比较复杂。三级联动的这种。这个是添加页面的。

<template>
  <div>
    <el-form label-width="100px">
      <el-form-item label="车型:" v-for="(item,index) in fartherList" :key="index">
        <el-col :span="2">
          <el-select
            class="el-select"
            v-model="form.FartherList[index]"
            size="mini"
            @change="updateSystem(index,form.FartherList[index])"
          >
            <el-option
              v-for="items in Cartype"
              :key="items.p_pinpai"
              :label="item.p_pinpai"
              :value="items.p_pinpai"
            ></el-option>
          </el-select>
        </el-col>
        <el-col :span="2">
          <el-select
            class="el-select"
            v-model="form.secondList[index]"
            @change="updateName(index,form.secondList[index])"
            size="mini"
          >
            <el-option
              v-for="(secondItem,keyss) in secondList.data[index]"
              :key="keyss"
              :label="secondItem.p_chexi"
              :value="secondItem.p_chexi_id"
            ></el-option>
          </el-select>
        </el-col>
        <el-col :span="2">
          <el-select
            class="el-select"
            v-model="form.thirdList[index]"
            size="mini"
            @change="Determin(thirdList.data[index],form.thirdList[index],index)"
          >
            <el-option
              v-for="(third,keys) in thirdList.data[index]"
              :key="keys"
              :label="third.p_chexingmingcheng"
              :value="third.p_chexing_id"
            ></el-option>
          </el-select>
        </el-col>
        <i class="el-icon-circle-plus" @click="Addingmodels"></i>
        <i class="el-icon-remove" @click="deleteItem(index)" v-if="index>=1"></i>
      </el-form-item>
      <!-- 添加多组车型 -->
    </el-form>
  </div>
</template>

<script>
export default {
  data() {
    return {
      Cartype: [],
      form: {
        FartherList: [],
        secondList: [],
        thirdList: []
      },
      secondList: {
        data: []
      },
      thirdList: {
        data: []
      },
      Car: {
        cartype: "",
        carsystem: "",
        carname: "",
        content: [],
        text: []
      },
      submit: [],
      fartherList: [
        {
          cartype: "",
          carsystem: "",
          carname: "",
          content: [],
          text: []
        }
      ],
      Obj: {}
    };
  },

  created() {
    this.Obj = JSON.parse(sessionStorage.getItem("order_no"));
    this.getData();
    this.getCartype();
  },
  methods: {
    // 获取手机端已选择的车型数据
    async getData() {
      const { data: res } = await this.$ajax.get(
        "/v2/order/order-info?order_no=" + this.Obj.no
      );
      if (res.code == 0) {
        // 打印手机端,返回的数组,
        console.log(res.data.vehicle_type);
      }
    },
    // 获取车型
    async getCartype() {
      const { data: res } = await this.$ajax.post("/v2/vehicle-management/get");
      if (res.code == 0) {
        this.Cartype = res.data;
      }
    },
    // 获取车系
    updateSystem(i, value) {
      this.form.secondList[i] = "";
      this.secondList.data[i] = "";
      this.form.thirdList[i] = "";
      this.Cartype.forEach((item, index) => {
        if (item.p_pinpai == value) {
          this.secondList.data[i] = item.son;
        }
      });
    },
    // 获取车型名称
    async updateName(index, value) {
      this.form.thirdList[index] = "";
      const { data: res } = await this.$ajax.post(
        "/v2/vehicle-management/get-type",
        {
          p_chexi_id: value
        }
      );
      if (res.code == 0) {
        this.$set(this.thirdList.data, index, res.data);
      }
    },
    // 添加多组车型
    Addingmodels() {
      this.fartherList.push(this.Car);
    },
    // 删除车型
    deleteItem(index) {
      this.form.FartherList.splice(index, 1);
      this.form.secondList.splice(index, 1);
      this.form.thirdList.splice(index, 1);
      this.secondList.data.splice(index, 1);
      this.thirdList.data.splice(index, 1);
      this.fartherList.splice(index, 1);
      this.submit.splice(index, 1);
    },

    Determin(value, value1, indexd) {
      value.forEach((item, index) => {
        if (item.p_chexing_id == value1) {
          this.submit[indexd] = item;
        }
      });
      console.log(this.submit);
      // 此处就是最后的值
      this.$store.commit("addTocar", this.submit[0]);
    }
  }
};
</script>
<style lang="scss" scoped>
.el-alert {
  margin-bottom: 10px;
}
.el-select {
  width: 100%;
  margin-right: 2px;
}
.el-date-editor.el-input,
.el-date-editor.el-input__inner {
  width: 100% !important;
}
.el-row {
  .el-col {
    line-height: 35px;
    font-size: 14px;
  }
  .el-col:nth-child(1) {
    text-align: right;
  }
}
.Aprice {
  color: red;
}
i {
  font-size: 16px;
  margin-right: 5px;
  cursor: pointer;
}
</style>
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用 Vue 3 的计算属性(computed)来实现下拉框三级联动。以下是一个简单的示例: ```html <template> <div> <select v-model="selectedProvince"> <option value="">请选择省份</option> <option v-for="province in provinces" :key="province" :value="province">{{ province }}</option> </select> <select v-model="selectedCity" :disabled="!selectedProvince"> <option value="">请选择城市</option> <option v-for="city in cities" :key="city" :value="city">{{ city }}</option> </select> <select v-model="selectedDistrict" :disabled="!selectedCity"> <option value="">请选择区县</option> <option v-for="district in districts" :key="district" :value="district">{{ district }}</option> </select> </div> </template> <script> import { computed, ref } from 'vue'; export default { setup() { const provinces = ['北京', '上海', '深圳']; const cities = ref([]); const districts = ref([]); const selectedProvince = ref(''); const selectedCity = ref(''); const selectedDistrict = ref(''); const filteredCities = computed(() => { if (!selectedProvince.value) return []; // 这里可以根据选中的省份来返回对应的城市列表 // 这里只是简单地返回了一个假数据 switch (selectedProvince.value) { case '北京': return ['北京市']; case '上海': return ['上海市']; case '深圳': return ['深圳市']; default: return []; } }); const filteredDistricts = computed(() => { if (!selectedCity.value) return []; // 这里可以根据选中的城市来返回对应的区县列表 // 这里只是简单地返回了一个假数据 switch (selectedCity.value) { case '北京市': return ['东城区', '西城区', '朝阳区']; case '上海市': return ['黄浦区', '徐汇区', '长宁区']; case '深圳市': return ['福田区', '罗湖区', '南山区']; default: return []; } }); // 监听选中的省份变化,更新城市列表和区县列表 watch(selectedProvince, (value) => { selectedCity.value = ''; selectedDistrict.value = ''; cities.value = filteredCities.value; districts.value = []; }); // 监听选中的城市变化,更新区县列表 watch(selectedCity, (value) => { selectedDistrict.value = ''; districts.value = filteredDistricts.value; }); return { provinces, cities, districts, selectedProvince, selectedCity, selectedDistrict, }; }, }; </script> ``` 在这个示例中,我们使用了 `computed` 函数来创建两个计算属性 `filteredCities` 和 `filteredDistricts`,它们分别根据选中的省份和城市来返回对应的城市列表和区县列表。我们还使用了 `watch` 函数来监听选中的省份和城市的变化,从而实现联动效果。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值