vue+handsontable+elementui动态绑定点击弹框事件

vue+handsontable+elementui动态绑定点击弹框事件

前端小白最近的遇到一个新需求,用hansontable生成表格,表格中第一列单元格是图片,图片下方有一个更新按钮,点击更新按钮会弹出一个上传图片的对话框,我遇到的问题主要是卡在了更新按钮绑定点击事件不生效的问题这,下面是我的记录
参考链接: 链接: [https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/124154575]
写了个大概,记录下问题,又想深入了解的评论区联系,基本上每天都看
下面是图示
请添加图片描述
请添加图片描述

错误做法

在hansontable第一列定义一个renderer函数,在函数中插入innerhtml,此时并没有给更新图片绑定点击事件,而是使用添加事件监听的方式绑定的点击事件,在监听中修改是否弹框属性,这种做法最后的结果就是弹框无法关闭,而且弹框中引用的组件也显示不出来,试了很多次都没有结果(切记使用 ` 反单引号)

columns: [
            //第一列图片自定义函数,显示图片,添加更新图片按钮并绑定点击事件
            {
              renderer: (instance, td, row, col, prop, value, cellProperties)=> {
                var dialogVisible= true
                const escaped = Handsontable.helper.stringify(value);
                Handsontable.dom.empty(td);
                td.style.textAlign = 'center';//图片居中对齐
                td.innerHTML =`<div><p><img src=${escaped} style='width: 100px'></p>` +
                  `<el-button id=${row} type='primary' plain size='small'
                  style='position: center;border-bottom: 5px solid blue;color: #0064B4'   >更新图片</el-button></div>`;

                // document.getElementById(row).addEventListener("click", function() {
                //   // 具体执行代码
                   console.log("点击id为"+row+"按钮")
                   document.getElementsByName("")
                   var test=document.getElementById("test")
                   test.setAttribute("style","display: block ;")
                   console.log("sync"+test.getAttribute(":visible.sync"))
                   test.setAttribute(":visible.sync","true")
                 } , false);
                return td;
              },
            },{},{},{},{},{},{},{},{},
          ],

最终做法

1、在第一列只插入innerhtml,在更新图片button标签中绑定onclick事件,并写上函数名

columns: [
            //第一列图片自定义函数,显示图片,添加更新图片按钮并绑定点击事件
            {
              renderer: (instance, td, row, col, prop, value, cellProperties)=> {
                var dialogVisible= true
                const escaped = Handsontable.helper.stringify(value);
                Handsontable.dom.empty(td);
                td.style.textAlign = 'center';//图片居中对齐
                td.innerHTML =`<div><p><img src=${escaped} style='width: 100px'></p>` +
                  `<el-button id=${row} type='primary' plain size='small'
                  style='position: center;border-bottom: 5px solid blue;color: #0064B4' onclick="showUpload()"  >更新图片</el-button></div>`;
                return td;
              },
            },{},{},{},{},{},{},{},{},
          ],

2、在mounted函数中调用methods中的方法showUpload

mounted() {

      //生命周期注意点
      this.shelfupInstance = this.$refs.shelfupRef.hotInstance;
      //模板参数传参
      const _this = this
      //innerHtml中给更新按钮button绑定onclick事件,在此处进行调用
      window.showUpload=function (){
        _this.showUpload();
      }
    },

3、在showUpload方法中修改dialog弹框的:visible.sync属性值为true

 showUpload() {
        console.log("进来了")
        this.dialogVisible = true
      },
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值