elementPuls 表格反选实现

真的在网上搜了很多资料发现根本实现不了反选 最下面有示例

然后去看了下官网

发现官网有教你怎么选中某个值的方法 官网中的"多选 "
官网地址

html 复制代码
<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" />
  </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>

既然官网有办法选中某个值

那反选是不是简单了 很多

以下是个人代码

第一步 获取已选中的值 和未选中的值

html 复制代码
<el-table
    ref="multipleTableRef"
    :data="tableData"
    style="width: 100%"
    绑定切换选中时候的数据
    @selection-change="handleSelectionChange"
  >
  
  </el-table>
  <div @click="invertSelection(state.noCheckList)"
      style="width: 114rem;height: 35rem;border: 1rem solid #9D9D9D;border-radius: 10rem;font-size: 15rem;color: #5C6170;display: flex;align-items: center;margin: 0 20rem;cursor: pointer;">
      <img style="height: 15rem;margin: 0 10rem 0 15rem;" src="../../../assets/选择-反向选择@2x.png"
          alt="" srcset="">
      <span>反向选择</span>
  </div>
<script setup>
let state = reactive({
		peopleList:[],//总数据
	    checkList: [],//选中数据数组
    	noCheckList:[],//未选中数据数组
})
// 重要 获取选中未选中数据
const handleSelectionChange = (val) => {
    console.log(val);
    state.checkList = val
    const notIncludedArray = state.peopleList.filter(item => !val.includes(item));
    state.noCheckList = notIncludedArray
    // console.log(notIncludedArray);
}
const multipleTableRef= ref() // 多选table
const invertSelection = (rows) => {
    // rows  需要选中的数据  直接传进 state.noCheckList 即可
    multipleTableRef.value.clearSelection()  //需要先全部取消选中  再去选中刚刚未选中的数据即可
    if (rows) {
    rows.forEach((row) => {
      multipleTableRef.value.toggleRowSelection(row, undefined)
    })
  } else {
    multipleTableRef.value.clearSelection()
  }
}

</script>
相关推荐
我爱学习_zwj1 天前
【鸿蒙进阶-7】鸿蒙与web混合开发
前端·华为·harmonyos
小谭鸡米花1 天前
高德地图电子围栏/地图选区/地图打点
前端·javascript·vue.js
摆烂工程师1 天前
什么是 ChatGPT Business 会员?与 ChatGPT Plus 有什么不同?
前端·后端·程序员
闲不住的李先森1 天前
使用Context7:告别AI“幻觉”,让编程助手永远生成最新、可用的代码
前端·ai编程·cursor
西瓜树枝1 天前
解决 JS 大整数精度丢失?一文读懂 BigInt 的底层逻辑与实战规则
前端·javascript
刺客_Andy1 天前
React 第四十六节 Router中useInRouterContext的使用详细介绍及注意事项
前端·javascript·react.js
刺客_Andy1 天前
React 第四十四节Router中 usefetcher的使用详解及注意事项
前端·javascript·react.js
该用户已不存在1 天前
我的Python工具箱,不用加班的秘密
前端·后端·python
刺客_Andy1 天前
React 第四十五节 Router 中 useHref() Hook的使用详解及注意事项
前端·javascript·react.js
好好好明天会更好1 天前
Vue2中页面数据响应的问题
前端·javascript·vue.js