layui動(dòng)態(tài)表頭的實(shí)現(xiàn)代碼
又get到一種思路,不光是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);
}
可以看到,一開始是有一個(gè)list,這個(gè)list是醫(yī)院醫(yī)生可以配置的科室,這些科室就是動(dòng)態(tài)的。這樣一來后臺(tái)即可得到動(dòng)態(tài)數(shù)據(jù)。同理,如果在layui時(shí)用到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 class='firstTh'>"+key+"(元)</th>";
}
if(i==(arrayPrice.length-1)&&(arrayPrice[arrayPrice.length-1][key]!=0)){
sumCols+="<td class='secondTd'>"+arrayPrice[i][key]+"</td>";
}
if(i!=(arrayPrice.length-1)&&(arrayPrice[arrayPrice.length-1][key]!=0)){
partColsStr += "<td class='thirdTd'>"+arrayPrice[i][key]+"</td>";
}
}
partCols.push(partColsStr);
}
var sRenshu = 0;
var stotal = 0;
var tablex = "";
tablex += "<tr class='firstTr'><th class='firstTh'>項(xiàng)目名稱</th><th class='firstTh'>檢查人數(shù)</th><th class='firstTh'>金額(元)</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 class='secondTr'><td class='secondTd'>總計(jì)</td><td class='secondTd'>"+sRenshu+"</td><td class='secondTd'>"+stotal+"</td>"+sumCols+"</tr>";
for(var i=0;i<data.length-1;i++){
{
tablex += "<tr class='thirdTr'><td class='thirdTd'>"+data[i].name+"</td>" +
"<td class='thirdTd'>" +
"<a style='color:#ff5722' href="+basePath +" rel="external nofollow" rel="external nofollow" /biz/hospital/rate/allot/toPageMx.do?startDate="+$("#startDate").val().toString()
+"&endDate="+$("#endDate").val().toString()
+"&productId_="+data[i].productId_+">"+
data[i].renshu+"</a></td>" +
"<td class='thirdTd'>"+data[i].total+ partCols[i] +
"</tr>";
}
}
}
$("#table_status").empty();
$("#table_status").append(tablex);
},
error : function() {
layer.msg('系統(tǒng)異常,請(qǐng)聯(lián)系管理員!',{icon:2,time:2000});
}
});
}
可以在最上方圖看到,有些字段是固定的,但是有些字段是動(dòng)態(tài)的。思路是通過將調(diào)用接口返回出來的數(shù)據(jù),動(dòng)態(tài)的部分放到一個(gè)鍵值對(duì)數(shù)組下。這樣一來,固定的部分,依然可以用返回的data得到,而動(dòng)態(tài)的部分,用處理的數(shù)組循環(huán)賦值即可。動(dòng)態(tài)表頭就是動(dòng)態(tài)數(shù)組的鍵。數(shù)據(jù)就是值。
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 class='firstTh'>"+key+"(元)</th>";
}
if(i==(arrayPrice.length-1)&&(arrayPrice[arrayPrice.length-1][key]!=0)){
sumCols+="<td class='secondTd'>"+arrayPrice[i][key]+"</td>";
}
if(i!=(arrayPrice.length-1)&&(arrayPrice[arrayPrice.length-1][key]!=0)){
partColsStr += "<td class='thirdTd'>"+arrayPrice[i][key]+"</td>";
}
}
partCols.push(partColsStr);
}
var sRenshu = 0;
var stotal = 0;
var tablex = "";
tablex += "<tr class='firstTr'><th class='firstTh'>項(xiàng)目名稱</th><th class='firstTh'>檢查人數(shù)</th><th class='firstTh'>金額(元)</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 class='secondTr'><td class='secondTd'>總計(jì)</td><td class='secondTd'>"+sRenshu+"</td><td class='secondTd'>"+stotal+"</td>"+sumCols+"</tr>";
for(var i=0;i<data.length-1;i++){
{
tablex += "<tr class='thirdTr'><td class='thirdTd'>"+data[i].name+"</td>" +
"<td class='thirdTd'>" +
"<a style='color:#ff5722' href="+basePath +" rel="external nofollow" rel="external nofollow" /biz/hospital/rate/allot/toPageMx.do?startDate="+$("#startDate").val().toString()
+"&endDate="+$("#endDate").val().toString()
+"&productId_="+data[i].productId_+">"+
data[i].renshu+"</a></td>" +
"<td class='thirdTd'>"+data[i].total+ partCols[i] +
"</tr>";
}
}
}
$("#table_status").empty();
$("#table_status").append(tablex);
},
error : function() {
layer.msg('系統(tǒng)異常,請(qǐng)聯(lián)系管理員!',{icon:2,time:2000});
}
});
}
這塊是name這些的是固定的,就排除掉,然后將數(shù)據(jù)放到arrayPrice1下再push到數(shù)組下。tablex就是表格的html。當(dāng)時(shí)拼接的是分兩步,先是表頭,然后是數(shù)據(jù)。有個(gè)總計(jì),后來在sql下加了WITH ROLLUP就得到了。
for(var key in arrayPrice[i]){
if(i==0&&(arrayPrice[arrayPrice.length-1][key]!=0)){
title+="<th class='firstTh'>"+key+"(元)</th>";
}
if(i==(arrayPrice.length-1)&&(arrayPrice[arrayPrice.length-1][key]!=0)){
sumCols+="<td class='secondTd'>"+arrayPrice[i][key]+"</td>";
}
if(i!=(arrayPrice.length-1)&&(arrayPrice[arrayPrice.length-1][key]!=0)){
partColsStr += "<td class='thirdTd'>"+arrayPrice[i][key]+"</td>";
}
}
這塊就是動(dòng)態(tài)的數(shù)據(jù),title表頭,sumCols總計(jì),partColsStr具體數(shù)據(jù),加到tablex下就行。以上就是原生的思路。

這塊table用到了layui,當(dāng)時(shí)也是看著這個(gè)layui動(dòng)態(tài)設(shè)置的思路去寫的原生。主要思路是:cols是一個(gè)數(shù)組,通過ajax得到數(shù)據(jù)后放到數(shù)組下,再放到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:'支付手機(jī)號(hào)', align:'center',width:'120'};
mycols[2] = {field : 'orderNO', title:'訂單號(hào)', align:'center',width:'120'};
mycols[3] = {field : 'createDate', title:'訂單創(chuàng)建時(shí)間', align:'center',width:'120'};
mycols[4] = {field : 'modifyDate', title:'訂單使用時(shí)間', 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('系統(tǒng)異常,請(qǐng)聯(lián)系管理員!',{icon:2,time:2000});
}
});
以上是ajax調(diào)用后處理的數(shù)組。下面這些就是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()
},
//分頁請(qǐng)求參數(shù)
request:{
pageName: 'pageIndex', //頁碼
limitName: 'limit' //每頁多少數(shù)據(jù)
},
//返回的數(shù)據(jù)格式
response:{
statusName: 'status', //數(shù)據(jù)狀態(tài)的字段名稱,默認(rèn):code
statusCode: 200, //成功的狀態(tài)碼,默認(rèn):0
msgName: 'message', //狀態(tài)信息的字段名稱,默認(rèn):msg
countName: 'total', //數(shù)據(jù)總數(shù)的字段名稱,默認(rèn):count
dataName: 'data' //數(shù)據(jù)列表的字段名稱,默認(rèn):data
},
//每頁10條數(shù)據(jù)
limit: 10,
//加載時(shí)出現(xiàn)加載條
loading: true,
elem: '#data_table',
cols: [
mycols
],
id: 'dataTable',
page: true,
};
//方法級(jí)渲染
table.render(options);
function reload(){
date = new Date($("#endDate").val());
date=date.setDate(date.getDate()+1);
date=new Date(date);
datemonth=date.getMonth()+1; //獲取當(dāng)前月份(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
],
這個(gè)就是動(dòng)態(tài)數(shù)據(jù)。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
javascript基于DOM實(shí)現(xiàn)省市級(jí)聯(lián)下拉框的方法
這篇文章主要介紹了javascript基于DOM實(shí)現(xiàn)省市級(jí)聯(lián)下拉框的方法,可實(shí)現(xiàn)選擇省份后出現(xiàn)對(duì)應(yīng)城市下拉框選項(xiàng)的功能,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-05-05
基于bootstrap寫的一點(diǎn)localStorage本地儲(chǔ)存
這篇文章主要介紹了基于bootstrap寫的一點(diǎn)localStorage本地儲(chǔ)存,需要的朋友可以參考下2017-11-11
JavaScript高級(jí)教程之如何玩轉(zhuǎn)箭頭函數(shù)
箭頭函數(shù)是在es6中添加的一種規(guī)范,箭頭函數(shù)相當(dāng)于匿名函數(shù),簡(jiǎn)化了函數(shù)的定義,下面這篇文章主要給大家介紹了關(guān)于JavaScript高級(jí)教程之如何玩轉(zhuǎn)箭頭函數(shù)的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-11-11

