当前位置:首页 > 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>关联文本。

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

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

相关文章

css表格制作表格

css表格制作表格

CSS 表格制作方法 使用 CSS 创建表格可以通过多种方式实现,包括原生 HTML 表格元素结合 CSS 样式,或者使用纯 CSS 布局技术(如 Flexbox 或 Grid)模拟表格效果。以下是常…

vue实现单选

vue实现单选

Vue 实现单选按钮 在 Vue 中实现单选按钮通常使用 v-model 绑定数据,结合 <input type="radio"> 或第三方组件库(如 Element UI、Ant Des…

vue实现表格数据修改

vue实现表格数据修改

实现表格数据修改的基本思路 在Vue中实现表格数据修改通常涉及以下核心步骤:数据绑定、编辑状态切换、表单输入处理和数据提交。以下是一个典型实现方案: 数据绑定与渲染 使用v-for指令循环渲染表格数…

vue   实现单选

vue 实现单选

实现单选的基本方法 在Vue中实现单选功能可以通过v-model绑定数据和v-for循环渲染选项。以下是一个基本示例: <template> <div> <…

vue实现子表格

vue实现子表格

实现子表格的基本思路 在Vue中实现子表格通常需要结合组件化思想和动态渲染技术。常见的场景是在主表格的行内嵌套子表格,展开主表行时显示对应的子表数据。 使用el-table实现子表格(Element…

vue实现图片单选

vue实现图片单选

Vue 实现图片单选功能 在 Vue 中实现图片单选功能可以通过多种方式完成,以下是两种常见的实现方法: 方法一:使用 v-model 和单选按钮 通过将单选按钮与图片绑定,利用 v-mod…