element 表格能做到ctrl+鼠标左键单击表格的某一行么?

光明的翅膀
  • 28

我想用element table实现点击鼠标左键单选,点击ctrl+鼠标左键多选,能做到么??
鼠标左键单选我会做,主要就是ctrl+鼠标左键不会

回复
阅读 3.2k
3 个回答
✓ 已被采纳

你好,你要的效果实现了,看看,有什么疑问可以直接提出或者私信。

<template>
  <div>
        <el-table
    ref="multipleTable"
    :data="tableData3"
    tooltip-effect="dark"
    style="width: 100%"
    @click.native="click"
    @selection-change="handleSelectionChange">
    <el-table-column
      type="selection"
      width="55">
    </el-table-column>
    <el-table-column
      label="日期"
      width="120">
      <template slot-scope="scope">{{ scope.row.date }}</template>
    </el-table-column>
    <el-table-column
      prop="name"
      label="姓名"
      width="120">
    </el-table-column>
    <el-table-column
      prop="address"
      label="地址"
      show-overflow-tooltip>
    </el-table-column>
  </el-table>
  <div style="margin-top: 20px">
    <el-button @click="toggleSelection([tableData3[1], tableData3[2]])">切换第二、第三行的选中状态</el-button>
    <el-button @click="toggleSelection()">取消选择</el-button>
  </div>
  </div>
</template>

<script>
  export default {
   data() {
      return {
        tableData3: [{
          date: '2016-05-03',
          name: '王小虎',
          address: '上海市普陀区金沙江路 1518 弄'
        }, {
          date: '2016-05-02',
          name: '王小虎',
          address: '上海市普陀区金沙江路 1518 弄'
        }, {
          date: '2016-05-04',
          name: '王小虎',
          address: '上海市普陀区金沙江路 1518 弄'
        }, {
          date: '2016-05-01',
          name: '王小虎',
          address: '上海市普陀区金沙江路 1518 弄'
        }, {
          date: '2016-05-08',
          name: '王小虎',
          address: '上海市普陀区金沙江路 1518 弄'
        }, {
          date: '2016-05-06',
          name: '王小虎',
          address: '上海市普陀区金沙江路 1518 弄'
        }, {
          date: '2016-05-07',
          name: '王小虎',
          address: '上海市普陀区金沙江路 1518 弄'
        }],
        multipleSelection: []
      }
    },

    methods: {
      toggleSelection(rows) {
        if (rows) {
          rows.forEach(row => {
            this.$refs.multipleTable.toggleRowSelection(row);
          });
        } else {
          this.$refs.multipleTable.clearSelection();
        }
      },
      handleSelectionChange(val) {
        this.multipleSelection = val;
      },
      click(e){
              e.preventDefault()
              console.log(this.isCtrl)
          if(e.button === 0 && this.isCtrl){
              this.$refs.multipleTable.toggleAllSelection()
          }
      }
    },
    mounted(){
        var sUserAgent = navigator.userAgent;
        var isWin = (navigator.platform == "Win32") || (navigator.platform == "Windows");
        var isMac = (navigator.platform == "Mac68K") || (navigator.platform == "MacPPC") || (navigator.platform == "Macintosh") || (navigator.platform == "MacIntel");
        document.addEventListener('keydown', e => {
            if((e.keyCode === 17 && isWin) || (e.keyCode === 91 && isMac)){
                this.isCtrl = true
            }
        })
        document.addEventListener('keyup', e => {
            if((e.keyCode === 17 && isWin) || (e.keyCode === 91 && isMac)){
                this.isCtrl = false
            }
        })
        
    }
  }
</script>

我判断了下用户操作系统,如果是window就是ctrl + 鼠标左键,如果是macOS,就是command + 鼠标左键

文档里有单选和多选啊Table 表格 自己实现也可以 自己实现也可以,监听onkeydown事件

shoyuf
  • 649
onkeydown="keyListener(event);"
function keyListener(event){
  if (event.ctrlKey){
    alert('你按下了CTRL');
  }
}
宣传栏