当前位置:首页 > VUE

vue实现单选表格

2026-01-18 21:47:22VUE

实现单选表格的基本思路

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

使用v-model绑定选中状态

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

<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>组件的单选功能。

vue实现单选表格

<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表格的制作方法

基础表格结构 使用<table>标签创建表格框架,<tr>定义行,<td>定义单元格: <table> <tr> <td…

css表格制作表格

css表格制作表格

CSS 表格制作方法 基础表格结构 使用HTML的<table>标签创建表格框架,搭配<tr>(行)、<td>(单元格)和<th>(表头)标签: &l…

jquery 单选

jquery 单选

jQuery 单选按钮操作 在 jQuery 中,操作单选按钮(Radio Button)通常涉及选中状态、取值和事件绑定。以下是常见操作示例: 获取选中的单选按钮值 通过 :checked 选择器…

vue动态表格实现

vue动态表格实现

Vue动态表格实现方法 使用v-for指令渲染动态数据 通过v-for指令可以循环遍历数组数据生成表格行,数据变化时表格自动更新。示例代码: <template> <table…

vue表格动态实现

vue表格动态实现

Vue 表格动态实现方法 动态表格的实现通常涉及数据绑定、列配置动态化以及交互功能的灵活处理。以下是几种常见场景的具体实现方式: 动态数据绑定 通过v-for指令循环渲染表格行数据,数据变化时表格自…

vue实现表格刷新

vue实现表格刷新

Vue 实现表格刷新的方法 使用 v-if 强制重新渲染 通过 v-if 控制表格的显示与隐藏,切换时 Vue 会重新渲染组件。 <template> <button @cli…