当前位置:首页 > VUE

vue实现级联选择

2026-02-18 15:06:00VUE

Vue 实现级联选择的方法

使用 Element UI 的 Cascader 组件

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

vue实现级联选择

<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 类似。

vue实现级联选择

<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 CLI创建项目,确保已配置好开发环境。使用vue create appointment-page初始化项目,选择默认配置或手动配置。 页面结构设计 在…

vue实现试卷

vue实现试卷

Vue实现试卷系统的基本思路 使用Vue.js构建试卷系统需要结合组件化开发、状态管理和后端数据交互。核心功能包括题目展示、答题逻辑、计时器和提交答案。 核心功能模块划分 试卷组件结构 试卷容器组…

vue实现接口

vue实现接口

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

vue实现弹幕

vue实现弹幕

Vue 实现弹幕功能 弹幕功能通常包括动态生成、滚动显示、颜色和速度随机化等特性。以下是基于 Vue 3 的实现方法。 核心思路 使用 CSS 动画控制弹幕从右向左滚动。 动态生成弹幕数据,随机设置…

实现vue message

实现vue message

实现 Vue 消息提示组件 创建 Message 组件 在 src/components 目录下创建 Message.vue 文件,内容如下: <template> <tran…

vue实现上划

vue实现上划

Vue 实现上划功能 在Vue中实现上划功能,可以通过监听触摸事件(touchstart、touchmove、touchend)来判断用户的手势方向。以下是实现上划功能的几种方法: 监听触摸事件 通…