layui动态表头的实现代码

又get到一种思路,不光是layui。

layui动态表头的实现代码

外面这层table,就是用原生拼接的。

@Override public List<Map<String, Object>> distribution(String begin,String end,String name,String hospitalCode) { HashMap<String, Object> params = new HashMap<String, Object>(); StringBuffer buf = new StringBuffer(); List<HRateAllotDepartment> hRateAllotDepartment = rateAllotDepartmentDao.getRateDepartment(hospitalCode); String str1 = ""; String str2 = ""; for(int i=0;i<hRateAllotDepartment.size();i++) { str1 +=",sum(`"+hRateAllotDepartment.get(i).getDepartmentName()+"`)`"+hRateAllotDepartment.get(i).getDepartmentName()+"`"; str2 +=",max(case when e.`name`='"+hRateAllotDepartment.get(i).getDepartmentName()+"' then f.price else 0 end) `"+hRateAllotDepartment.get(i).getDepartmentName()+"`"; } buf.append("select d.`name`,d.productId_,count(*) renshu,sum(price) total"+str1+" "); buf.append(" from ( SELECT b.`name`,b.id productId_ ,d.id,max(b.price) price "); buf.append(" "+str2+" FROM "); buf.append(" dt_pay_health_order_product a " + "JOIN dt_pay_health_order d ON a.orderId = d.id " + "JOIN dt_pay_health_product b ON a.productId = b.id "); buf.append(" JOIN dt_hospital_health_item c ON b.bizId = c.id " + "JOIN dt_hospital_health_order_use g ON g.orderProductId = a.id " + "JOIN dt_hospital_rate_allot f ON b.id = f.productId "); buf.append(" JOIN dt_hospital_department e ON f.departmentId = e.id where g.createDate>=:begin and g.createDate<=:end and b.name like :name and a.state ='02' group by b.`id`,d.`id` ) d group by d.productId_ WITH ROLLUP "); if(begin == null || begin.length() == 0){ begin = "1970-01-01"; } if(end == null || end.length() == 0){ end = "2099-01-01"; } params.put("begin",begin); params.put("end",end); params.put("name","%"+name+"%"); return this.getMapListByMap(buf.toString(), params); }

可以看到,一开始是有一个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<data.length;i++){ var arrayPrice1 = new Array(); for(var key in data[i]){ if(key!="name"&&key!="productId_"&&key!="renshu"&&key!="total"){ arrayPrice1[key]=data[i][key]; } } arrayPrice.push(arrayPrice1); } var title=""; var sumCols=""; var partCols=new Array(); for(var i=0;i<arrayPrice.length;i++){ var partColsStr = ""; for(var key in arrayPrice[i]){ if(i==0&&(arrayPrice[arrayPrice.length-1][key]!=0)){ title+="<th>"+key+"(元)</th>"; } if(i==(arrayPrice.length-1)&&(arrayPrice[arrayPrice.length-1][key]!=0)){ sumCols+="<td>"+arrayPrice[i][key]+"</td>"; } if(i!=(arrayPrice.length-1)&&(arrayPrice[arrayPrice.length-1][key]!=0)){ partColsStr += "<td>"+arrayPrice[i][key]+"</td>"; } } partCols.push(partColsStr); } var sRenshu = 0; var stotal = 0; var tablex = ""; tablex += "<tr><th>项目名称</th><th>检查人数</th><th>金额(元)</th>"+title+"</tr>"; if(data!=null && data.length>0){ for(var i=0;i<data.length-1;i++){ sRenshu+=data[i].renshu; stotal+=data[i].total; } tablex += "<tr><td>总计</td><td>"+sRenshu+"</td><td>"+stotal+"</td>"+sumCols+"</tr>"; for(var i=0;i<data.length-1;i++){ { tablex += "<tr><td>"+data[i].name+"</td>" + "<td>" + "<a href="+basePath +" /biz/hospital/rate/allot/toPageMx.do?startDate="+$("#startDate").val().toString() +"&endDate="+$("#endDate").val().toString() +"&productId_="+data[i].productId_+">"+ data[i].renshu+"</a></td>" + "<td>"+data[i].total+ partCols[i] + "</tr>"; } } } $("#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<data.length;i++){ var arrayPrice1 = new Array(); for(var key in data[i]){ if(key!="name"&&key!="productId_"&&key!="renshu"&&key!="total"){ arrayPrice1[key]=data[i][key]; } } arrayPrice.push(arrayPrice1); } var title=""; var sumCols=""; var partCols=new Array(); for(var i=0;i<arrayPrice.length;i++){ var partColsStr = ""; for(var key in arrayPrice[i]){ if(i==0&&(arrayPrice[arrayPrice.length-1][key]!=0)){ title+="<th>"+key+"(元)</th>"; } if(i==(arrayPrice.length-1)&&(arrayPrice[arrayPrice.length-1][key]!=0)){ sumCols+="<td>"+arrayPrice[i][key]+"</td>"; } if(i!=(arrayPrice.length-1)&&(arrayPrice[arrayPrice.length-1][key]!=0)){ partColsStr += "<td>"+arrayPrice[i][key]+"</td>"; } } partCols.push(partColsStr); } var sRenshu = 0; var stotal = 0; var tablex = ""; tablex += "<tr><th>项目名称</th><th>检查人数</th><th>金额(元)</th>"+title+"</tr>"; if(data!=null && data.length>0){ for(var i=0;i<data.length-1;i++){ sRenshu+=data[i].renshu; stotal+=data[i].total; } tablex += "<tr><td>总计</td><td>"+sRenshu+"</td><td>"+stotal+"</td>"+sumCols+"</tr>"; for(var i=0;i<data.length-1;i++){ { tablex += "<tr><td>"+data[i].name+"</td>" + "<td>" + "<a href="+basePath +" /biz/hospital/rate/allot/toPageMx.do?startDate="+$("#startDate").val().toString() +"&endDate="+$("#endDate").val().toString() +"&productId_="+data[i].productId_+">"+ data[i].renshu+"</a></td>" + "<td>"+data[i].total+ partCols[i] + "</tr>"; } } } $("#table_status").empty(); $("#table_status").append(tablex); }, error : function() { layer.msg('系统异常,请联系管理员!',{icon:2,time:2000}); } }); }

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/6f98a9d0ce613c1ce99b20771eb32173.html