当前位置:首页 > VUE

vue实现表格的单选

2026-01-22 02:23:08VUE

Vue 实现表格单选功能

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

使用 v-model 绑定选中项

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

<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 的示例:

vue实现表格的单选

<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中实现单选功能可以通过多种方式,以下是几种常见的实现方法: 使用原生HTML单选按钮 通过v-model绑定数据,结合原生<input type="radio"&g…

用vue实现表格

用vue实现表格

Vue 表格实现方法 基础表格实现 使用 Vue 的 v-for 指令可以轻松渲染表格数据。准备一个数据数组,在模板中用 v-for 循环渲染。 <template> <tab…

vue实现表格透明

vue实现表格透明

实现表格透明的方法 在Vue中实现表格透明效果,可以通过CSS样式调整表格的背景透明度。以下是几种常见的方法: 设置表格背景透明度 通过CSS的background-color属性配合rgba()…

vue实现全选表格

vue实现全选表格

Vue实现表格全选功能 实现表格全选功能主要分为以下几个部分: 数据绑定 在Vue组件中定义数据模型,包括表格数据和全选状态: data() { return { tableData: […

vue表格实现插入

vue表格实现插入

Vue 表格实现插入功能 在 Vue 中实现表格的插入功能,可以通过操作数据数组动态添加新行。以下是一个基于 Element UI 表格组件的实现示例: 基本实现方法 安装 Element UI(如…

vue实现表格复制

vue实现表格复制

Vue 实现表格复制功能 在 Vue 中实现表格复制功能,可以通过以下几种方法实现: 使用 Clipboard API Clipboard API 是现代浏览器提供的原生 API,可以方便地实现复制…