您好,登錄后才能下訂單哦!
這期內(nèi)容當(dāng)中小編將會(huì)給大家?guī)?lái)有關(guān)使用layui怎么實(shí)現(xiàn)一個(gè)動(dòng)態(tài)表頭,文章內(nèi)容豐富且以專(zhuān)業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
layui是一款采用自身模塊規(guī)范編寫(xiě)的前端UI框架,它遵循原生HTML/CSS/JS的書(shū)寫(xiě)與組織形式,門(mén)檻極低,適合新手,并且它還提供了豐富的內(nèi)置模塊,他們皆可通過(guò)模塊化的方式按需加載,從核心代碼到API的每一處細(xì)節(jié)都經(jīng)過(guò)精心雕琢,非常適合界面的快速開(kāi)發(fā),能夠作為PC網(wǎng)頁(yè)端后臺(tái)系統(tǒng)與前臺(tái)界面的速成開(kāi)發(fā)方案。
@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)型即可。接下來(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)目名稱(chē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)目名稱(chē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è)置的思路去寫(xiě)的原生。主要思路是: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)的字段名稱(chēng),默認(rèn):code statusCode: 200, //成功的狀態(tài)碼,默認(rèn):0 msgName: 'message', //狀態(tài)信息的字段名稱(chēng),默認(rèn):msg countName: 'total', //數(shù)據(jù)總數(shù)的字段名稱(chēng),默認(rèn):count dataName: 'data' //數(shù)據(jù)列表的字段名稱(chēng),默認(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 ],
上述就是小編為大家分享的使用layui怎么實(shí)現(xiàn)一個(gè)動(dòng)態(tài)表頭了,如果剛好有類(lèi)似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。