当前位置:首页 > 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>

动态加载数据

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

<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实现文字播放栏

Vue 实现文字播放栏(跑马灯效果) 方法一:使用 CSS 动画 + Vue 数据绑定 通过 CSS 的 @keyframes 实现动画效果,结合 Vue 的动态数据绑定控制内容。 <tem…

vue分类实现

vue分类实现

Vue分类实现方法 在Vue中实现分类功能通常涉及数据分组、动态渲染和交互逻辑。以下是几种常见的实现方式: 基于计算属性的分类 通过计算属性对原始数据进行分组处理,适合静态或少量数据分类: com…

vue实现节流

vue实现节流

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

vue实现签章

vue实现签章

Vue 实现签章功能 签章功能通常包括手写签名、电子印章等。以下是基于 Vue 的实现方法: 使用 canvas 实现手写签名 安装依赖(如需要): npm install signatu…

vue 实现视频

vue 实现视频

Vue 实现视频播放功能 使用 Vue 实现视频播放功能可以通过 HTML5 的 <video> 标签或第三方库(如 video.js)来实现。以下是两种常见的方法: 使用 HTML5…

vue diff实现

vue diff实现

Vue Diff 算法实现原理 Vue 的虚拟 DOM Diff 算法(即 patch 过程)是其高效渲染的核心,主要通过对比新旧虚拟节点的差异,最小化 DOM 操作。以下是关键实现要点: 同层级比…