layui动态表头的实现代码
前端  /  管理员 发布于 4年前   796
又get到一种思路,不光是layui。
外面这层table,就是用原生拼接的。
@Override public List
可以看到,一开始是有一个list,这个list是医院医生可以配置的科室,这些科室就是动态的。这样一来后台即可得到动态数据。同理,如果在layui时用到page,在这里返回成page类型即可。接下来再看js
function toList(begin,end,name){ console.log(begin); console.log(end); $.ajax({ url: basePath + "/biz/hospital/rate/allot/list.do", data: { begin:begin, end:end, name:name }, type : 'post', dataType : 'json', success : function(data) { var arrayPrice = new Array(); for(var i=0;i"+key+"(元)"; } if(i==(arrayPrice.length-1)&&(arrayPrice[arrayPrice.length-1][key]!=0)){ sumCols+=" "+arrayPrice[i][key]+" "; } if(i!=(arrayPrice.length-1)&&(arrayPrice[arrayPrice.length-1][key]!=0)){ partColsStr += ""+arrayPrice[i][key]+" "; } } partCols.push(partColsStr); } var sRenshu = 0; var stotal = 0; var tablex = ""; tablex += ""; if(data!=null && data.length>0){ for(var i=0;i 项目名称 检查人数 金额(元) "+title+"总计 "+sRenshu+" "+stotal+" "+sumCols+""; for(var i=0;i"+data[i].name+" " + "" + ""+ data[i].renshu+" " + ""+data[i].total+ partCols[i] + ""; } } } $("#table_status").empty(); $("#table_status").append(tablex); }, error : function() { layer.msg('系统异常,请联系管理员!',{icon:2,time:2000}); } }); } 可以在最上方图看到,有些字段是固定的,但是有些字段是动态的。思路是通过将调用接口返回出来的数据,动态的部分放到一个键值对数组下。这样一来,固定的部分,依然可以用返回的data得到,而动态的部分,用处理的数组循环赋值即可。动态表头就是动态数组的键。数据就是值。
function toList(begin,end,name){ console.log(begin); console.log(end); $.ajax({ url: basePath + "/biz/hospital/rate/allot/list.do", data: { begin:begin, end:end, name:name }, type : 'post', dataType : 'json', success : function(data) { var arrayPrice = new Array(); for(var i=0;i"+key+"(元)"; } if(i==(arrayPrice.length-1)&&(arrayPrice[arrayPrice.length-1][key]!=0)){ sumCols+=" "+arrayPrice[i][key]+" "; } if(i!=(arrayPrice.length-1)&&(arrayPrice[arrayPrice.length-1][key]!=0)){ partColsStr += ""+arrayPrice[i][key]+" "; } } partCols.push(partColsStr); } var sRenshu = 0; var stotal = 0; var tablex = ""; tablex += ""; if(data!=null && data.length>0){ for(var i=0;i 项目名称 检查人数 金额(元) "+title+"总计 "+sRenshu+" "+stotal+" "+sumCols+""; for(var i=0;i"+data[i].name+" " + "" + ""+ data[i].renshu+" " + ""+data[i].total+ partCols[i] + ""; } } } $("#table_status").empty(); $("#table_status").append(tablex); }, error : function() { layer.msg('系统异常,请联系管理员!',{icon:2,time:2000}); } }); } 这块是name这些的是固定的,就排除掉,然后将数据放到arrayPrice1下再push到数组下。tablex就是表格的html。当时拼接的是分两步,先是表头,然后是数据。有个总计,后来在sql下加了WITH ROLLUP就得到了。
for(var key in arrayPrice[i]){ if(i==0&&(arrayPrice[arrayPrice.length-1][key]!=0)){ title+=""+key+"(元) "; } if(i==(arrayPrice.length-1)&&(arrayPrice[arrayPrice.length-1][key]!=0)){ sumCols+=""+arrayPrice[i][key]+" "; } if(i!=(arrayPrice.length-1)&&(arrayPrice[arrayPrice.length-1][key]!=0)){ partColsStr += ""+arrayPrice[i][key]+" "; } }这块就是动态的数据,title表头,sumCols总计,partColsStr具体数据,加到tablex下就行。以上就是原生的思路。
这块table用到了layui,当时也是看着这个layui动态设置的思路去写的原生。主要思路是:cols是一个数组,通过ajax得到数据后放到数组下,再放到cols下即可。
$.ajax({ url: basePath + "/biz/hospital/rate/allot/department/getDepartment.do", data: { }, type : 'post', dataType : 'json', success : function(data) { mycols[0] = {field : 'nameTrue', title:"姓名", align:'center',width:'120'}; mycols[1] = {field : 'telephoneTrue', title:'支付手机号', align:'center',width:'120'}; mycols[2] = {field : 'orderNO', title:'订单号', align:'center',width:'120'}; mycols[3] = {field : 'createDate', title:'订单创建时间', align:'center',width:'120'}; mycols[4] = {field : 'modifyDate', title:'订单使用时间', align:'center',width:'120'}; mycols[5] = {field : 'price', title:'支付金额(元)', align:'center',width:'120'}; for (var i = 0;i < data.length; i++){ var obj = data[i].departmentName; if(obj!=0){ mycols[i+6] = {field : obj, title:obj+"(元)", align:'center',width:'120'}; } } console.log(mycols); reload(); }, error : function() { layer.msg('系统异常,请联系管理员!',{icon:2,time:2000}); }});以上是ajax调用后处理的数组。下面这些就是table。reload();就是重新渲染。
var options = { url: basePath + "/biz/hospital/rate/allot/list_mx.do", method: 'post', where:{ begin:$("#startDate").val().toString(), end:end, productId_:$("#productId_").val().toString(), orderNO:$("#qorderNO").val().toString(), name:$("#qname").val().toString() }, //分页请求参数 request:{ pageName: 'pageIndex', //页码 limitName: 'limit' //每页多少数据 }, //返回的数据格式 response:{ statusName: 'status', //数据状态的字段名称,默认:code statusCode: 200, //成功的状态码,默认:0 msgName: 'message', //状态信息的字段名称,默认:msg countName: 'total', //数据总数的字段名称,默认:count dataName: 'data' //数据列表的字段名称,默认:data }, //每页10条数据 limit: 10, //加载时出现加载条 loading: true, elem: '#data_table', cols: [ mycols ], id: 'dataTable', page: true, }; //方法级渲染 table.render(options);function reload(){ date = new Date($("#endDate").val()); date=date.setDate(date.getDate()+1); date=new Date(date); datemonth=date.getMonth()+1; //获取当前月份(0-11,0代表1月) end=date.getFullYear()+"-"+datemonth+"-"+date.getDate(); //options.where.departmentId = $("#departmentId").val(); options.where.begin = $("#startDate").val(); options.where.end = end; options.where.orderNO = $("#qorderNO").val();; options.where.name = $("#qname").val();; table.reload("dataTable",options); }可以看到
cols: [ mycols ],这个就是动态数据。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
您可能感兴趣的文章:
- 使用layui 渲染table数据表格的实例代码
- Layui事件监听的实现(表单和数据表格)
- 对layui数据表格动态cols(字段)动态变化详解
上一条:
ES6基础之字符串和函数的拓展详解
下一条:
node express使用HTML模板的方法示例Top
- 相关文章
- 使用 Alpine.js 排序插件对元素进行排序(0个评论)
- 在js中使用jszip + file-saver实现批量下载OSS文件功能示例(0个评论)
- 在vue中实现父页面按钮显示子组件中的el-dialog效果(0个评论)
- 使用mock-server实现模拟接口对接流程步骤(0个评论)
- vue项目打包程序实现把项目打包成一个exe可执行程序(0个评论)
- 近期文章
- 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
- 在go语言中使用github.com/signintech/gopdf实现生成pdf分页文件功能(0个评论)
- gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
- 欧盟关于强迫劳动的规定的官方举报渠道及官方举报网站(0个评论)
- 在go语言中使用github.com/signintech/gopdf实现生成pdf文件功能(0个评论)
- Laravel从Accel获得5700万美元A轮融资(0个评论)
- 在go + gin中gorm实现指定搜索/区间搜索分页列表功能接口实例(0个评论)
- 在go语言中实现IP/CIDR的ip和netmask互转及IP段形式互转及ip是否存在IP/CIDR(0个评论)
- PHP 8.4 Alpha 1现已发布!(0个评论)
- Laravel 11.15版本发布 - Eloquent Builder中添加的泛型(0个评论)
- 近期评论
122 在
学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..123 在
Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..原梓番博客 在
在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..博主 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..1111 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号
![]()