当前位置:首页 > VUE

vue实现表格的单选

2026-02-22 17:29:22VUE

Vue 实现表格单选功能

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

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

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

vue实现表格的单选

<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>

方法二:自定义单选逻辑

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

vue实现表格的单选

<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 管理。

// 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 的内置功能适合快速实现,自定义逻辑则提供更大的灵活性。

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

相关文章

用css制作表格

用css制作表格

使用CSS制作表格 CSS可以用来样式化HTML表格,使其更具视觉吸引力和功能性。以下是几种常见的方法: 基础表格样式 通过CSS可以调整表格的边框、间距和颜色: <table>…

vue实现表格多行修改

vue实现表格多行修改

实现表格多行修改的方法 数据绑定与表格渲染 使用v-model绑定表格数据到Vue实例的data属性。通过v-for循环渲染表格行,并为每行数据添加编辑状态标识。 <template>…

vue实现表格上传头像

vue实现表格上传头像

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

css好看的表格制作

css好看的表格制作

使用CSS制作美观表格的方法 边框与间距优化 通过border-collapse属性合并边框,避免双边框效果。设置padding增加单元格内边距,提升可读性。 table { border-…

css制作表格

css制作表格

CSS 制作表格的方法 使用 CSS 可以灵活地创建和样式化表格,以下是几种常见的方法: 使用 HTML 表格标签结合 CSS 样式 通过 HTML 的 <table>、<tr&g…

jquery 单选

jquery 单选

jQuery 单选按钮操作 在 jQuery 中,操作单选按钮(Radio Button)通常涉及选中状态、取值和事件绑定。以下是常见操作示例: 获取选中的单选按钮值 通过 :checked 选择器…