SoFunction
Updated on 2025-04-04

elementPuls table anti-select implementation sample code

I really searched a lot of information online and found that I couldn't achieve the anti-selectThere is an example below
Then I went to check out the official website
I found that the official website has a method to teach you how to select a certain value. "Multiple choices
Official website address

<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">{{  }}</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) {
    ((row) => {
      // TODO: improvement typing when refactor table
      // eslint-disable-next-line @typescript-eslint/ban-ts-comment
      // @ts-expect-error
      !.toggleRowSelection(row, undefined)
    })
  } else {
    !.clearSelection()
  }
}
const handleSelectionChange = (val: User[]) => {
   = 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>

Since there is a way to select a value on the official website
Isn't it easy to choose?
The following is the personal code
Step 1 Get the selected value and the unselected value

&lt;el-table
    ref="multipleTableRef"
    :data="tableData"
    style="width: 100%"
    Bind toggle selected data
    @selection-change="handleSelectionChange"
  &gt;
  &lt;/el-table&gt;
  &lt;div @click="invertSelection()"
      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;"&gt;
      &lt;img style="height: 15rem;margin: 0 10rem 0 15rem;" src="../../../assets/Select - Reverse Select @"
          alt="" srcset=""&gt;
      &lt;span&gt;Reverse selection&lt;/span&gt;
  &lt;/div&gt;
&lt;script setup&gt;
let state = reactive({
		peopleList:[],//Total data	    checkList: [],//Select the data array    	noCheckList:[],//The data array is not selected})
// Important Get selected unselected dataconst handleSelectionChange = (val) =&gt; {
    (val);
     = val
    const notIncludedArray = (item =&gt; !(item));
     = notIncludedArray
    // (notIncludedArray);
}
const multipleTableRef= ref() // Multiple choice tableconst invertSelection = (rows) =&gt; {
    // rows requires selected data to be directly transferred to    ()  //You need to unselect all first and then select the data that was just not selected    if (rows) {
    ((row) =&gt; {
      (row, undefined)
    })
  } else {
    ()
  }
}
&lt;/script&gt;

This is the end of this article about the implementation of the elementPuls form anti-selecting. For more related elementPuls form anti-selecting content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!