You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

136 lines
6.3 KiB

  1. $(function(){
  2. var sourceTable = $("#table");//table id 获取表对象
  3. var sourceTableHead = $("#table_head");//table thead tr id 获取表头thead
  4. var headHeight = sourceTableHead.outerHeight();//table thead tr height 获取资源表的宽度
  5. //copy table and thead html tag from source table,
  6. $('#table').append('<div id="shelter"><table id="fixed_table" class="table table-bordered table-hover permission"><thead></thead></table></div>'); //重建一个table添加到body中
  7. //only set top and left,beacuse i need the top bar can scroll left
  8. $("#shelter").css({'height':headHeight,'position':'fixed','top':'0','left':'50px', 'z-index': 30}); // 设置目标表格的高度等于源表格的高度,高度是一定的
  9. $('#table_head th').bind('DOMNodeInserted', function(e){
  10. // 当窗口变化时重复以上步骤
  11. var sourceTable = $("#table");//table id
  12. var sourceTableHead = $("#table_head");//table thead tr id
  13. var headHeight = sourceTableHead.outerHeight();//table thead tr height
  14. $('#shelter').remove();
  15. $('#table').append('<div id="shelter" class="shelter"><table id="fixed_table" class="table table-bordered table-hover permission"><thead></thead></table></div>');
  16. //only set top and left,beacuse i need the top bar can scroll left
  17. $("#shelter").css({'height':headHeight,'position':'fixed','top':'0','left':'50px', 'z-index':30});
  18. var targetHead = sourceTableHead.clone();
  19. targetHead.attr('id','fix_head');
  20. targetHead.appendTo('#fixed_table thead');
  21. targetHead.find('input').attr('onclick', 'alterCheck(this)');
  22. var sourceTableWidth = sourceTable.outerWidth(); //get source table width
  23. $("#fixed_table").css({'width': sourceTableWidth + "px"});
  24. // $('.shelter').remove();
  25. // 获取源表格的头部,遍历头部的th标签,设置目标表格的头的宽度
  26. $("#table_head th").each(function(index,value){
  27. var tempWidth = $(value).outerWidth();
  28. var tempHeight = $(value).outerHeight();
  29. $("#fixed_table th").eq(index).css({'width':tempWidth+"px",'height':tempHeight+"px"});
  30. });
  31. $("#shelter").hide();
  32. });
  33. $(window).resize(function()
  34. {
  35. // 当窗口变化时重复以上步骤
  36. var sourceTable = $("#table");//table id
  37. var sourceTableHead = $("#table_head");//table thead tr id
  38. var headHeight = sourceTableHead.outerHeight();//table thead tr height
  39. $('#table').append('<div id="shelter" class="shelter removeShelter"><table id="fixed_table" class="table table-bordered table-hover permission"><thead></thead></table></div>');
  40. //only set top and left,beacuse i need the top bar can scroll left
  41. $("#shelter").css({'height':headHeight,'position':'fixed','top':'0','left':'50px', 'z-index':30});
  42. var sourceTableWidth = sourceTable.outerWidth(); //get source table width
  43. $("#fixed_table").css({'width':sourceTableWidth+"px"});
  44. $('.removeShelter').remove();
  45. // 窗口变化时获取源表格的头部,遍历头部的th标签,设置目标表格的头的宽度
  46. $("#table_head th").each(function(index,value){
  47. var tempWidth = $(value).outerWidth();
  48. var tempHeight = $(value).outerHeight();
  49. $("#fixed_table th").eq(index).css({'width':tempWidth+"px",'height':tempHeight+"px"});
  50. });
  51. /*var targetHead = sourceTableHead.clone();
  52. targetHead.attr('id','fix_head');
  53. targetHead.appendTo('#fixed_table thead');
  54. targetHead.find('input').attr('onclick', 'alterCheck(this)');*/
  55. });
  56. // 克隆源表格的头部并设置目标表格的id=fix_head
  57. var targetHead = sourceTableHead.clone();
  58. targetHead.attr('id','fix_head');
  59. targetHead.appendTo('#fixed_table thead');
  60. targetHead.find('input').attr('onclick', 'alterCheck(this)');
  61. // 根据源表的宽度和高度设置目标的宽和高
  62. $("#table_head th").each(function(index,value){
  63. var tempWidth = $(value).outerWidth();
  64. var tempHeight = $(value).outerHeight();
  65. $("#fixed_table th").eq(index).css({'width':tempWidth+"px",'height':tempHeight+"px"});
  66. });
  67. // 先将目标表头隐藏
  68. $("#shelter").hide();
  69. // 窗口滚动时触发的事件
  70. $(window).scroll(function(){
  71. var sl=-Math.max($('body').scrollLeft(), $('document').scrollLeft());
  72. if(isNaN(sl)){
  73. sl = - $(window).scrollLeft();
  74. }
  75. $('#shelter').css("left", 50 + sl +'px');
  76. var scroll_top = $(document).scrollTop() - sourceTable.offset().top;
  77. //control show or hide
  78. if (scroll_top > 0) {
  79. // console.log(scroll_top);
  80. $('#shelter').show();
  81. }else {
  82. // console.log(scroll_top);
  83. $('#shelter').hide();
  84. }
  85. });
  86. });
  87. /*
  88. 提取函数,减少冗余代码
  89. setNewTbTh('table', 'table_head', 'table table-bordered table-hover permission', 50)
  90. function setNewTbTh(table, table_head, taClass, taRemove, left)
  91. {
  92. var sourceTable = $("#" + table);//table id
  93. var sourceTableHead = $("#" + table_head);//table thead tr id
  94. var headHeight = sourceTableHead.outerHeight();//table thead tr height
  95. if(taRemove == 1)
  96. {
  97. $('#shelter').remove();
  98. }
  99. $('#table').append('<div id="shelter" class="shelter"><table id="fixed_table" class="taClass"><thead></thead></table></div>');
  100. //only set top and left,beacuse i need the top bar can scroll left
  101. $("#shelter").css({'height':headHeight,'position':'fixed','top':'0','left': left + 'px', 'z-index':30});
  102. var targetHead = sourceTableHead.clone();
  103. targetHead.attr('id','fix_head');
  104. targetHead.appendTo('#fixed_table thead');
  105. targetHead.find('input').attr('onclick', 'alterCheck(this)');
  106. var sourceTableWidth = sourceTable.outerWidth(); //get source table width
  107. $("#fixed_table").css({'width': sourceTableWidth + "px"});
  108. // $('.shelter').remove();
  109. // 获取源表格的头部,遍历头部的th标签,设置目标表格的头的宽度
  110. $("#" + table_head + " th").each(function(index,value){
  111. var tempWidth = $(value).outerWidth();
  112. var tempHeight = $(value).outerHeight();
  113. $("#fixed_table th").eq(index).css({'width':tempWidth+"px",'height':tempHeight+"px"});
  114. });
  115. $("#shelter").hide();
  116. }*/