TableFix.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /**
  2. *
  3. * 固定普通table的行和列
  4. *
  5. *
  6. */
  7. if (typeof TableFix == 'undefined') {
  8. TableFix = {};
  9. };
  10. /**
  11. * 固定表头和列([tablefix='headRow']表示平台中的表头)
  12. *
  13. * @param tableId
  14. * @param fixColNum 需要固定的列数
  15. * @param width
  16. * @param height
  17. */
  18. TableFix.fix = function(tableId, fixColNum, width, height) {
  19. if ($("#" + tableId + "_tableLayout").length != 0) {
  20. $("#" + tableId + "_tableLayout").before($("#" + tableId));
  21. $("#" + tableId + "_tableLayout").empty();
  22. } else {
  23. $("#" + tableId).after("<div id='" + tableId + "_tableLayout' style='overflow:hidden;height:"
  24. + height + "px; width:" + width + "px;'></div>");
  25. }
  26. $('<div id="' + tableId + '_tableFix"></div>'
  27. + '<div id="' + tableId + '_tableHead"></div>'
  28. + '<div id="' + tableId + '_tableColumn"></div>'
  29. + '<div id="' + tableId + '_tableData"></div>')
  30. .appendTo("#" + tableId + "_tableLayout");
  31. var oldtable = $("#" + tableId);
  32. var tableFixClone = oldtable.clone(true);
  33. tableFixClone.attr("id", tableId + "_tableFixClone");
  34. $("#" + tableId + "_tableFix").append(tableFixClone);
  35. var tableHeadClone = oldtable.clone(true);
  36. tableHeadClone.attr("id", tableId + "_tableHeadClone");
  37. $("#" + tableId + "_tableHead").append(tableHeadClone);
  38. var tableColumnClone = oldtable.clone(true);
  39. tableColumnClone.attr("id", tableId + "_tableColumnClone");
  40. $("#" + tableId + "_tableColumn").append(tableColumnClone);
  41. $("#" + tableId + "_tableData").append(oldtable);
  42. $("#" + tableId + "_tableLayout table").each(function() {
  43. $(this).css("margin", "0");
  44. });
  45. var headHeight = 0;
  46. $("#" + tableId + "_tableHead").find("[tablefix='headRow']").each(function (){
  47. headHeight = headHeight + $(this).height();
  48. });
  49. $("#" + tableId + "_tableHead").css("height", headHeight);
  50. $("#" + tableId + "_tableFix").css("height", headHeight);
  51. var ColumnsWidth = 0;
  52. var ColumnsNumber = 0;
  53. $("#" + tableId + "_tableColumn tr:last td:lt(" + fixColNum + ")").each(function() {
  54. ColumnsWidth += $(this).outerWidth(true);
  55. ColumnsNumber++;
  56. });
  57. ColumnsWidth += 2;
  58. if ($.browser.msie) {
  59. switch ($.browser.version) {
  60. case "7.0":
  61. if (ColumnsNumber >= 3)
  62. ColumnsWidth--;
  63. break;
  64. case "8.0":
  65. if (ColumnsNumber >= 2)
  66. ColumnsWidth--;
  67. break;
  68. }
  69. }
  70. $("#" + tableId + "_tableColumn").css("width", ColumnsWidth);
  71. $("#" + tableId + "_tableFix").css("width", ColumnsWidth);
  72. $("#" + tableId + "_tableData").scroll(
  73. function() {
  74. $("#" + tableId + "_tableHead").scrollLeft(
  75. $("#" + tableId + "_tableData").scrollLeft());
  76. $("#" + tableId + "_tableColumn").scrollTop(
  77. $("#" + tableId + "_tableData").scrollTop());
  78. });
  79. $("#" + tableId + "_tableFix").css({
  80. "overflow" : "hidden",
  81. "position" : "relative",
  82. "z-index" : "50",
  83. "background-color" : "Silver"
  84. });
  85. $("#" + tableId + "_tableHead").css({
  86. "overflow" : "hidden",
  87. "width" : width - 17,
  88. "position" : "relative",
  89. "z-index" : "45",
  90. "background-color" : "Silver"
  91. });
  92. $("#" + tableId + "_tableColumn").css({
  93. "overflow" : "hidden",
  94. "height" : height - 17,
  95. "position" : "relative",
  96. "z-index" : "40",
  97. "background-color" : "Silver"
  98. });
  99. $("#" + tableId + "_tableData").css({
  100. "overflow" : "auto",
  101. "width" : width,
  102. "height" : height,
  103. "position" : "relative",
  104. "z-index" : "35"
  105. });
  106. if ($("#" + tableId + "_tableHead").width() > $(
  107. "#" + tableId + "_tableFix table").width()) {
  108. $("#" + tableId + "_tableHead").css("width",
  109. $("#" + tableId + "_tableFix table").width());
  110. $("#" + tableId + "_tableData").css("width",
  111. $("#" + tableId + "_tableFix table").width() + 17);
  112. }
  113. if ($("#" + tableId + "_tableColumn").height() > $(
  114. "#" + tableId + "_tableColumn table").height()) {
  115. $("#" + tableId + "_tableColumn").css("height",
  116. $("#" + tableId + "_tableColumn table").height());
  117. $("#" + tableId + "_tableData").css("height",
  118. $("#" + tableId + "_tableColumn table").height() + 17);
  119. }
  120. $("#" + tableId + "_tableFix").offset(
  121. $("#" + tableId + "_tableLayout").offset());
  122. $("#" + tableId + "_tableHead").offset(
  123. $("#" + tableId + "_tableLayout").offset());
  124. $("#" + tableId + "_tableColumn").offset(
  125. $("#" + tableId + "_tableLayout").offset());
  126. $("#" + tableId + "_tableData").offset(
  127. $("#" + tableId + "_tableLayout").offset());
  128. $("#" + tableId + "_tableFixClone").find("[name]").removeAttr("name");
  129. $("#" + tableId + "_tableHeadClone").find("[name]").removeAttr("name");
  130. $("#" + tableId + "_tableColumnClone").find("[name]").removeAttr("name");
  131. $("#" + tableId + "_tableFixClone").find("[formtype]").removeAttr("formtype");
  132. $("#" + tableId + "_tableHeadClone").find("[formtype]").removeAttr("formtype");
  133. $("#" + tableId + "_tableColumnClone").find("[formtype]").removeAttr("formtype");
  134. }
  135. /**
  136. * 还原改变后的表
  137. *
  138. * @param tableId
  139. */
  140. TableFix.restore = function(tableId) {
  141. var table = $("#" + tableId);
  142. var tableClone = table.clone(true);
  143. $("#" + tableId + "_tableLayout").after(tableClone);
  144. $("#" + tableId + "_tableLayout").remove();
  145. }