元素,并通过v-model绑定选中状态。核心逻辑是确保同一时…">
当前位置:首页 > VUE

vue表格实现单选框

2026-03-27 04:04:52VUE

实现单选框的基本思路

在Vue中实现表格单选框,通常需要结合<table><input type="radio">元素,并通过v-model绑定选中状态。核心逻辑是确保同一时间只有一个选项被选中。

基础实现代码示例

<template>
  <table>
    <thead>
      <tr>
        <th>选择</th>
        <th>姓名</th>
        <th>年龄</th>
      </tr>
    </thead>
    <tbody>
      <tr v-for="(item, index) in tableData" :key="index">
        <td>
          <input 
            type="radio" 
            v-model="selectedRow" 
            :value="item.id"
          >
        </td>
        <td>{{ item.name }}</td>
        <td>{{ item.age }}</td>
      </tr>
    </tbody>
  </table>
</template>

<script>
export default {
  data() {
    return {
      selectedRow: null,
      tableData: [
        { id: 1, name: '张三', age: 25 },
        { id: 2, name: '李四', age: 30 },
        { id: 3, name: '王五', age: 28 }
      ]
    }
  }
}
</script>

使用组件库优化

如果使用Element UI等组件库,可以直接使用其提供的单选表格组件:

<template>
  <el-table
    :data="tableData"
    @current-change="handleCurrentChange"
    highlight-current-row
  >
    <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: 25 },
        { name: '李四', age: 30 },
        { name: '王五', age: 28 }
      ]
    }
  },
  methods: {
    handleCurrentChange(val) {
      console.log('当前选中行:', val)
    }
  }
}
</script>

自定义样式和交互

可以通过CSS自定义单选按钮样式,并添加交互效果:

/* 隐藏原生单选按钮 */
input[type="radio"] {
  appearance: none;
  -webkit-appearance: none;
  width: 16px;
  height: 16px;
  border: 1px solid #999;
  border-radius: 50%;
  outline: none;
  cursor: pointer;
}

/* 选中状态样式 */
input[type="radio"]:checked {
  background-color: #409EFF;
  border-color: #409EFF;
}

/* 添加hover效果 */
tr:hover {
  background-color: #f5f5f5;
}

完整功能实现

结合全选/反选功能和获取选中数据:

vue表格实现单选框

<template>
  <div>
    <button @click="clearSelection">清除选择</button>
    <table>
      <!-- 表格内容同上 -->
    </table>
    <p>当前选中ID: {{ selectedRow }}</p>
  </div>
</template>

<script>
export default {
  methods: {
    clearSelection() {
      this.selectedRow = null
    },
    getSelectedData() {
      return this.tableData.find(item => item.id === this.selectedRow)
    }
  }
}
</script>

注意事项

  • 确保每个单选按钮的value值是唯一的,通常使用数据项的ID
  • 对于大量数据,考虑使用虚拟滚动优化性能
  • 移动端适配可能需要调整单选按钮的大小和间距
  • 如果表格有分页,需要处理跨页选中的状态保持问题

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

相关文章

css表格表单制作

css表格表单制作

CSS表格制作 使用CSS创建表格主要涉及<table>标签及其子元素的样式控制。以下是一个基础示例: <table class="styled-table"> <…

vue实现表格拖动列宽

vue实现表格拖动列宽

实现表格列宽拖动的核心思路 通过监听鼠标事件(mousedown、mousemove、mouseup)实现拖拽逻辑,动态修改列宽。需要处理表头与表体的联动,确保拖动时视觉一致。 基础HTML结构 &…

css怎么制作表格

css怎么制作表格

使用HTML和CSS制作表格 在HTML中,表格由<table>标签定义,结合CSS可以灵活控制样式。 基本HTML结构: <table> <tr>…

vue实现表格树

vue实现表格树

Vue 实现表格树的方法 使用递归组件实现树形表格 在 Vue 中可以通过递归组件实现树形表格结构。定义一个组件,该组件可以调用自身来渲染子节点。 <template> <ta…

vue实现表格序号

vue实现表格序号

实现表格序号的常见方法 在Vue中为表格添加序号列可以通过多种方式实现,以下是几种典型方案: 使用v-for索引 <template> <table> <…

vue实现表格组件

vue实现表格组件

Vue 表格组件实现方法 基础表格实现 使用 Vue 的模板语法可以快速实现基础表格功能。通过 v-for 指令循环渲染表格数据。 <template> <table>…