当前位置:首页 > VUE

vue实现li多选

2026-03-28 23:15:39VUE

实现多选列表的基本思路

在Vue中实现多选列表功能,可以通过v-model绑定数组来管理选中状态。当用户点击列表项时,动态更新数组内容。

基础实现代码示例

<template>
  <div>
    <ul>
      <li 
        v-for="item in items" 
        :key="item.id"
        @click="toggleSelection(item)"
        :class="{ selected: selectedItems.includes(item) }"
      >
        {{ item.name }}
      </li>
    </ul>
    <p>已选择: {{ selectedItems.map(i => i.name).join(', ') }}</p>
  </div>
</template>

<script>
export default {
  data() {
    return {
      items: [
        { id: 1, name: '选项1' },
        { id: 2, name: '选项2' },
        { id: 3, name: '选项3' }
      ],
      selectedItems: []
    }
  },
  methods: {
    toggleSelection(item) {
      const index = this.selectedItems.findIndex(i => i.id === item.id)
      if (index === -1) {
        this.selectedItems.push(item)
      } else {
        this.selectedItems.splice(index, 1)
      }
    }
  }
}
</script>

<style>
.selected {
  background-color: #eee;
  font-weight: bold;
}
</style>

使用复选框的实现方式

更符合用户习惯的方式是使用复选框:

<template>
  <div>
    <ul>
      <li v-for="item in items" :key="item.id">
        <input 
          type="checkbox" 
          :id="'item-' + item.id" 
          :value="item" 
          v-model="selectedItems"
        >
        <label :for="'item-' + item.id">{{ item.name }}</label>
      </li>
    </ul>
  </div>
</template>

全选/全不选功能

可以添加全选功能提升用户体验:

<template>
  <div>
    <div>
      <input 
        type="checkbox" 
        id="select-all" 
        v-model="allSelected"
        @change="toggleAll"
      >
      <label for="select-all">全选</label>
    </div>
    <ul>
      <li v-for="item in items" :key="item.id">
        <input 
          type="checkbox" 
          :id="'item-' + item.id" 
          :value="item" 
          v-model="selectedItems"
        >
        <label :for="'item-' + item.id">{{ item.name }}</label>
      </li>
    </ul>
  </div>
</template>

<script>
export default {
  computed: {
    allSelected: {
      get() {
        return this.selectedItems.length === this.items.length
      },
      set(value) {
        this.selectedItems = value ? [...this.items] : []
      }
    }
  }
}
</script>

性能优化建议

对于大型列表,使用对象存储选中状态比数组更高效:

data() {
  return {
    selectedMap: {}
  }
},
methods: {
  toggleSelection(item) {
    this.$set(this.selectedMap, item.id, !this.selectedMap[item.id])
  },
  selectedItems() {
    return this.items.filter(item => this.selectedMap[item.id])
  }
}

与后端交互

提交选中数据时,通常只需要ID数组:

vue实现li多选

submit() {
  const selectedIds = this.selectedItems.map(item => item.id)
  // 发送selectedIds到后端
}

以上实现方式可以根据具体需求进行调整和组合,Vue的响应式系统使得多选列表的实现变得简单直观。

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

相关文章

vue实现drag

vue实现drag

Vue 实现拖拽功能的方法 在 Vue 中实现拖拽功能可以通过原生 HTML5 的拖拽 API 或第三方库如 vuedraggable 来完成。以下是两种常见方法的实现方式。 使用 HTML5 拖拽…

vue实现录像

vue实现录像

Vue 实现录像功能 在 Vue 中实现录像功能通常需要借助浏览器的 MediaDevices API 和 MediaRecorder API。以下是实现步骤: 获取用户摄像头和麦克风权限 使用 n…

vue实现treeview

vue实现treeview

Vue 实现 TreeView 的方法 使用递归组件实现 TreeView 递归组件是 Vue 中实现 TreeView 的常见方式。通过组件调用自身,可以轻松处理嵌套的树形结构数据。 <te…

vue实现链接

vue实现链接

Vue 实现链接的方法 在 Vue 中实现链接可以通过多种方式,包括使用 <router-link>、<a> 标签或编程式导航。以下是几种常见的方法: 使用 <rout…

vue远离实现

vue远离实现

Vue 原理实现 Vue.js 的核心原理包括响应式系统、虚拟 DOM、模板编译和组件化机制。以下是其关键实现原理的详细解析。 响应式系统 Vue 的响应式系统基于 Object.definePro…

vue 实现登录验证

vue 实现登录验证

Vue 实现登录验证的方法 使用 Vue Router 进行路由守卫 在 Vue 项目中,可以通过 Vue Router 的路由守卫(Navigation Guards)实现登录验证。路由守卫可以在路…