当前位置:首页 > VUE

vue实现人员选择

2026-01-17 13:43:33VUE

Vue 实现人员选择功能

在 Vue 中实现人员选择功能通常需要结合表单组件、列表展示和交互逻辑。以下是几种常见的实现方式:

使用下拉选择器(Select)

通过 Vue 的 v-model 绑定下拉选择器的值,结合 v-for 渲染人员列表:

<template>
  <select v-model="selectedPerson">
    <option v-for="person in personList" :key="person.id" :value="person">
      {{ person.name }}
    </option>
  </select>
</template>

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

使用复选框(Checkbox)实现多选

通过 v-model 绑定数组实现多选:

vue实现人员选择

<template>
  <div v-for="person in personList" :key="person.id">
    <input 
      type="checkbox" 
      :id="'person-' + person.id" 
      :value="person" 
      v-model="selectedPersons"
    >
    <label :for="'person-' + person.id">{{ person.name }}</label>
  </div>
</template>

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

使用第三方组件库

对于更复杂的需求,可以使用 Element UI、Ant Design Vue 等组件库:

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

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

实现搜索过滤

为人员选择添加搜索功能:

vue实现人员选择

<template>
  <input v-model="searchQuery" placeholder="搜索人员">
  <select v-model="selectedPerson">
    <option 
      v-for="person in filteredPersons" 
      :key="person.id" 
      :value="person"
    >
      {{ person.name }}
    </option>
  </select>
</template>

<script>
export default {
  data() {
    return {
      searchQuery: '',
      selectedPerson: null,
      personList: [
        { id: 1, name: '张三' },
        { id: 2, name: '李四' }
      ]
    }
  },
  computed: {
    filteredPersons() {
      return this.personList.filter(person => 
        person.name.includes(this.searchQuery)
      )
    }
  }
}
</script>

实现分页加载

对于大量人员数据,可以添加分页功能:

<template>
  <select v-model="selectedPerson">
    <option 
      v-for="person in currentPageData" 
      :key="person.id" 
      :value="person"
    >
      {{ person.name }}
    </option>
  </select>
  <button @click="prevPage" :disabled="currentPage === 1">上一页</button>
  <button @click="nextPage" :disabled="currentPage === totalPages">下一页</button>
</template>

<script>
export default {
  data() {
    return {
      selectedPerson: null,
      personList: [
        // 大量人员数据
      ],
      currentPage: 1,
      pageSize: 10
    }
  },
  computed: {
    totalPages() {
      return Math.ceil(this.personList.length / this.pageSize)
    },
    currentPageData() {
      const start = (this.currentPage - 1) * this.pageSize
      const end = start + this.pageSize
      return this.personList.slice(start, end)
    }
  },
  methods: {
    prevPage() {
      if (this.currentPage > 1) this.currentPage--
    },
    nextPage() {
      if (this.currentPage < this.totalPages) this.currentPage++
    }
  }
}
</script>

实现树形人员选择

对于有层级关系的人员数据(如部门-人员),可以使用树形选择器:

<template>
  <el-tree
    :data="treeData"
    show-checkbox
    node-key="id"
    :props="defaultProps"
    @check-change="handleCheckChange">
  </el-tree>
</template>

<script>
export default {
  data() {
    return {
      treeData: [{
        id: 1,
        label: '部门A',
        children: [{
          id: 2,
          label: '张三'
        }, {
          id: 3,
          label: '李四'
        }]
      }],
      defaultProps: {
        children: 'children',
        label: 'label'
      }
    }
  },
  methods: {
    handleCheckChange(data, checked) {
      console.log('选中人员:', data, checked)
    }
  }
}
</script>

以上实现方式可以根据实际需求进行组合和扩展,满足不同场景下的人员选择功能。

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

相关文章

vue实现列表显示

vue实现列表显示

Vue 实现列表显示的方法 在 Vue 中实现列表显示通常使用 v-for 指令,结合数组或对象数据渲染动态列表。以下是几种常见的实现方式。 基本列表渲染 通过 v-for 指令遍历数组,动态生成列…

vue实现多选题

vue实现多选题

Vue实现多选题的方法 使用Vue实现多选题功能,可以通过v-model绑定数组、动态渲染选项、以及处理选中状态来实现。以下是一个完整的实现示例: 基础实现代码 <template>…

vue实现机制

vue实现机制

Vue 实现机制的核心原理 Vue.js 的核心实现机制主要基于响应式系统、虚拟 DOM 和组件化设计。以下是关键实现细节: 响应式系统 Vue 通过 Object.defineProperty(V…

vue实现抽奖

vue实现抽奖

Vue 实现抽奖功能 基本抽奖逻辑 创建一个Vue组件,包含奖品列表、抽奖按钮和结果显示区域。使用随机数生成器选择奖品。 <template> <div> <…

vue 实现excel

vue 实现excel

Vue 实现 Excel 功能 前端导出 Excel 使用 xlsx 库可以轻松实现前端 Excel 导出功能。安装依赖: npm install xlsx 导出 Excel 示例代码: impo…

vue实现刷卡

vue实现刷卡

Vue 实现刷卡效果 在 Vue 中实现刷卡效果可以通过 CSS 动画和 Vue 的过渡系统结合完成。以下是一种常见的实现方式: 基础实现方法 安装 Vue 过渡依赖(如果尚未安装): npm i…