当前位置:首页 > VUE

vue2.0 实现选中

2026-02-23 04:48:26VUE

Vue 2.0 实现选中功能

在 Vue 2.0 中实现选中功能可以通过多种方式完成,以下介绍几种常见的方法。

使用 v-model 绑定单选或多选

对于单选或多选场景,可以直接使用 v-model 绑定数据。

单选示例:

<template>
  <div>
    <input type="radio" id="option1" value="Option 1" v-model="selectedOption">
    <label for="option1">Option 1</label>
    <input type="radio" id="option2" value="Option 2" v-model="selectedOption">
    <label for="option2">Option 2</label>
    <p>Selected: {{ selectedOption }}</p>
  </div>
</template>

<script>
export default {
  data() {
    return {
      selectedOption: ''
    };
  }
};
</script>

多选示例:

<template>
  <div>
    <input type="checkbox" id="option1" value="Option 1" v-model="selectedOptions">
    <label for="option1">Option 1</label>
    <input type="checkbox" id="option2" value="Option 2" v-model="selectedOptions">
    <label for="option2">Option 2</label>
    <p>Selected: {{ selectedOptions }}</p>
  </div>
</template>

<script>
export default {
  data() {
    return {
      selectedOptions: []
    };
  }
};
</script>

动态高亮选中项

对于列表或表格中的选中项高亮,可以通过动态绑定 classstyle 实现。

vue2.0 实现选中

示例:

<template>
  <div>
    <ul>
      <li
        v-for="item in items"
        :key="item.id"
        @click="selectItem(item)"
        :class="{ 'active': selectedItem === item }"
      >
        {{ item.name }}
      </li>
    </ul>
  </div>
</template>

<script>
export default {
  data() {
    return {
      items: [
        { id: 1, name: 'Item 1' },
        { id: 2, name: 'Item 2' }
      ],
      selectedItem: null
    };
  },
  methods: {
    selectItem(item) {
      this.selectedItem = item;
    }
  }
};
</script>

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

使用计算属性处理选中逻辑

如果需要复杂的选中逻辑,可以使用计算属性动态判断选中状态。

示例:

vue2.0 实现选中

<template>
  <div>
    <button
      v-for="item in items"
      :key="item.id"
      @click="toggleSelect(item)"
      :class="{ 'selected': isSelected(item) }"
    >
      {{ item.name }}
    </button>
  </div>
</template>

<script>
export default {
  data() {
    return {
      items: [
        { id: 1, name: 'Item 1' },
        { id: 2, name: 'Item 2' }
      ],
      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);
      }
    },
    isSelected(item) {
      return this.selectedItems.some(selected => selected.id === item.id);
    }
  }
};
</script>

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

结合 Vuex 管理选中状态

如果应用状态较为复杂,可以使用 Vuex 集中管理选中状态。

示例:

// store.js
import Vue from 'vue';
import Vuex from 'vuex';

Vue.use(Vuex);

export default new Vuex.Store({
  state: {
    selectedItems: []
  },
  mutations: {
    toggleSelect(state, item) {
      const index = state.selectedItems.findIndex(selected => selected.id === item.id);
      if (index === -1) {
        state.selectedItems.push(item);
      } else {
        state.selectedItems.splice(index, 1);
      }
    }
  },
  getters: {
    isSelected: state => item => {
      return state.selectedItems.some(selected => selected.id === item.id);
    }
  }
});

组件中使用:

<template>
  <div>
    <button
      v-for="item in items"
      :key="item.id"
      @click="toggleSelect(item)"
      :class="{ 'selected': isSelected(item) }"
    >
      {{ item.name }}
    </button>
  </div>
</template>

<script>
import { mapGetters, mapMutations } from 'vuex';

export default {
  data() {
    return {
      items: [
        { id: 1, name: 'Item 1' },
        { id: 2, name: 'Item 2' }
      ]
    };
  },
  computed: {
    ...mapGetters(['isSelected'])
  },
  methods: {
    ...mapMutations(['toggleSelect'])
  }
};
</script>

以上方法可以根据实际需求灵活选择,实现选中功能。

分享给朋友:

相关文章

vue实现编辑

vue实现编辑

Vue 实现编辑功能 在 Vue 中实现编辑功能通常涉及表单绑定、状态管理和事件处理。以下是一个常见的实现方法: 双向数据绑定 使用 v-model 指令实现表单元素与数据的双向绑定: <i…

vue 实现拖拉

vue 实现拖拉

Vue 实现拖拽功能的方法 使用 HTML5 原生拖放 API HTML5 提供了原生的拖放 API,可以通过 draggable 属性、dragstart、dragend、dragover 和 dr…

vue实现注册

vue实现注册

Vue 实现注册功能 注册功能通常涉及前端表单、数据验证、与后端 API 的交互等步骤。以下是一个完整的 Vue 实现注册功能的方案。 创建注册表单 使用 Vue 的模板语法创建注册表单,包含用户名…

轮播vue实现

轮播vue实现

使用 Vue 实现轮播功能 基础轮播实现 安装依赖(如使用 Swiper): npm install swiper vue-awesome-swiper 组件代码示例: <templ…

vue实现CRUD

vue实现CRUD

Vue 实现 CRUD 操作 Vue 提供了响应式数据绑定和组件化开发能力,结合后端 API 可以轻松实现 CRUD(增删改查)功能。以下是基于 Vue 3 和 Axios 的完整实现方案。 安装依…

vue实现截屏怎么实现

vue实现截屏怎么实现

Vue实现截屏的方法 使用html2canvas库 html2canvas是一个流行的JavaScript库,可以将网页元素转换为Canvas图像。在Vue项目中安装html2canvas: npm…