layui動態(tài)表頭的實現(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);
}
可以看到,一開始是有一個list,這個list是醫(yī)院醫(yī)生可以配置的科室,這些科室就是動態(tài)的。這樣一來后臺即可得到動態(tài)數(shù)據(jù)。同理,如果在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 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'>項目名稱</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'>總計</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)異常,請聯(lián)系管理員!',{icon:2,time:2000});
}
});
}
可以在最上方圖看到,有些字段是固定的,但是有些字段是動態(tài)的。思路是通過將調(diào)用接口返回出來的數(shù)據(jù),動態(tài)的部分放到一個鍵值對數(shù)組下。這樣一來,固定的部分,依然可以用返回的data得到,而動態(tài)的部分,用處理的數(shù)組循環(huán)賦值即可。動態(tài)表頭就是動態(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'>項目名稱</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'>總計</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)異常,請聯(lián)系管理員!',{icon:2,time:2000});
}
});
}
這塊是name這些的是固定的,就排除掉,然后將數(shù)據(jù)放到arrayPrice1下再push到數(shù)組下。tablex就是表格的html。當時拼接的是分兩步,先是表頭,然后是數(shù)據(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>";
}
}
這塊就是動態(tài)的數(shù)據(jù),title表頭,sumCols總計,partColsStr具體數(shù)據(jù),加到tablex下就行。以上就是原生的思路。

這塊table用到了layui,當時也是看著這個layui動態(tài)設置的思路去寫的原生。主要思路是:cols是一個數(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:'支付手機號', align:'center',width:'120'};
mycols[2] = {field : 'orderNO', title:'訂單號', align:'center',width:'120'};
mycols[3] = {field : 'createDate', title:'訂單創(chuàng)建時間', 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('系統(tǒ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()
},
//分頁請求參數(shù)
request:{
pageName: 'pageIndex', //頁碼
limitName: 'limit' //每頁多少數(shù)據(jù)
},
//返回的數(shù)據(jù)格式
response:{
statusName: 'status', //數(shù)據(jù)狀態(tài)的字段名稱,默認:code
statusCode: 200, //成功的狀態(tài)碼,默認:0
msgName: 'message', //狀態(tài)信息的字段名稱,默認:msg
countName: 'total', //數(shù)據(jù)總數(shù)的字段名稱,默認:count
dataName: 'data' //數(shù)據(jù)列表的字段名稱,默認:data
},
//每頁10條數(shù)據(jù)
limit: 10,
//加載時出現(xiàn)加載條
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
],
這個就是動態(tài)數(shù)據(jù)。
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
基于JavaScript實現(xiàn) 網(wǎng)頁切出 網(wǎng)站title變化代碼
這篇文章主要介紹了基于JavaScript實現(xiàn) 網(wǎng)頁切出 網(wǎng)站title變化代碼的相關資料,需要的朋友可以參考下2016-04-04
強悍無比的WEB開發(fā)好助手FireBug(Firefox Plugin)
強悍無比的WEB開發(fā)好助手FireBug(Firefox Plugin)...2007-01-01
Webpack實現(xiàn)按需打包Lodash的幾種方法詳解
這篇文章主要給大家介紹了關于Webpack實現(xiàn)按需打包Lodash的幾種方法,文中介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面來一起看看吧。2017-05-05
javascript實現(xiàn)的LI列表輸出,隔行同色的代碼
javascript實現(xiàn)的LI列表輸出,隔行同色的代碼...2007-10-10
Javascript自定義函數(shù)判斷網(wǎng)站訪問類型是PC還是移動終端
如果,能夠判斷出訪問Web網(wǎng)頁的類型(PC還是移動終端)。就可以解決許多絢麗多彩的 Flash效果出不來的問題2014-01-01
解決js頁面滾動效果scrollTop在FireFox與Chrome瀏覽器間的兼容問題的方法
這篇文章為大家分享了解決js頁面滾動效果scrollTop在FireFox與Chrome瀏覽器間的兼容問題的方法,感興趣或者是遇到這種問題的朋友可以參考這篇文章2015-12-12

