当前位置:首页 > 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-virtual-scroller)
  • 避免在模板中进行复杂计算
  • 使用Object.freeze冻结静态数据
  • 考虑分页加载

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

vue实现单选表格

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

相关文章

vue   实现单选

vue 实现单选

Vue 实现单选功能 在 Vue 中实现单选功能可以通过多种方式,以下是几种常见的方法: 使用原生 <input type="radio"> 绑定 v-model 通过 v-model…

vue 实现表格单选

vue 实现表格单选

实现表格单选的方法 在Vue中实现表格单选功能可以通过多种方式完成,以下是几种常见的实现方法: 使用v-model绑定选中项 通过v-model绑定一个变量来记录当前选中的行数据。当用户点击某一行时…

css怎么制作表格

css怎么制作表格

使用HTML和CSS创建表格 HTML提供<table>标签用于创建表格结构,结合CSS可以自定义样式。以下是基本实现方法: HTML结构 <table> <…

vue实现表格读取数据

vue实现表格读取数据

Vue 实现表格数据读取 在 Vue 中实现表格数据读取通常涉及以下关键步骤: 数据绑定与表格渲染 通过 v-for 指令动态渲染表格行,数据通常存储在组件的 data 或 Vuex 状态中:…

vue动态表格实现

vue动态表格实现

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

网页表格制作css代码

网页表格制作css代码

基础表格样式 使用CSS为HTML表格添加基础样式,包括边框、间距和背景色: table { width: 100%; border-collapse: collapse; margin…