ElementUI表格table组件实现单选及禁用默认选中效果

在使用ElementUI,需要ElementUI表格table组件实现单选及禁用默认选中效果,
先看下效果图:
在这里插入图片描述
代码如下:

<template>
  <el-table
    ref="multipleTable"
    :data="tableData"
    tooltip-effect="dark"
    style="width: 100%"
    @row-click="rowClick" @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>
  
</template>

<script>
  export default {
    data() {
      return {
	    selected:'',//选中的名字
        tableData: [{
          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: {
      //点击行
	 rowClick(row, column, event)
	 {
	     this.$refs.multipleTable.toggleRowSelection(row);
	 },
	 //改变时
	 handleSelectionChange(val)
	 {
	     if (val.length > 1)
	     {
	         this.$refs.multipleTable.clearSelection()
	         this.$refs.multipleTable.toggleRowSelection(val.pop())
	         console.log("handleSelectionChange1:",val)
	     }
	     else
	     {
	         if(val.length==1)
	         this.selected = val[0].name
	     }
	     if(val.length==0)
         this.selected = '';
	 }
 
    }
  }
</script>

相关推荐

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-04-01 00:28:02       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-04-01 00:28:02       106 阅读
  3. 在Django里面运行非项目文件

    2024-04-01 00:28:02       87 阅读
  4. Python语言-面向对象

    2024-04-01 00:28:02       96 阅读

热门阅读

  1. 【技术】Vue3 组件通讯方法有哪些

    2024-04-01 00:28:02       35 阅读
  2. python之函数的参数

    2024-04-01 00:28:02       43 阅读
  3. Git 实战教程

    2024-04-01 00:28:02       45 阅读
  4. FFmpeg入门指南

    2024-04-01 00:28:02       37 阅读
  5. Web软件测试面试总结

    2024-04-01 00:28:02       41 阅读
  6. [小程序开发] 设置request封装请求参数

    2024-04-01 00:28:02       43 阅读
  7. 每日一题 --- 四数之和[力扣][Go]

    2024-04-01 00:28:02       47 阅读
  8. 解锁背包问题:C++实现指南

    2024-04-01 00:28:02       48 阅读
  9. 每日一题 第五十七期 洛谷 统计子矩阵

    2024-04-01 00:28:02       39 阅读