当前位置:首页 > VUE

vue 实现表格单选

2026-03-07 20:23:08VUE

实现表格单选的方法

在 Vue 中实现表格单选功能可以通过多种方式完成,以下是几种常见的实现方法:

使用 v-model 绑定选中项

通过 v-model 绑定一个变量来存储当前选中的行数据,结合 @click 事件实现单选逻辑。

vue 实现表格单选

<template>
  <table>
    <tr 
      v-for="item in tableData" 
      :key="item.id"
      @click="selectedRow = item"
      :class="{ 'selected': selectedRow === item }"
    >
      <td>{{ item.name }}</td>
      <td>{{ item.age }}</td>
    </tr>
  </table>
</template>

<script>
export default {
  data() {
    return {
      tableData: [
        { id: 1, name: 'Alice', age: 25 },
        { id: 2, name: 'Bob', age: 30 }
      ],
      selectedRow: null
    }
  }
}
</script>

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

使用 el-table 组件(Element UI)

如果使用 Element UI 的 el-table 组件,可以直接利用其内置的单选功能。

vue 实现表格单选

<template>
  <el-table
    :data="tableData"
    highlight-current-row
    @current-change="handleCurrentChange"
  >
    <el-table-column prop="name" label="Name"></el-table-column>
    <el-table-column prop="age" label="Age"></el-table-column>
  </el-table>
</template>

<script>
export default {
  data() {
    return {
      tableData: [
        { id: 1, name: 'Alice', age: 25 },
        { id: 2, name: 'Bob', age: 30 }
      ],
      currentRow: null
    }
  },
  methods: {
    handleCurrentChange(val) {
      this.currentRow = val;
    }
  }
}
</script>

使用自定义单选按钮

通过添加单选按钮列,实现更直观的单选交互。

<template>
  <table>
    <tr v-for="item in tableData" :key="item.id">
      <td>
        <input 
          type="radio" 
          :name="'tableRadio'" 
          :value="item.id"
          v-model="selectedId"
        >
      </td>
      <td>{{ item.name }}</td>
      <td>{{ item.age }}</td>
    </tr>
  </table>
</template>

<script>
export default {
  data() {
    return {
      tableData: [
        { id: 1, name: 'Alice', age: 25 },
        { id: 2, name: 'Bob', age: 30 }
      ],
      selectedId: null
    }
  }
}
</script>

使用计算属性获取选中行

通过计算属性动态获取当前选中的行数据,方便后续操作。

<template>
  <table>
    <tr 
      v-for="item in tableData" 
      :key="item.id"
      @click="selectedId = item.id"
      :class="{ 'selected': selectedId === item.id }"
    >
      <td>{{ item.name }}</td>
      <td>{{ item.age }}</td>
    </tr>
  </table>
</template>

<script>
export default {
  data() {
    return {
      tableData: [
        { id: 1, name: 'Alice', age: 25 },
        { id: 2, name: 'Bob', age: 30 }
      ],
      selectedId: null
    }
  },
  computed: {
    selectedRow() {
      return this.tableData.find(item => item.id === this.selectedId);
    }
  }
}
</script>

注意事项

  • 如果表格数据是动态加载的,需要在数据更新后重置选中状态。
  • 高亮样式可以通过 CSS 自定义,确保用户体验一致。
  • 对于大型表格,考虑性能优化,避免不必要的渲染。

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

相关文章

vue   实现单选

vue 实现单选

Vue 实现单选功能 在 Vue 中实现单选功能可以通过多种方式,以下是几种常见的方法: 使用原生 <input type="radio"> 绑定 v-model 通过 v-model…

vue实现简单表格

vue实现简单表格

Vue 实现简单表格的方法 使用原生 HTML 表格 在 Vue 模板中可以直接使用 HTML 原生表格标签,通过 v-for 动态渲染数据: <template> <tabl…

网页表格制作css代码

网页表格制作css代码

基础表格样式 使用CSS为HTML表格添加基础样式,如边框、间距和背景色: table { width: 100%; border-collapse: collapse; margin:…

国外vue实现表格控件

国外vue实现表格控件

以下是国外流行的 Vue 表格控件实现方案及特点,供参考: vue-good-table 基于 Vue 2/3 的轻量级表格组件,支持排序、分页、筛选等功能。 安装方式: npm instal…

h5实现表格样式

h5实现表格样式

实现H5表格样式的方法 使用HTML和CSS创建基础表格 在HTML中,表格通过<table>标签定义,配合<tr>(行)、<td>(单元格)和<th>…

vue实现表格导入

vue实现表格导入

Vue 实现表格导入的方法 使用 Element UI 的 Upload 组件 Element UI 提供了 el-upload 组件,可以方便地实现文件上传功能。结合 xlsx 库解析 Excel…