当前位置:首页 > VUE

vue前端table实现单选

2026-02-23 04:58:32VUE

实现 Vue 表格单选功能

在 Vue 中实现表格单选功能可以通过多种方式完成,以下是几种常见的方法:

使用 v-model 和计算属性

通过绑定 v-model 到当前选中的行数据,结合计算属性实现单选逻辑。

<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" 
            v-model="selectedRow"
          />
        </td>
        <td>{{ item.name }}</td>
        <td>{{ item.age }}</td>
      </tr>
    </tbody>
  </table>
</template>

<script>
export default {
  data() {
    return {
      tableData: [
        { id: 1, name: '张三', age: 25 },
        { id: 2, name: '李四', age: 30 },
        { id: 3, name: '王五', age: 28 }
      ],
      selectedRow: null
    }
  }
}
</script>

使用自定义点击事件

通过点击事件手动控制选中状态,适用于更复杂的交互场景。

<template>
  <table>
    <thead>
      <tr>
        <th>选择</th>
        <th>姓名</th>
        <th>年龄</th>
      </tr>
    </thead>
    <tbody>
      <tr 
        v-for="item in tableData" 
        :key="item.id"
        @click="selectRow(item)"
        :class="{ 'active': selectedRow && selectedRow.id === item.id }"
      >
        <td>
          <input 
            type="radio" 
            :checked="selectedRow && selectedRow.id === item.id"
          />
        </td>
        <td>{{ item.name }}</td>
        <td>{{ item.age }}</td>
      </tr>
    </tbody>
  </table>
</template>

<script>
export default {
  data() {
    return {
      tableData: [
        { id: 1, name: '张三', age: 25 },
        { id: 2, name: '李四', age: 30 },
        { id: 3, name: '王五', age: 28 }
      ],
      selectedRow: null
    }
  },
  methods: {
    selectRow(row) {
      this.selectedRow = row
    }
  }
}
</script>

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

使用第三方组件库

如果项目中使用了 Element UI 或 Ant Design Vue 等组件库,可以直接使用其提供的单选表格功能。

以 Element UI 为例:

<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: [
        { id: 1, name: '张三', age: 25 },
        { id: 2, name: '李四', age: 30 },
        { id: 3, name: '王五', age: 28 }
      ]
    }
  },
  methods: {
    handleCurrentChange(val) {
      console.log('当前选中行:', val)
    }
  }
}
</script>

使用 Vuex 管理状态

对于大型应用,可以通过 Vuex 集中管理选中状态。

vue前端table实现单选

// store.js
export default new Vuex.Store({
  state: {
    selectedRow: null
  },
  mutations: {
    setSelectedRow(state, row) {
      state.selectedRow = row
    }
  }
})
<template>
  <table>
    <tr 
      v-for="item in tableData" 
      :key="item.id"
      @click="$store.commit('setSelectedRow', item)"
      :class="{ 'active': $store.state.selectedRow && $store.state.selectedRow.id === item.id }"
    >
      <td>{{ item.name }}</td>
      <td>{{ item.age }}</td>
    </tr>
  </table>
</template>

以上方法均可实现 Vue 表格的单选功能,选择哪种方式取决于项目需求和复杂度。对于简单场景,第一种方法足够;复杂交互建议使用自定义事件;使用组件库可以快速实现标准功能;大型应用推荐使用状态管理。

标签: 单选vue
分享给朋友:

相关文章

vue设计与实现下载

vue设计与实现下载

vue设计与实现电子书下载 《Vue.js设计与实现》是一本深入解析Vue.js框架原理的书籍,由霍春阳(HcySunYang)撰写。以下是获取该资源的常见途径: 正版购买渠道 京东、…

vue实现主题

vue实现主题

Vue 实现主题的方法 使用 CSS 变量动态切换主题 定义全局 CSS 变量,通过修改这些变量实现主题切换。在 Vue 的根组件或全局样式中定义变量: :root { --primary-co…

vue权限实现

vue权限实现

Vue 权限实现方案 基于路由的权限控制 通过路由守卫实现权限验证,在路由配置中添加meta字段标记权限要求。在router.beforeEach中检查用户权限与路由要求的匹配情况。 // rout…

vue实现全屏

vue实现全屏

Vue 实现全屏功能的方法 在 Vue 中实现全屏功能可以通过浏览器提供的 Fullscreen API 来实现。以下是几种常见的实现方式: 使用原生 Fullscreen API 通过调用 doc…

vue实现海报

vue实现海报

Vue 实现海报生成方案 使用 html2canvas 库 html2canvas 是一个将 HTML 元素转换为 Canvas 的库,适合将 Vue 组件渲染为海报图片 安装依赖: npm in…

tabbar 实现 vue

tabbar 实现 vue

在 Vue 中实现 TabBar 功能可以通过多种方式完成,以下是几种常见的方法: 使用 Vue Router 结合自定义组件 创建一个自定义的 TabBar 组件,结合 Vue Route…