layui動態表頭

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

clipboard.png

外面這層table,就是用原生拼接的。ajax

@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類型便可。接下來再看jssql

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'>檢查人數</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 +"/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('系統異常,請聯繫管理員!',{icon:2,time:2000});
            }
        });
        
        
    }

能夠在最上方圖看到,有些字段是固定的,可是有些字段是動態的。思路是經過將調用接口返回出來的數據,動態的部分放到一個鍵值對數組下。這樣一來,固定的部分,依然能夠用返回的data獲得,而動態的部分,用處理的數組循環賦值便可。動態表頭就是動態數組的鍵。數據就是值。json

if(key!="name"&&key!="productId_"&&key!="renshu"&&key!="total"){
                        arrayPrice1[key]=data[i][key];
                    }

這塊是name這些的是固定的,就排除掉,而後將數據放到arrayPrice1下再push到數組下。tablex就是表格的html。當時拼接的是分兩步,先是表頭,而後是數據。有個總計,後來在sql下加了WITH ROLLUP就獲得了。數組

for(var key in arrayPrice[i]){app

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>";
                    
                }
             
            }

這塊就是動態的數據,title表頭,sumCols總計,partColsStr具體數據,加到tablex下就行。以上就是原生的思路。ide

clipboard.png

這塊table用到了layui,當時也是看着這個layui動態設置的思路去寫的原生。主要思路是:cols是一個數組,經過ajax獲得數據後放到數組下,再放到cols下便可。post

$.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:'訂單建立時間', 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('系統異常,請聯繫管理員!',{icon:2,time:2000});
    }
});

以上是ajax調用後處理的數組。下面這些就是table。reload();就是從新渲染。ui

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()
            },
            //分頁請求參數
            request:{
                pageName: 'pageIndex', //頁碼
                limitName: 'limit' //每頁多少數據
            },
            //返回的數據格式
            response:{
                 statusName: 'status', //數據狀態的字段名稱,默認:code
                 statusCode: 200, //成功的狀態碼,默認:0
                 msgName: 'message', //狀態信息的字段名稱,默認:msg
                 countName: 'total', //數據總數的字段名稱,默認:count
                 dataName: 'data' //數據列表的字段名稱,默認:data
            },
            //每頁10條數據
            limit: 10,
            
            //加載時出現加載條
            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);
        
    }

能夠看到this

cols: [
                mycols
              ],

這個就是動態數據。

相關文章
相關標籤/搜索