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);
}
可以看到,一開(kāi)始是有一個(gè)list,這個(gè)list是醫(yī)院醫(yī)生可以配置的科室,這些科室就是動(dòng)態(tài)的。這樣一來(lái)后臺(tái)即可得到動(dòng)態(tài)數(shù)據(jù)。同理,如果在layui時(shí)用到page,在這里返回成page類型即可。接下來(lái)再看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)的。思路是通過(guò)將調(diào)用接口返回出來(lái)的數(shù)據(jù),動(dòng)態(tài)的部分放到一個(gè)鍵值對(duì)數(shù)組下。這樣一來(lái),固定的部分,依然可以用返回的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ì),后來(lái)在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ù)組,通過(guò)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()
},
//分頁(yè)請(qǐng)求參數(shù)
request:{
pageName: 'pageIndex', //頁(yè)碼
limitName: 'limit' //每頁(yè)多少數(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
},
//每頁(yè)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實(shí)現(xiàn) 網(wǎng)頁(yè)切出 網(wǎng)站title變化代碼
這篇文章主要介紹了基于JavaScript實(shí)現(xiàn) 網(wǎng)頁(yè)切出 網(wǎng)站title變化代碼的相關(guān)資料,需要的朋友可以參考下2016-04-04
強(qiáng)悍無(wú)比的WEB開(kāi)發(fā)好助手FireBug(Firefox Plugin)
強(qiáng)悍無(wú)比的WEB開(kāi)發(fā)好助手FireBug(Firefox Plugin)...2007-01-01
利用JavaScript為句子加標(biāo)題的3種方法示例
這篇文章主要給大家介紹了關(guān)于如何利用JavaScript為句子加標(biāo)題的3種方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01
Webpack實(shí)現(xiàn)按需打包Lodash的幾種方法詳解
這篇文章主要給大家介紹了關(guān)于Webpack實(shí)現(xiàn)按需打包Lodash的幾種方法,文中介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起看看吧。2017-05-05
javascript實(shí)現(xiàn)的LI列表輸出,隔行同色的代碼
javascript實(shí)現(xiàn)的LI列表輸出,隔行同色的代碼...2007-10-10
JavaScript中常見(jiàn)的高階函數(shù)總結(jié)
JavaScript的函數(shù)其實(shí)都指向某個(gè)變量,既然變量可以指向函數(shù),函數(shù)的參數(shù)能接收變量,那么一個(gè)函數(shù)就可以接收另一個(gè)函數(shù)作為參數(shù),這種函數(shù)就稱之為高階函數(shù),這篇文章主要給大家介紹了關(guān)于JavaScript中常見(jiàn)的高階函數(shù),需要的朋友可以參考下2022-02-02
Javascript自定義函數(shù)判斷網(wǎng)站訪問(wèn)類型是PC還是移動(dòng)終端
如果,能夠判斷出訪問(wèn)Web網(wǎng)頁(yè)的類型(PC還是移動(dòng)終端)。就可以解決許多絢麗多彩的 Flash效果出不來(lái)的問(wèn)題2014-01-01
解決js頁(yè)面滾動(dòng)效果scrollTop在FireFox與Chrome瀏覽器間的兼容問(wèn)題的方法
這篇文章為大家分享了解決js頁(yè)面滾動(dòng)效果scrollTop在FireFox與Chrome瀏覽器間的兼容問(wèn)題的方法,感興趣或者是遇到這種問(wèn)題的朋友可以參考這篇文章2015-12-12

