当前位置:首页 > VUE

vue实现li多选

2026-02-17 00:20:04VUE

实现思路

在Vue中实现li多选功能,可以通过数据绑定和事件处理来完成。核心思路是利用数组存储选中的项,通过v-for渲染列表,结合v-model@click动态更新选中状态。

基础实现方法

模板部分

<template>
  <ul>
    <li 
      v-for="item in list" 
      :key="item.id"
      @click="toggleSelect(item)"
      :class="{ 'selected': selectedItems.includes(item) }"
    >
      {{ item.name }}
    </li>
  </ul>
</template>

脚本部分

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

样式部分

<style>
.selected {
  background-color: #42b983;
  color: white;
}
</style>

使用复选框实现

如果需要更直观的多选体验,可以结合复选框实现:

模板部分

<template>
  <ul>
    <li v-for="item in list" :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>
</template>

全选/反选功能

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

模板部分

<template>
  <div>
    <input type="checkbox" id="selectAll" v-model="allSelected" @change="toggleAll">
    <label for="selectAll">全选</label>
    <ul>
      <li v-for="item in list" :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 {
  data() {
    return {
      // 其他数据同上
      allSelected: false
    }
  },
  methods: {
    toggleAll() {
      this.selectedItems = this.allSelected ? [...this.list] : []
    }
  },
  watch: {
    selectedItems: {
      handler(newVal) {
        this.allSelected = newVal.length === this.list.length
      },
      deep: true
    }
  }
}
</script>

性能优化建议

对于大型列表,可以考虑以下优化:

  • 使用对象代替数组存储选中状态,提升查找效率
  • 添加虚拟滚动技术处理超长列表
  • 使用计算属性减少不必要的计算

注意事项

  • 确保每个li有唯一的key属性
  • 对象比较时注意引用类型的问题
  • 多选功能可能需要根据实际需求调整选中逻辑

vue实现li多选

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

相关文章

用vue实现滑动输入条

用vue实现滑动输入条

使用 Vue 实现滑动输入条 使用原生 HTML5 input range 实现 在 Vue 模板中直接使用 <input type="range">,通过 v-model 绑定数据:…

实现 vue 框架

实现 vue 框架

Vue 框架的基本实现 Vue 是一个渐进式 JavaScript 框架,用于构建用户界面。以下是实现 Vue 框架的基本步骤和核心概念。 核心概念 响应式数据绑定 Vue 的核心特性之一是响应式…

vue 实现列表

vue 实现列表

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

vue实现oauth

vue实现oauth

Vue 实现 OAuth 的步骤 安装必要的依赖 确保项目中安装了 axios 和 vue-router。OAuth 通常需要发送 HTTP 请求和处理路由跳转。 npm install axio…

vue实现路由

vue实现路由

Vue 路由的实现方法 Vue 路由可以通过 Vue Router 库来实现,Vue Router 是 Vue.js 官方的路由管理器,用于构建单页面应用(SPA)。 安装 Vue Router 通…

vue实现试卷

vue实现试卷

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