欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

layui动态表头的实现代码

程序员文章站 2024-01-29 20:49:00
又get到一种思路,不光是layui。 外面这层table,就是用原生拼接的。 @override public list

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

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是医院医生可以配置的科室,这些科室就是动态的。这样一来后台即可得到动态数据。同理,如果在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'>检查人数</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('系统异常,请联系管理员!',{icon:2,time:2000});
      }
    });
    
    
  }

可以在最上方图看到,有些字段是固定的,但是有些字段是动态的。思路是通过将调用接口返回出来的数据,动态的部分放到一个键值对数组下。这样一来,固定的部分,依然可以用返回的data得到,而动态的部分,用处理的数组循环赋值即可。动态表头就是动态数组的键。数据就是值。

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 +" 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('系统异常,请联系管理员!',{icon:2,time:2000});
      }
    });
    
    
  }

这块是name这些的是固定的,就排除掉,然后将数据放到arrayprice1下再push到数组下。tablex就是表格的html。当时拼接的是分两步,先是表头,然后是数据。有个总计,后来在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>";
          
        }
       
      }

这块就是动态的数据,title表头,sumcols总计,partcolsstr具体数据,加到tablex下就行。以上就是原生的思路。

layui动态表头的实现代码

这块table用到了layui,当时也是看着这个layui动态设置的思路去写的原生。主要思路是:cols是一个数组,通过ajax得到数据后放到数组下,再放到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:'订单创建时间', 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();就是重新渲染。

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

可以看到

cols: [
        mycols
       ],
       

这个就是动态数据。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。