当前位置:首页 > VUE

vue实现选人

2026-02-10 10:26:47VUE

Vue 实现选人功能

使用 Element UI 的 Select 组件

Element UI 提供了 Select 组件,可以方便地实现选人功能。需要安装 Element UI 并引入 Select 组件。

<template>
  <el-select v-model="selectedPerson" placeholder="请选择人员">
    <el-option
      v-for="person in personList"
      :key="person.id"
      :label="person.name"
      :value="person.id">
    </el-option>
  </el-select>
</template>

<script>
export default {
  data() {
    return {
      selectedPerson: '',
      personList: [
        { id: 1, name: '张三' },
        { id: 2, name: '李四' },
        { id: 3, name: '王五' }
      ]
    }
  }
}
</script>

使用自定义组件实现多选

如果需要多选功能,可以使用 multiple 属性。

vue实现选人

<el-select v-model="selectedPersons" multiple placeholder="请选择人员">
  <el-option
    v-for="person in personList"
    :key="person.id"
    :label="person.name"
    :value="person.id">
  </el-option>
</el-select>

结合后端 API 动态加载人员列表

通常人员列表需要从后端 API 获取,可以使用 axios 或其他 HTTP 库。

export default {
  data() {
    return {
      selectedPerson: '',
      personList: []
    }
  },
  mounted() {
    this.fetchPersonList()
  },
  methods: {
    fetchPersonList() {
      axios.get('/api/persons').then(response => {
        this.personList = response.data
      })
    }
  }
}

添加搜索过滤功能

Element UI 的 Select 组件支持搜索过滤,通过 filterable 属性启用。

vue实现选人

<el-select v-model="selectedPerson" filterable placeholder="请搜索并选择人员">
  <el-option
    v-for="person in personList"
    :key="person.id"
    :label="person.name"
    :value="person.id">
  </el-option>
</el-select>

使用虚拟滚动优化性能

对于大量人员数据,可以使用虚拟滚动提升性能。

<el-select
  v-model="selectedPerson"
  filterable
  remote
  reserve-keyword
  placeholder="请输入关键词"
  :remote-method="remoteMethod"
  :loading="loading">
  <el-option
    v-for="item in options"
    :key="item.value"
    :label="item.label"
    :value="item.value">
  </el-option>
</el-select>

实现自定义模板

如果需要更复杂的展示方式,可以使用自定义模板。

<el-select v-model="selectedPerson" placeholder="请选择人员">
  <el-option
    v-for="person in personList"
    :key="person.id"
    :label="person.name"
    :value="person.id">
    <span style="float: left">{{ person.name }}</span>
    <span style="float: right; color: #8492a6; font-size: 13px">{{ person.department }}</span>
  </el-option>
</el-select>

以上方法可以根据实际需求灵活组合使用,实现不同场景下的选人功能。

标签: 选人vue
分享给朋友:

相关文章

vue实现多人视频

vue实现多人视频

实现多人视频通话的Vue方案 使用WebRTC技术结合Vue框架可以构建多人视频通话应用。以下是核心实现方法: 技术栈选择 Vue 2/3作为前端框架 WebRTC用于实时通信 Socket.io…

vue实现grid

vue实现grid

Vue 实现 Grid 布局的方法 使用 CSS Grid 布局 Vue 可以结合 CSS Grid 布局实现灵活的网格系统。CSS Grid 是现代浏览器原生支持的布局方案,无需额外依赖库。 &l…

vue路由实现

vue路由实现

Vue 路由实现 Vue Router 是 Vue.js 的官方路由管理器,用于构建单页面应用(SPA)。以下是 Vue Router 的基本实现步骤和核心功能。 安装 Vue Router 通过…

vue 实现视频

vue 实现视频

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

vue 实现树状

vue 实现树状

Vue 实现树状结构的方法 递归组件实现树状结构 递归组件是 Vue 中实现树状结构的常见方法。通过组件自身调用自身,可以轻松构建多层级树状结构。 <template> <ul…

vue如何实现

vue如何实现

Vue 实现方法 在 Vue 中实现功能通常涉及组件化开发、状态管理和响应式数据绑定。以下是几种常见的实现方式: 组件化开发 使用 Vue 的单文件组件(.vue 文件)结构,将 UI 拆分为可复用…