vue按住shift键多选,按住ctrl取消选中的数据(以element框架的table为例)

一、如图所示

选中某一行,然后按下shift键,选中另一行,中间所有数据,包括原来选中的两行数据都会被选中,注意是选中行,而不是勾选复选框
在这里插入图片描述
选中某一行,然后按下ctrl键,选中另一行,中间所有数据,包括原来选中的两行数据都会被取消勾选,注意是选中行,而不是勾选复选框
在这里插入图片描述

二、具体代码如下

      <el-table
        v-loading="loading"
        :data="dataList"
        @selection-change="handleSelectionChange"
        ref="dataList"
        :row-style="rowStyle"
        :height="tableHeight"
        @row-click="handleRowClick"
        border
        :row-class-name="tableRowClassName"
      >
     // 以下变量定义在data里
      lastSelectedRowIndex: null, // 用于记录上次点击的行索引
      lastSelectedRow: null, // 上一次选中的行
      selectedRow: null, // 当前选中的行

    //  点击行选择数据
    handleRowClick(row, column, event) {
      this.lastSelectedRow = this.selectedRow; // 记录上一次选中的行
      this.selectedRow = row; // 记录当前选中的行
      this.lastSelectedRowIndex = this.dataList.indexOf(row);
      if (this.lastSelectedRow && event.shiftKey) {
        // 且按着 shift 键 则勾选这一行
        if (this.currentRow.length > 0) {
          // 如果按住Shift键并且之前有选中的行,则进行区域选择
          const currentIndex = this.dataList.indexOf(this.lastSelectedRow);
          const start = Math.min(currentIndex, this.lastSelectedRowIndex);
          const end = Math.max(currentIndex, this.lastSelectedRowIndex);
          for (let i = start; i <= end; i++) {
            this.$refs.dataList.toggleRowSelection(this.dataList[i], true);
          }
        }
      } else if (this.lastSelectedRow && event.ctrlKey) {
        if (this.currentRow.length > 0) {
          // 如果按住ctrl键并且之前有选中的行,则进行区域选择取消
          const currentIndex = this.dataList.indexOf(this.lastSelectedRow);
          const start = Math.min(currentIndex, this.lastSelectedRowIndex);
          const end = Math.max(currentIndex, this.lastSelectedRowIndex);
          for (let i = start; i <= end; i++) {
            this.$refs.dataList.toggleRowSelection(this.dataList[i], false);
          }
        }
      } else {
        this.$refs.dataList.toggleRowSelection(row);
      }
    },
  • 9
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue 3 + TypeScript中,你可以使用事件监听和状态管理来实现按住Shift选中多条数据的功能。以下是一个简单的示代码: ```html <template> <div> <div> <!-- 绑定 shift 的按下和抬起事件 --> <div @keydown.shift="handleShiftKeyDown" @keyup.shift="handleShiftKeyUp"> <div v-for="item in items" :key="item.id" :class="{ selected: item.selected }"> {{ item.name }} </div> </div> </div> </div> </template> <script lang="ts"> import { defineComponent, reactive } from 'vue'; export default defineComponent({ data() { return { items: [ { id: 1, name: 'Item 1', selected: false }, { id: 2, name: 'Item 2', selected: false }, { id: 3, name: 'Item 3', selected: false }, // 添加更多条目... ], lastSelectedIndex: -1, }; }, methods: { handleShiftKeyDown(event: KeyboardEvent) { // 按下 Shift 时,将 lastSelectedIndex 设置为当前选中的条目索引 const selectedItem = this.items.find(item => item.selected); this.lastSelectedIndex = selectedItem ? this.items.indexOf(selectedItem) : -1; }, handleShiftKeyUp(event: KeyboardEvent) { // 抬起 Shift 时,根据 lastSelectedIndex 和当前选中的条目索引,选中范围内的条目 const selectedIndex = this.items.findIndex(item => item.selected); if (selectedIndex !== -1 && this.lastSelectedIndex !== -1) { const start = Math.min(selectedIndex, this.lastSelectedIndex); const end = Math.max(selectedIndex, this.lastSelectedIndex); for (let i = start; i <= end; i++) { this.items[i].selected = true; } } }, }, }); </script> <style scoped> .selected { background-color: yellow; } </style> ``` 在上面的示中,我们使用了`@keydown.shift`和`@keyup.shift`来监听Shift的按下和抬起事件。当Shift按下时,我们记录下当前选中的条目索引为`lastSelectedIndex`。当Shift抬起时,根据`lastSelectedIndex`和当前选中的条目索引,选中范围内的条目。 注意:在示代码中,我们使用了Vue 3的Composition API中的`defineComponent`和`reactive`函数,以及TypeScript的类型注解来定义组件和方法的类型。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值