当前位置:首页 > VUE

vue实现表格的单选

2026-01-22 02:23:08VUE

Vue 实现表格单选功能

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

vue实现表格的单选

使用 v-model 绑定选中项

通过 v-model 绑定一个变量来记录当前选中的行数据。在表格的每一行添加单选按钮或点击事件来更新选中状态。

vue实现表格的单选

<template>
  <table>
    <thead>
      <tr>
        <th>选择</th>
        <th>姓名</th>
        <th>年龄</th>
      </tr>
    </thead>
    <tbody>
      <tr 
        v-for="item in tableData" 
        :key="item.id"
        @click="selectRow(item)"
        :class="{ 'selected': selectedItem === item }"
      >
        <td>
          <input 
            type="radio" 
            :checked="selectedItem === item"
            @click.stop="selectRow(item)"
          />
        </td>
        <td>{{ item.name }}</td>
        <td>{{ item.age }}</td>
      </tr>
    </tbody>
  </table>
</template>

<script>
export default {
  data() {
    return {
      tableData: [
        { id: 1, name: '张三', age: 25 },
        { id: 2, name: '李四', age: 30 },
        { id: 3, name: '王五', age: 28 }
      ],
      selectedItem: null
    };
  },
  methods: {
    selectRow(item) {
      this.selectedItem = item;
    }
  }
};
</script>

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

通过计算属性判断选中状态

利用计算属性或方法动态判断当前行是否被选中,减少模板中的逻辑。

<template>
  <table>
    <thead>
      <tr>
        <th>选择</th>
        <th>姓名</th>
        <th>年龄</th>
      </tr>
    </thead>
    <tbody>
      <tr 
        v-for="item in tableData" 
        :key="item.id"
        @click="selectRow(item)"
        :class="{ 'selected': isSelected(item) }"
      >
        <td>
          <input 
            type="radio" 
            :checked="isSelected(item)"
            @click.stop="selectRow(item)"
          />
        </td>
        <td>{{ item.name }}</td>
        <td>{{ item.age }}</td>
      </tr>
    </tbody>
  </table>
</template>

<script>
export default {
  data() {
    return {
      tableData: [
        { id: 1, name: '张三', age: 25 },
        { id: 2, name: '李四', age: 30 },
        { id: 3, name: '王五', age: 28 }
      ],
      selectedItem: null
    };
  },
  methods: {
    selectRow(item) {
      this.selectedItem = item;
    },
    isSelected(item) {
      return this.selectedItem === item;
    }
  }
};
</script>

使用第三方表格组件

如果使用 Element UI 或 Ant Design Vue 等第三方组件库,可以更方便地实现单选功能。以下是 Element UI 的示例:

<template>
  <el-table
    :data="tableData"
    @row-click="handleRowClick"
    highlight-current-row
  >
    <el-table-column label="姓名" prop="name"></el-table-column>
    <el-table-column label="年龄" prop="age"></el-table-column>
  </el-table>
</template>

<script>
export default {
  data() {
    return {
      tableData: [
        { id: 1, name: '张三', age: 25 },
        { id: 2, name: '李四', age: 30 },
        { id: 3, name: '王五', age: 28 }
      ]
    };
  },
  methods: {
    handleRowClick(row) {
      console.log('选中行:', row);
    }
  }
};
</script>

关键点总结

  • 使用 v-model 或变量绑定选中状态。
  • 通过点击事件或单选按钮更新选中项。
  • 高亮选中行可以通过 CSS 或组件库内置功能实现。
  • 第三方组件库(如 Element UI)提供更便捷的单选支持。

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

相关文章

vue表格实现教学

vue表格实现教学

基础表格实现 使用 Vue 的 v-for 指令渲染数组数据到表格中。 <template> <table> <thead> <…

vue实现表格多行修改

vue实现表格多行修改

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

vue实现动态用户表格

vue实现动态用户表格

Vue 动态用户表格实现方法 基础表格结构 使用<table>或第三方组件库(如Element UI的el-table)构建基础框架。静态数据示例: <template>…

使用vue实现表格

使用vue实现表格

使用 Vue 实现表格 Vue.js 提供了灵活的方式来实现表格功能,可以通过组合 v-for 指令和动态数据绑定快速构建表格。以下是几种常见的实现方法: 基础表格实现 通过 v-for 遍历数组数…

vue实现分组表格

vue实现分组表格

Vue 实现分组表格的方法 使用 v-for 嵌套循环实现分组 通过嵌套的 v-for 循环可以轻松实现分组表格。外层循环遍历分组数据,内层循环遍历每组中的具体项。 <template>…

vue 实现跨行表格

vue 实现跨行表格

实现跨行表格的方法 在Vue中实现跨行表格可以通过多种方式完成,以下是几种常见的方法: 使用HTML原生表格 HTML原生表格支持rowspan属性,可以直接在Vue模板中使用: <tabl…