当前位置: 首页 > news >正文

layui的treeTable组件,多层级上传按钮失效的问题解决

现象描述:

layui的treeTable 的上传按钮在一层能用,展开后其他按钮正常点击,上传按钮无效。

具体原因没有深究,大概率是展开的子菜单没有被渲染treeTable的done管理到,导致没有重绘上传按钮。

解决方案:

不使用layu的上传组件方法,按照传统文件上传来,写一个隐藏的input框,每次触发上传事件的时候,就是触发input框的点击事件,具体代码如下:

html:

<div class="user-main user-collasped"><div class="layui-card"><div class="layui-card-body"><table id="file-table" lay-filter="file-table"></table><input type="file" id="fileInput" style="display: none;" /></div></div>
</div>

渲染操作按钮:

// 表格栏let cols = [[{ title: '文件名称', field: 'title' },{title: '类型', field: 'type', templet: function (d) {return d.type === 'dir' ? '目录' : '文件'}},{ title: '路径', field: 'path' },{title: '操作', align: 'center', width: 300, templet: function (d) {let html = '';if (d.type === 'dir') {html += '<button class="layui-btn layui-btn-xs" lay-event="addDir" title="新增目录"><i class="pear-icon pear-icon-add"></i></button>';html += '<button class="layui-btn layui-btn-xs layui-bg-blue" lay-event="upload" title="上传文件" style="margin-left: 5px;"><i class="pear-icon pear-icon-upload"></i></button>';html += '<button class="layui-btn layui-btn-xs layui-bg-red" lay-event="remove" title="删除" style="margin-left: 5px;"><i class="pear-icon pear-icon-ashbin"></i></button>';} else {html += '<button class="layui-btn layui-btn-primary layui-border layui-btn-xs" lay-event="download" title="下载"><i class="pear-icon pear-icon-download"></i></button>';html += '<button class="layui-btn layui-btn-xs layui-bg-red" lay-event="remove" title="删除" style="margin-left: 5px;"><i class="pear-icon pear-icon-ashbin"></i></button>';}return html;}}]]

表格操事件绑定:

// 全局变量let selectPath = null; //需要上传的父路径
// 绑定表格每行的操作按钮treeTable.on('tool(file-table)', function (obj) {if (obj.event === 'addDir') {addDir(obj.data);} else if (obj.event === 'upload') {selectPath = obj.data.path // selectPath全局变量$('#fileInput').click();} else if (obj.event === 'download') {// 下载文件downloadFile(obj.data);} else if (obj.event === 'remove') {// 删除文件removeFile(obj.data);}})

给input绑定点击事件:

// 绑定上传事件function bindUploadClick() {$('#fileInput').on('change', function () {var file = $('#fileInput')[0].files[0]; // 获取文件if (file) {// 创建FormData对象var formData = new FormData();formData.append('file', file);formData.append('folder_path', selectPath && selectPath.split('\\').slice(1).join('\\') || '')// 使用$.ajax上传文件$.ajax({url: MODULE_PATH + '/uploadFile',type: 'POST',data: formData,processData: false, // 不处理发送的数据contentType: false, // 不设置内容类型success: function (res) {// 清空选中的文件夹selectPath = null;if (res.success) {getData(); // 刷新treeTablelayer.msg(res.msg, { icon: 1 })} else {layer.msg(res.msg, { icon: 2 })}},error: function () {layer.msg('文件上传失败', { icon: 2 });}});}});}bindUploadClick();

如果有更好的解决方式,麻烦私信一下我,hahahaha

http://www.lryc.cn/news/348074.html

相关文章:

  • HashMap在JDK1.8的优化
  • Kotlin标准函数和静态方法
  • RabbitMQ(四种使用模式)
  • 【UE5 C++】基础学习笔记——01 UObject的创建与使用
  • TCP及IP协议
  • 运筹系列92:vrp算法包VROOM
  • 【Spring Security注解详解】
  • C++学习笔记3
  • 基于SpringBoot的酒店(预约)客房管理系统的设计与实现+毕业论文
  • Rust 中的声明可见性
  • 让 计算机 将 数学 公式 表达式 的计算过程绘制出来 【mathematical-expression(MAE)】
  • Django——中间件
  • 景联文科技:用高质量数据采集标注赋能无人机技术,引领无人机迈入新纪元!
  • SpringBoot集成Redis,使用RedisTemple存储对象使用纯JSON格式
  • [muduo网络库]——muduo库的Reactor模型(剖析muduo网络库核心部分、设计思想)
  • vue中怎样清除computed的缓存
  • 代码大师的工具箱:现代软件开发利器
  • 整理好了!2024年最常见 100 道 Java基础面试题(四十三)
  • 【TypeScript模块简介以及使用方法】
  • Offer必备算法38_贪心算法四_八道力扣题详解(由易到难)
  • java8 转对象,Java8转Map,Java8转Llist
  • 【Pytest官方文档翻译及学习】2.1 如何调用pytest
  • RabbitMQ的用途
  • R语言软件安装及配置
  • 网络配置的加密存储
  • 你写代码,会关注时间复杂度吗?
  • 【连连国际注册/登录安全分析报告】
  • linux进阶高级配置,你需要知道的有哪些(10)-远程访问
  • 不显示 表格 style=“display: none;“ 这个默认是不显示的
  • Bittensor怎么挖?手把手教你,使用bitget钱包