利用jquery 实现动态多表头Grid

由于在GridView控件里面实现过一次,所以用js来做也是比较容易的.基本的原来就是分析表头根据字符串进行拆分 比如我这里用的特殊符号是"_"

我在GridView里面实现的是可以无限层的,js的实现我直接写死了层数,不过很容易修改,写个递归就ok啦.

数据源为:

"[{\"用电单位\":\"郭楼三陈\",\"2009-06-01_度数\":\"\",\"2009-06-01_单价\":\"\",\"2009-06-01_金额\":
\"\",\"2009-07-01_度数\":\"242.00\",\"2009-07-01_单价\":\"12.00\",\"2009-07-01_金额\":\"2904.0000\"},
{\"用电单位\":\"狼坡\",\"2009-06-01_度数\":\"190.00\",\"2009-06-01_单价\":\"12.00\",
\"2009-06-01_金额\":\"2280.0000\",\"2009-07-01_度数\":\"23.00\",\"2009-07-01_单价\":\"21.00\",
\"2009-07-01_金额\":\"483.0000\"}];"

可以看到我的表头类似是"2009-06-01_度数"这样的结构,然后通过js进行处理.

   1:  <script>
   2:  $(window).ready(function() {$.getJSON("/Power/PowerStatList/",{ id: "0", sid: "1;2;3", rid: Math.round(Math.random() * 10) },
   3:  function(json) {
   4:      jsonval = eval(json);
   5:      for (var j = 0; j < jsonval.length; j++) {
   6:          $("#GridView1 tr:last").after("<tr id='" + j + "'></tr>");
   7:          var m = 0;
   8:          for (var i in jsonval[j]) {
   9:              if (j == 0) {
  10:                  $("#tableHeader").append("<td id='h" + m + "'>" + i + "</td>");
  11:              }
  12:              $("#GridView1 tr:last").append("<td>" + jsonval[j][i] + "</td>");
  13:              m++;
  14:          }
  15:      }
  16:      $("#GridView1 tr:first").before("<tr id='newHeader'></tr>");
  17:      var temp = "";
  18:      var col = 1;
  19:      var trs = {}
  20:      $.each($("#tableHeader td"), function(i, td) {
  21:   
  22:          if ($(td).text().indexOf('_') > 0) {
  23:              if (temp != "") {
  24:                  if (temp != $(td).text().split('_')[0]) {
  25:                      $("#GridView1 tr:first").append("<td colspan=" + col + ">" + temp + "</td>");
  26:                      col = 1;
  27:                  }
  28:                  else {
  29:                      col++;
  30:                  }
  31:              }
  32:              temp = $(td).text().split('_')[0];
  33:          }
  34:          else {
  35:              $("#GridView1 tr:first").append("<td rowspan=2>" + $("#h" + i).text() + "</td>");
  36:              $("#h" + i).remove();
  37:          }
  38:          $(td).text($(td).text().split('_')[0]);
  39:      });
  40:      $("#GridView1 tr:first").append("<td colspan=" + col + ">" + temp + "</td>");
  41:      
  42:  });
  43:  });
  44:      
  45:      </script>
  46:   
  47:      <table border=1 id="GridView1">
  48:          <tr id="tableHeader">
  49:             
  50:          </tr>
  51:          
  52:      </table>

效果:

image

加上样式就好看了 O(∩_∩)O哈哈~

转载于:https://www.cnblogs.com/doublog/archive/2009/07/09/1519538.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值