当前位置:首页 > VUE

vue前端table实现单选

2026-02-23 04:58:32VUE

实现 Vue 表格单选功能

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

使用 v-model 和计算属性

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

vue前端table实现单选

<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 等组件库,可以直接使用其提供的单选表格功能。

vue前端table实现单选

以 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 集中管理选中状态。

// 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实现mvc

vue实现mvc

Vue 实现 MVC 模式 Vue 本身是一个 MVVM(Model-View-ViewModel)框架,但可以通过结构调整实现 MVC(Model-View-Controller)模式。以下是具体实…

vue实现div

vue实现div

Vue 实现 DIV 元素的方法 在 Vue 中实现 DIV 元素可以通过多种方式,以下是几种常见的实现方法: 使用模板语法 在 Vue 的模板中直接使用 HTML 的 <div> 标签…

vue 实现列表

vue 实现列表

Vue 实现列表的方法 在 Vue 中实现列表通常使用 v-for 指令,结合数组或对象数据渲染动态列表。以下是几种常见的实现方式: 基础列表渲染 使用 v-for 指令遍历数组,渲染列表项。it…

vue实现返回

vue实现返回

返回按钮功能实现 在Vue中实现返回功能通常有两种方式:使用浏览器历史记录的history.back()或结合Vue Router进行编程式导航。以下是具体实现方法: 使用浏览器API met…

vue实现定位

vue实现定位

Vue 中实现定位的方法 在 Vue 中实现定位可以通过以下几种方式: 使用 CSS 定位 通过 CSS 的 position 属性实现定位,Vue 中可以直接在组件的 <style>…

vue滚动实现

vue滚动实现

Vue 滚动实现方法 使用原生滚动 在 Vue 中可以通过 ref 获取 DOM 元素,调用原生滚动方法实现滚动效果。适用于简单场景。 <template> <div ref=…