当前位置:首页 > VUE

vue实现单选表格

2026-01-18 21:47:22VUE

实现单选表格的基本思路

在Vue中实现单选表格通常需要结合<table>元素和Vue的数据绑定特性。通过v-model或自定义事件管理选中状态,确保同一时间仅有一行被选中。

vue实现单选表格

使用v-model绑定选中状态

定义数据对象存储当前选中行的唯一标识(如ID),结合v-modelv-bind:checked实现单选逻辑。

vue实现单选表格

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

<script>
export default {
  data() {
    return {
      selectedId: null,
      tableData: [
        { id: 1, name: 'Alice', age: 25 },
        { id: 2, name: 'Bob', age: 30 }
      ]
    };
  },
  methods: {
    handleSelect(item) {
      console.log('选中行:', item);
    }
  }
};
</script>

通过事件手动控制选中状态

若需更灵活的控制(如避免使用v-model),可通过点击事件动态更新选中状态。

<template>
  <table>
    <tr 
      v-for="item in tableData" 
      :key="item.id"
      @click="selectRow(item.id)"
      :class="{ 'selected': selectedId === item.id }"
    >
      <td>
        <input 
          type="radio" 
          :checked="selectedId === item.id"
          @click.stop
        >
      </td>
      <td>{{ item.name }}</td>
      <td>{{ item.age }}</td>
    </tr>
  </table>
</template>

<script>
export default {
  data() {
    return {
      selectedId: null,
      tableData: [/* 数据同上 */]
    };
  },
  methods: {
    selectRow(id) {
      this.selectedId = id;
    }
  }
};
</script>

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

使用第三方组件库(如Element UI)

若项目中使用Element UI,可直接利用其<el-table>组件的单选功能。

<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: [/* 数据同上 */]
    };
  },
  methods: {
    handleCurrentChange(val) {
      console.log('当前选中行:', val);
    }
  }
};
</script>

关键注意事项

  • 唯一标识:确保每行数据有唯一id或其他标识字段。
  • 性能优化:大数据量时考虑虚拟滚动(如使用vue-virtual-scroller)。
  • 无障碍访问:为单选按钮添加aria-label或通过<label>关联文本。

通过上述方法,可灵活实现符合业务需求的单选表格功能。

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

相关文章

在vue实现学生表格

在vue实现学生表格

创建 Vue 项目 使用 Vue CLI 或 Vite 初始化项目: npm init vue@latest student-table cd student-table npm install…

vue实现 单选

vue实现 单选

实现 Vue 单选功能 在 Vue 中实现单选功能可以通过多种方式完成,以下是几种常见的方法: 使用 v-model 绑定单选按钮 通过 v-model 可以轻松实现单选功能,将单选按钮的值绑定到…

vue实现div单选

vue实现div单选

Vue 实现 div 单选 在 Vue 中实现 div 单选可以通过 v-model 结合 v-for 和事件绑定来完成。以下是具体实现方法: 模板部分 <template>…

vue实现子表格

vue实现子表格

Vue 实现子表格的方法 在 Vue 中实现子表格可以通过多种方式完成,以下是几种常见的方法: 使用嵌套组件 创建一个父表格组件和一个子表格组件,通过 props 将数据传递给子表格组件。父表格的每…

vue如何实现单选

vue如何实现单选

Vue 实现单选的方法 在 Vue 中实现单选功能可以通过多种方式完成,以下是几种常见的实现方法: 使用 v-model 绑定单选按钮 通过 v-model 绑定到同一个变量,确保同一时间只有一个选…

vue实现图片单选

vue实现图片单选

实现图片单选功能 在Vue中实现图片单选功能可以通过v-model绑定选中状态,结合CSS样式控制选中效果。以下是具体实现方式: 基础实现方案 <template> <div…