当前位置:首页 > VUE

vue实现表格的单选

2026-02-22 17:29:22VUE

Vue 实现表格单选功能

在 Vue 中实现表格的单选功能,可以通过以下方法完成。这里以 Element UI 的表格组件为例,但原理同样适用于其他 UI 库或原生表格。

方法一:使用 Element UI 的表格单选功能

Element UI 的 el-table 组件内置了单选功能,可以通过 highlight-current-row 属性开启高亮当前行,并通过 @current-change 事件获取选中行数据。

<template>
  <el-table
    :data="tableData"
    highlight-current-row
    @current-change="handleCurrentChange"
    style="width: 100%">
    <el-table-column
      prop="name"
      label="姓名">
    </el-table-column>
    <el-table-column
      prop="age"
      label="年龄">
    </el-table-column>
  </el-table>
</template>

<script>
export default {
  data() {
    return {
      tableData: [
        { name: '张三', age: 20 },
        { name: '李四', age: 25 },
        { name: '王五', age: 30 }
      ],
      currentRow: null
    }
  },
  methods: {
    handleCurrentChange(val) {
      this.currentRow = val
    }
  }
}
</script>

方法二:自定义单选逻辑

如果需要更灵活的控制,可以手动实现单选逻辑。例如,通过点击行时切换选中状态。

<template>
  <table>
    <tr
      v-for="(item, index) in tableData"
      :key="index"
      @click="selectRow(index)"
      :class="{ 'selected': selectedIndex === index }">
      <td>{{ item.name }}</td>
      <td>{{ item.age }}</td>
    </tr>
  </table>
</template>

<script>
export default {
  data() {
    return {
      tableData: [
        { name: '张三', age: 20 },
        { name: '李四', age: 25 },
        { name: '王五', age: 30 }
      ],
      selectedIndex: -1
    }
  },
  methods: {
    selectRow(index) {
      this.selectedIndex = index
    }
  }
}
</script>

<style>
.selected {
  background-color: #f0f0f0;
}
</style>

方法三:使用单选按钮

另一种常见的方式是在每行添加单选按钮,通过绑定 v-model 实现单选。

<template>
  <table>
    <tr v-for="(item, index) in tableData" :key="index">
      <td>
        <input
          type="radio"
          :value="index"
          v-model="selectedIndex">
      </td>
      <td>{{ item.name }}</td>
      <td>{{ item.age }}</td>
    </tr>
  </table>
</template>

<script>
export default {
  data() {
    return {
      tableData: [
        { name: '张三', age: 20 },
        { name: '李四', age: 25 },
        { name: '王五', age: 30 }
      ],
      selectedIndex: -1
    }
  }
}
</script>

方法四:使用 Vuex 管理选中状态

如果需要在多个组件间共享选中状态,可以使用 Vuex 管理。

vue实现表格的单选

// store.js
export default new Vuex.Store({
  state: {
    selectedRow: null
  },
  mutations: {
    setSelectedRow(state, row) {
      state.selectedRow = row
    }
  }
})
<template>
  <el-table
    :data="tableData"
    highlight-current-row
    @current-change="handleCurrentChange">
    <!-- 表格列定义 -->
  </el-table>
</template>

<script>
import { mapMutations } from 'vuex'

export default {
  data() {
    return {
      tableData: [
        { name: '张三', age: 20 },
        { name: '李四', age: 25 },
        { name: '王五', age: 30 }
      ]
    }
  },
  methods: {
    ...mapMutations(['setSelectedRow']),
    handleCurrentChange(row) {
      this.setSelectedRow(row)
    }
  }
}
</script>

以上方法可以根据具体需求选择使用。Element UI 的内置功能适合快速实现,自定义逻辑则提供更大的灵活性。

标签: 单选表格
分享给朋友:

相关文章

vue实现 单选

vue实现 单选

实现 Vue 单选功能 在 Vue 中实现单选功能可以通过多种方式完成,以下是几种常见的方法: 使用 v-model 绑定单选按钮 通过 v-model 可以轻松实现单选功能,将单选按钮的值绑定到同…

css 制作表格

css 制作表格

基础表格结构 使用HTML的<table>标签创建表格框架,搭配<tr>(行)、<th>(表头)和<td>(单元格)标签。例如: <table&…

表格制作css

表格制作css

基础表格样式 使用CSS可以轻松地为HTML表格添加样式。以下是一个基础表格的HTML和CSS示例: <table class="basic-table"> <thead>…

vue实现表格读取数据

vue实现表格读取数据

Vue 实现表格数据读取 在 Vue 中实现表格数据读取通常涉及以下关键步骤: 数据绑定与表格渲染 通过 v-for 指令动态渲染表格行,数据通常存储在组件的 data 或 Vuex 状态中:…

vue实现表格上传头像

vue实现表格上传头像

实现表格上传头像功能 在Vue中实现表格上传头像功能,可以通过以下步骤完成。这里假设使用Element UI作为UI框架,但方法同样适用于其他UI库或原生实现。 安装必要依赖 确保项目中已安装Ele…

vue element实现表格

vue element实现表格

Vue + Element UI 实现表格 Element UI 提供了强大的表格组件 el-table,结合 Vue 可以快速实现数据展示、排序、筛选等功能。 基础表格实现 安装 Element…