当前位置:首页 > VUE

vue实现单选表格

2026-02-19 14:10:10VUE

实现单选表格的基本思路

在Vue中实现单选表格需要结合表格组件和单选逻辑。通常使用<table>元素或第三方UI库(如Element UI、Ant Design Vue)的表格组件,通过v-model或自定义事件管理选中状态。

使用原生HTML表格实现

<template>
  <table>
    <thead>
      <tr>
        <th>选择</th>
        <th>ID</th>
        <th>名称</th>
      </tr>
    </thead>
    <tbody>
      <tr v-for="item in tableData" :key="item.id" @click="selectRow(item)">
        <td>
          <input 
            type="radio" 
            :checked="selectedItem && selectedItem.id === item.id"
            @click.stop
          />
        </td>
        <td>{{ item.id }}</td>
        <td>{{ item.name }}</td>
      </tr>
    </tbody>
  </table>
</template>

<script>
export default {
  data() {
    return {
      tableData: [
        { id: 1, name: '项目A' },
        { id: 2, name: '项目B' },
        { id: 3, name: '项目C' }
      ],
      selectedItem: null
    }
  },
  methods: {
    selectRow(item) {
      this.selectedItem = item
    }
  }
}
</script>

使用Element UI实现

安装Element UI后,可通过el-table和单选列快速实现:

<template>
  <el-table
    :data="tableData"
    @current-change="handleCurrentChange"
    highlight-current-row
  >
    <el-table-column prop="id" label="ID" />
    <el-table-column prop="name" label="名称" />
  </el-table>
</template>

<script>
export default {
  data() {
    return {
      tableData: [
        { id: 1, name: '项目A' },
        { id: 2, name: '项目B' },
        { id: 3, name: '项目C' }
      ]
    }
  },
  methods: {
    handleCurrentChange(val) {
      console.log('当前选中:', val)
    }
  }
}
</script>

使用Ant Design Vue实现

安装Ant Design Vue后,通过a-table和自定义选择逻辑实现:

<template>
  <a-table
    :columns="columns"
    :data-source="tableData"
    :row-selection="rowSelection"
  />
</template>

<script>
const columns = [
  { title: 'ID', dataIndex: 'id' },
  { title: '名称', dataIndex: 'name' }
]

export default {
  data() {
    return {
      tableData: [
        { id: 1, name: '项目A' },
        { id: 2, name: '项目B' },
        { id: 3, name: '项目C' }
      ],
      selectedRowKeys: []
    }
  },
  computed: {
    rowSelection() {
      return {
        type: 'radio',
        selectedRowKeys: this.selectedRowKeys,
        onChange: keys => {
          this.selectedRowKeys = keys
        }
      }
    }
  }
}
</script>

自定义单选逻辑实现

对于需要更复杂控制的场景,可手动管理选中状态:

<template>
  <table>
    <tr 
      v-for="item in tableData" 
      :key="item.id"
      :class="{ 'active': selectedId === item.id }"
      @click="selectedId = item.id"
    >
      <td>{{ item.id }}</td>
      <td>{{ item.name }}</td>
    </tr>
  </table>
</template>

<script>
export default {
  data() {
    return {
      tableData: [...],
      selectedId: null
    }
  }
}
</script>

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

性能优化建议

对于大数据量表格,建议:

vue实现单选表格

  • 使用虚拟滚动(如vue-virtual-scroller)
  • 避免在模板中进行复杂计算
  • 使用Object.freeze冻结静态数据
  • 考虑分页加载

以上方法可根据项目需求和技术栈选择最适合的实现方式。第三方UI库通常提供更丰富的功能和样式支持,而原生实现则更灵活可控。

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

相关文章

怎么制作css表格

怎么制作css表格

使用HTML和CSS创建表格 HTML提供<table>标签用于创建表格结构,CSS用于控制样式。以下是一个基础示例: <table class="styled-table">…

vue表格动态实现

vue表格动态实现

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

vue实现表格输入

vue实现表格输入

Vue实现表格输入的方法 基础表格实现 使用v-for指令动态渲染表格行和列,结合v-model实现双向数据绑定。 <template> <table> <…

纯vue实现表格

纯vue实现表格

Vue 表格实现基础 使用 Vue 实现表格主要依赖 v-for 指令和数组数据绑定。以下是一个基础示例: <template> <table> <thea…

vue实现表格分页

vue实现表格分页

实现表格分页的基本思路 在Vue中实现表格分页通常需要结合以下核心功能:数据分片(根据页码和每页条数截取数据)、分页器组件(控制页码跳转)、表格渲染(展示当前页数据)。以下是具体实现方法。 准备分页…

vue实现单选表格

vue实现单选表格

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