当前位置:首页 > VUE

vue中表格实现单选

2026-01-22 13:11:14VUE

实现 Vue 表格单选功能

在 Vue 中实现表格的单选功能,可以通过以下方法完成。假设使用的是 Element UI 或其他类似的 UI 框架,以下是具体实现方式。

使用 Element UI 的表格单选

Element UI 的表格组件内置了单选功能,可以通过 highlight-current-row 属性开启。

<template>
  <el-table
    :data="tableData"
    highlight-current-row
    @current-change="handleCurrentChange"
  >
    <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: [
        { name: '张三', age: 20 },
        { name: '李四', age: 22 },
        { name: '王五', age: 25 }
      ],
      currentRow: null
    };
  },
  methods: {
    handleCurrentChange(val) {
      this.currentRow = val;
    }
  }
};
</script>

自定义单选逻辑

如果不使用 UI 框架,可以通过自定义逻辑实现表格单选。

<template>
  <table>
    <thead>
      <tr>
        <th>姓名</th>
        <th>年龄</th>
      </tr>
    </thead>
    <tbody>
      <tr
        v-for="(item, index) in tableData"
        :key="index"
        @click="selectRow(index)"
        :class="{ selected: selectedIndex === index }"
      >
        <td>{{ item.name }}</td>
        <td>{{ item.age }}</td>
      </tr>
    </tbody>
  </table>
</template>

<script>
export default {
  data() {
    return {
      tableData: [
        { name: '张三', age: 20 },
        { name: '李四', age: 22 },
        { name: '王五', age: 25 }
      ],
      selectedIndex: null
    };
  },
  methods: {
    selectRow(index) {
      this.selectedIndex = index;
    }
  }
};
</script>

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

使用 v-model 绑定选中项

通过 v-model 绑定选中项,可以更方便地管理选中状态。

<template>
  <el-table
    :data="tableData"
    highlight-current-row
    @current-change="handleCurrentChange"
    :current-row-key="currentRowKey"
  >
    <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: 20 },
        { id: 2, name: '李四', age: 22 },
        { id: 3, name: '王五', age: 25 }
      ],
      currentRowKey: null
    };
  },
  methods: {
    handleCurrentChange(val) {
      this.currentRowKey = val ? val.id : null;
    }
  }
};
</script>

动态切换选中行

如果需要动态切换选中行,可以通过调用表格的 setCurrentRow 方法实现。

<template>
  <el-table
    ref="singleTable"
    :data="tableData"
    highlight-current-row
    @current-change="handleCurrentChange"
  >
    <el-table-column prop="name" label="姓名"></el-table-column>
    <el-table-column prop="age" label="年龄"></el-table-column>
  </el-table>
  <el-button @click="setCurrent">选中第二行</el-button>
</template>

<script>
export default {
  data() {
    return {
      tableData: [
        { name: '张三', age: 20 },
        { name: '李四', age: 22 },
        { name: '王五', age: 25 }
      ]
    };
  },
  methods: {
    handleCurrentChange(val) {
      this.currentRow = val;
    },
    setCurrent() {
      this.$refs.singleTable.setCurrentRow(this.tableData[1]);
    }
  }
};
</script>

以上方法可以根据具体需求选择使用,灵活实现表格的单选功能。

vue中表格实现单选

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

相关文章

vue表格重置怎么实现

vue表格重置怎么实现

重置表格数据的方法 在Vue中重置表格数据通常需要操作绑定的数据源,以下是几种常见实现方式: 直接重置数据源 // 假设表格绑定的是tableData this.tableData = [...t…

vue   实现单选

vue 实现单选

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

vue实现单选按钮

vue实现单选按钮

使用 v-model 绑定单选按钮 在 Vue 中,可以通过 v-model 实现单选按钮的数据绑定。单选按钮组需要共享同一个 v-model 绑定的变量,并通过 value 属性区分选项。 &l…

使用vue实现表格

使用vue实现表格

使用 Vue 实现表格 在 Vue 中实现表格可以通过多种方式完成,以下是一个基于 Vue 3 的示例,展示如何动态渲染表格数据并支持基础功能(如排序、筛选)。 基础表格实现 通过 v-for 循…

vue实现单选

vue实现单选

Vue 实现单选按钮 在 Vue 中实现单选按钮通常使用 v-model 绑定数据,结合 <input type="radio"> 或第三方组件库(如 Element UI、Ant Des…

vue实现表格上传头像

vue实现表格上传头像

实现表格上传头像功能 在Vue中实现表格上传头像功能,可以通过以下步骤完成。这里假设使用Element UI作为UI框架,但方法同样适用于其他UI库或原生实现。 安装必要依赖 确保项目中已安装Ele…