当前位置:首页 > VUE

vue实现级联选择

2026-02-18 15:06:00VUE

Vue 实现级联选择的方法

使用 Element UI 的 Cascader 组件

Element UI 提供了一个现成的级联选择器组件,适用于大多数场景。安装 Element UI 后,可以直接使用 el-cascader 组件。

<template>
  <el-cascader
    v-model="selectedOptions"
    :options="options"
    :props="props"
    @change="handleChange"
  ></el-cascader>
</template>

<script>
export default {
  data() {
    return {
      selectedOptions: [],
      options: [
        {
          value: 'province1',
          label: 'Province 1',
          children: [
            {
              value: 'city1',
              label: 'City 1',
              children: [
                {
                  value: 'district1',
                  label: 'District 1'
                }
              ]
            }
          ]
        }
      ],
      props: {
        expandTrigger: 'hover'
      }
    };
  },
  methods: {
    handleChange(value) {
      console.log(value);
    }
  }
};
</script>

使用 Ant Design Vue 的 Cascader 组件

Ant Design Vue 也提供了级联选择器组件,功能与 Element UI 类似。

<template>
  <a-cascader
    v-model:value="selectedOptions"
    :options="options"
    placeholder="Please select"
    @change="handleChange"
  />
</template>

<script>
export default {
  data() {
    return {
      selectedOptions: [],
      options: [
        {
          value: 'province1',
          label: 'Province 1',
          children: [
            {
              value: 'city1',
              label: 'City 1',
              children: [
                {
                  value: 'district1',
                  label: 'District 1'
                }
              ]
            }
          ]
        }
      ]
    };
  },
  methods: {
    handleChange(value) {
      console.log(value);
    }
  }
};
</script>

自定义级联选择器

如果需要更灵活的级联选择器,可以手动实现。以下是一个简单的自定义级联选择器示例。

<template>
  <div>
    <select v-model="selectedProvince" @change="loadCities">
      <option v-for="province in provinces" :value="province.value">
        {{ province.label }}
      </option>
    </select>
    <select v-model="selectedCity" @change="loadDistricts" v-if="cities.length">
      <option v-for="city in cities" :value="city.value">
        {{ city.label }}
      </option>
    </select>
    <select v-model="selectedDistrict" v-if="districts.length">
      <option v-for="district in districts" :value="district.value">
        {{ district.label }}
      </option>
    </select>
  </div>
</template>

<script>
export default {
  data() {
    return {
      selectedProvince: '',
      selectedCity: '',
      selectedDistrict: '',
      provinces: [
        { value: 'province1', label: 'Province 1' }
      ],
      cities: [],
      districts: []
    };
  },
  methods: {
    loadCities() {
      this.cities = [
        { value: 'city1', label: 'City 1' }
      ];
      this.selectedCity = '';
      this.selectedDistrict = '';
      this.districts = [];
    },
    loadDistricts() {
      this.districts = [
        { value: 'district1', label: 'District 1' }
      ];
      this.selectedDistrict = '';
    }
  }
};
</script>

动态加载数据

对于数据量较大的场景,可以使用动态加载数据的方式,避免一次性加载所有数据。

vue实现级联选择

<template>
  <el-cascader
    v-model="selectedOptions"
    :props="props"
  ></el-cascader>
</template>

<script>
export default {
  data() {
    return {
      selectedOptions: [],
      props: {
        lazy: true,
        lazyLoad(node, resolve) {
          const { level } = node;
          setTimeout(() => {
            const nodes = Array.from({ length: level + 1 })
              .map((_, i) => ({
                value: `${level}-${i}`,
                label: `Option ${level}-${i}`,
                leaf: level >= 2
              }));
            resolve(nodes);
          }, 1000);
        }
      }
    };
  }
};
</script>

注意事项

  • 数据格式必须符合组件要求,通常是一个嵌套的数组结构。
  • 动态加载数据时,注意处理加载状态和错误情况。
  • 如果需要更复杂的功能,可以结合后端 API 动态获取数据。

标签: 级联vue
分享给朋友:

相关文章

vue实现继承语法糖

vue实现继承语法糖

在Vue中实现继承可以通过多种方式模拟类继承的效果,以下是常见的几种方法: 使用mixins实现逻辑复用 通过Vue的mixins机制共享组件选项,达到类似继承的效果: const Bas…

vue实现选区

vue实现选区

Vue 实现选区的基本方法 在Vue中实现选区功能通常涉及DOM操作和事件处理。以下是几种常见的方法: 使用原生JavaScript的Selection API 通过window.getSelec…

vue实现过滤

vue实现过滤

Vue实现过滤的方法 在Vue中实现数据过滤可以通过多种方式完成,以下是几种常见的方法: 使用计算属性过滤 计算属性是Vue中处理数据过滤的理想选择,它们基于响应式依赖进行缓存,只有在相关依赖发生改…

vue实现节流

vue实现节流

vue实现节流的方法 在Vue中实现节流(throttle)功能,通常用于限制高频事件的触发频率,例如滚动、输入或按钮点击。以下是几种常见的实现方式: 使用Lodash的throttle函数 安装L…

vue实现接口

vue实现接口

在 Vue 中实现接口请求 Vue 本身不直接提供 HTTP 请求功能,通常需要借助第三方库如 axios 或 fetch 来实现接口请求。以下是几种常见方法: 使用 axios 发送请求 安装 a…

vue 实现templateurl

vue 实现templateurl

在 Vue 中,templateUrl 是 Angular 中的一个概念,用于指定外部 HTML 文件作为模板。Vue 本身不直接支持 templateUrl,但可以通过以下方法实现类似功能: 使用…