Vue3 使用Element Plus表格单选带checkbox

官方地址:添加链接描述

官方给出的多选带checkbox,单选直接选中当前行高亮,有时候不想要单行高亮,想要带checkbox的单选,需要对多选进行改造

官方给的多选例子:

复制代码
<template>
  <el-table
    ref="multipleTableRef"
    :data="tableData"
    style="width: 100%"
    @selection-change="handleSelectionChange"
  >
    <el-table-column type="selection" width="55" />
    <el-table-column label="Date" width="120">
      <template #default="scope">{{ scope.row.date }}</template>
    </el-table-column>
    <el-table-column property="name" label="Name" width="120" />
    <el-table-column property="address" label="Address" show-overflow-tooltip />
  </el-table>
 <!-- <div style="margin-top: 20px">
    <el-button @click="toggleSelection([tableData[1], tableData[2]])"
      >Toggle selection status of second and third rows</el-button
    >
    <el-button @click="toggleSelection()">Clear selection</el-button>
  </div> -->
</template>

<script lang="ts" setup>
import { ref } from 'vue'
import { ElTable } from 'element-plus'

interface User {
  date: string
  name: string
  address: string
}

const multipleTableRef = ref<InstanceType<typeof ElTable>>()
const multipleSelection = ref<User[]>([])
const toggleSelection = (rows?: User[]) => {
  if (rows) {
    rows.forEach((row) => {
      // TODO: improvement typing when refactor table
      // eslint-disable-next-line @typescript-eslint/ban-ts-comment
      // @ts-expect-error
      multipleTableRef.value!.toggleRowSelection(row, undefined)
    })
  } else {
    multipleTableRef.value!.clearSelection()
  }
}
const `handleSelectionChange` = (val: User[]) => {
  multipleSelection.value = val
}

const tableData: User[] = [
  {
    date: '2016-05-03',
    name: 'Tom',
    address: 'No. 189, Grove St, Los Angeles',
  },
  {
    date: '2016-05-02',
    name: 'Tom',
    address: 'No. 189, Grove St, Los Angeles',
  },
  {
    date: '2016-05-04',
    name: 'Tom',
    address: 'No. 189, Grove St, Los Angeles',
  },
  {
    date: '2016-05-01',
    name: 'Tom',
    address: 'No. 189, Grove St, Los Angeles',
  },
  {
    date: '2016-05-08',
    name: 'Tom',
    address: 'No. 189, Grove St, Los Angeles',
  },
  {
    date: '2016-05-06',
    name: 'Tom',
    address: 'No. 189, Grove St, Los Angeles',
  },
  {
    date: '2016-05-07',
    name: 'Tom',
    address: 'No. 189, Grove St, Los Angeles',
  },
]
</script>

只需要在事件handleSelectionChange中添加两行代码

复制代码
const handleSelectionChange = (val: User[]) => {
  multipleSelection.value = val
  if(val.length > 1){
	  multipleTableRef.value.clearSelection()
	  multipleTableRef.value.toggleRowSelection(val.pop())
  }
}
相关推荐
Go 言 Go 语1 天前
Claude Code 核心加载机制详解
服务器·前端·数据库
朝阳391 天前
CSS【详解】给子元素添加间距的最佳实践(含space 和 gap 的区别图解和面试的标准答案)
前端·css
s6516654961 天前
Makefile语法学习
java·linux·前端
悟空爬虫-彪哥1 天前
Stich接入Codex教程
java·前端·数据库
深海鱼在掘金1 天前
Next.js从入门到实战保姆级教程(第十五章):部署运维与 CI/CD
前端·ci/cd·next.js
Mr.mjw1 天前
vue中封装一个进度条组件,无需引入,纯css
javascript·css·vue.js
veminhe1 天前
Java后端、PC前端学习备忘
前端
深海鱼在掘金1 天前
Next.js从入门到实战保姆级教程(第十七章):综合实战项目(下)——前端页面、性能优化与部署
前端·ci/cd·next.js
深海鱼在掘金1 天前
Next.js从入门到实战保姆级教程(第十六章):实战项目(上)——全栈博客系统架构与核心功能
前端·数据库·next.js
茅盾体1 天前
Electron图标相关
java·前端·electron