Handsontable添加操作按钮

需求: 要求表格第一列添加删除按钮,点击按钮删除该行数据。并且前三列固定在左侧不随着滚动条滚动,前三列内容不能编辑。在这里插入图片描述
所用到的语法:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

<template>
  <div id="app">
    <hot-table ref="hotOfGoal" id="hot"  :settings="goalHotSettings"></hot-table>
  </div>
</template>

步骤一: 固定左侧前三列不能水平滚动,设置属性 fixedColumnsLeft ,如果是固定顶部多少行不能垂直滚动,则设置属性 fixedRowsTop ,设置左侧前三列的 readOnly 属性,则通过设置 cells 来实现。

data() {
	return {
		goalHotSettings: {
			//...
			//...
			//...
			fixedRowsTop: 0,  // 固定顶部多少行不能垂直滚动
        	fixedColumnsLeft: 3, // 固定左侧多少列不能水平滚动
        	cells: function(row, column, prop) { // 动态设置一些单元格的属性
		          const cellProperties = {readOnly: false, className: ''};  // 初始化属性和类名
		          const visualRowIndex = this.instance.toVisualRow(row);  // 获取行号
		          const visualColIndex = this.instance.toVisualColumn(column);  // 获取列号
		          if(visualColIndex === 0 || visualColIndex === 1 || visualColIndex === 2) {  // 当列号为0,1,2时
		            cellProperties.readOnly = true;  // 设置readOnly属性为true
		            cellProperties.className = 'forbidden';  // 设置类名为forbidden
		            if(visualColIndex === 0) {
		                cellProperties.className = 'forbidden linkhoverStyle';  // 特殊处理按钮的样式
		            }
		          } else {
		              cellProperties.className = 'myCellStyle';
		          }
		          return cellProperties;  // 返回
		    }
		}
	}
}

样式

   .forbidden {
     background-color: #f7f7f7;
     vertical-align: middle;
     color: #626266;
   }
   .linkhoverStyle {
       color: #397cf6;
       text-align: center;
       vertical-align: middle;
   }
   .myCellStyle {
       text-align: left;
       vertical-align: middle;
       color: #333;
   }

步骤二: 为删除按钮添加点击事件

mounted() {
	this.$nextTick(() => {
		this.$refs['hotOfGoal'].hotInstance.addHook('afterOnCellMouseDown', (event, coords, td) => {
            this.callBackMD(event, coords, td, 'hotOfGoal')  // 如果页面不止一个表格,则需要传入表格对应的ref,以区分每个表格
        });
	})
},
methods: {
	// 注册handsontable单击事件的回调函数,第一列单元格点击后询问是否删除,确认删除
    callBackMD(event, coords, td, ref) {
        var row = coords.row;  // 获取行号和列号
	    var col = coords.col;
        if(col == 0 && row != -1) {  // 第一列除了表头都注册点击事件
            const rowOfGoal = this.$refs[ref].hotInstance.getSourceDataAtRow(row);  // 获取对应行的所有数据
            this.$Modal.confirm({
                title: '确定取消?',
                content: '<p>组织下所填写的信息将一并删除</p>',
                onOk: () => {
                    this.deleteRow(ref, row);  // 删除该行数据
                    // 重新载入表格
                    if(this.$refs[ref]) {
                        if(ref === 'hotOfGoal') {
                            this.$refs[ref].hotInstance.loadData(this.goalHotSettings.data);
                        } else {
                            // this.$refs[ref].hotInstance.loadData(this.completedHotSettings.data);
                        }
                    }
                }
            })
        }
    },
    // 删除行
    deleteRow(ref, index) {
        if(this.$refs[ref]) {
            this.$refs[ref].hotInstance.alter('remove_row', index, 1);
        }
    }
}
  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值