table-fixer.jquery.js 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. (function ($) {
  2. $.fn.tableFixer = function (param) {
  3. return this.each(function () {
  4. table.call(this);
  5. });
  6. function table() {
  7. /**
  8. * This function solver z-index problem in corner cell
  9. * where fix row and column at the same time,
  10. * set corner cells z-index 1 more then other fixed cells
  11. */
  12. function setCorner() {
  13. var table = $(settings.table);
  14. if (settings.head) {
  15. var tr;
  16. if (settings.left > 0) {
  17. tr = table.find("> thead > tr");
  18. tr.each(function (k, row) {
  19. solveLeftColspan(row, function (cell) {
  20. $(cell).css("z-index", settings['z-index'] + 1);
  21. });
  22. });
  23. }
  24. if (settings.right > 0) {
  25. tr = table.find("> thead > tr");
  26. tr.each(function (k, row) {
  27. solveRightColspan(row, function (cell) {
  28. $(cell).css("z-index", settings['z-index'] + 1);
  29. });
  30. });
  31. }
  32. }
  33. if (settings.foot) {
  34. if (settings.left > 0) {
  35. tr = table.find("> tfoot > tr");
  36. tr.each(function (k, row) {
  37. solveLeftColspan(row, function (cell) {
  38. $(cell).css("z-index", settings['z-index'] + 1);
  39. });
  40. });
  41. }
  42. if (settings.right > 0) {
  43. tr = table.find("> tfoot > tr");
  44. tr.each(function (k, row) {
  45. solveRightColspan(row, function (cell) {
  46. $(cell).css("z-index", settings['z-index'] + 1);
  47. });
  48. });
  49. }
  50. }
  51. }
  52. // Set style of table parent
  53. function setParent() {
  54. var parent = $(settings.parent);
  55. var table = $(settings.table);
  56. parent.append(table).css({
  57. 'overflow-x': 'auto',
  58. 'overflow-y': 'auto',
  59. 'position': 'relative', // for cells position computation
  60. });
  61. parent.on('scroll', $.proxy(function () {
  62. var scrollWidth = parent[0].scrollWidth;
  63. var clientWidth = parent[0].clientWidth;
  64. var scrollHeight = parent[0].scrollHeight;
  65. var clientHeight = parent[0].clientHeight;
  66. var scroll_top = parent.scrollTop();
  67. var scroll_left = parent.scrollLeft();
  68. var table_position = {
  69. left: table[0].offsetLeft,
  70. top: table[0].offsetTop,
  71. };
  72. table_position.right = scrollWidth - table_position.left - table.width();
  73. table_position.bottom = scrollHeight - table_position.top - table.height();
  74. if (settings.head) {
  75. var top = scroll_top - table_position.top;
  76. this.find("> thead > tr > *").css("top", top < 0 ? 0 : top);
  77. }
  78. if (settings.foot) {
  79. var bottom = scrollHeight - clientHeight - scroll_top - table_position.bottom;
  80. this.find("> tfoot > tr > *").css("bottom", bottom < 0 ? 0 : bottom);
  81. }
  82. if (settings.left > 0) {
  83. var left = scroll_left - table_position.left;
  84. settings.leftColumns.css("left", left < 0 ? 0 : left);
  85. }
  86. if (settings.right > 0) {
  87. var right = scrollWidth - clientWidth - scroll_left - table_position.right;
  88. settings.rightColumns.css("right", right < 0 ? 0 : right);
  89. }
  90. }, table));
  91. }
  92. // Set table head fixed
  93. function fixHead() {
  94. var thead = $(settings.table).find("> thead");
  95. var cells = thead.find("> tr > *");
  96. setBackground(cells);
  97. cells.css({
  98. 'position': 'relative',
  99. 'z-index': settings['z-index'],
  100. });
  101. }
  102. // Set table foot fixed
  103. function fixFoot() {
  104. var tfoot = $(settings.table).find("> tfoot");
  105. var cells = tfoot.find("> tr > *");
  106. setBackground(cells);
  107. cells.css({
  108. 'position': 'relative',
  109. 'z-index': settings['z-index'],
  110. });
  111. }
  112. // Set table left column fixed
  113. function fixLeft() {
  114. var table = $(settings.table);
  115. settings.leftColumns = $();
  116. var tr = table.find("> thead > tr, > tbody > tr, > tfoot > tr");
  117. tr.each(function (k, row) {
  118. solveLeftColspan(row, function (cell) {
  119. settings.leftColumns = settings.leftColumns.add(cell);
  120. });
  121. });
  122. var column = settings.leftColumns;
  123. column.each(function (k, cell) {
  124. cell = $(cell);
  125. setBackground(cell);
  126. cell.css({
  127. 'position': 'relative',
  128. 'z-index': settings['z-index'],
  129. });
  130. });
  131. }
  132. // Set table right column fixed
  133. function fixRight() {
  134. var table = $(settings.table);
  135. settings.rightColumns = $();
  136. var tr = table.find("> thead > tr, > tbody > tr, > tfoot > tr");
  137. tr.each(function (k, row) {
  138. solveRightColspan(row, function (cell) {
  139. settings.rightColumns = settings.rightColumns.add(cell);
  140. });
  141. });
  142. var column = settings.rightColumns;
  143. column.each(function (k, cell) {
  144. cell = $(cell);
  145. setBackground(cell);
  146. cell.css({
  147. 'position': 'relative',
  148. 'z-index': settings['z-index'],
  149. });
  150. });
  151. }
  152. // Set fixed cells backgrounds
  153. function setBackground(elements) {
  154. elements.each(function (k, element) {
  155. element = $(element);
  156. var parent = $(element).parent();
  157. var elementBackground = element.css("background-color");
  158. elementBackground = (elementBackground === "transparent" || elementBackground === "rgba(0, 0, 0, 0)")
  159. ? null
  160. : elementBackground;
  161. var parentBackground = parent.css("background-color");
  162. parentBackground = (parentBackground === "transparent" || parentBackground === "rgba(0, 0, 0, 0)")
  163. ? null
  164. : parentBackground;
  165. var background = parentBackground ? parentBackground : "white";
  166. background = elementBackground ? elementBackground : background;
  167. element.css("background-color", background);
  168. });
  169. }
  170. function solveLeftColspan(row, action) {
  171. var fixColumn = settings.left;
  172. var inc = 1;
  173. for (var i = 1; i <= fixColumn; i = i + inc) {
  174. var nth = inc > 1 ? i - 1 : i;
  175. var cell = $(row).find("> *:nth-child(" + nth + ")");
  176. var colspan = cell.prop("colspan");
  177. if (typeof cell.cellPos() !== 'undefined' && cell.cellPos().left < fixColumn) {
  178. action(cell);
  179. }
  180. inc = colspan;
  181. }
  182. }
  183. function solveRightColspan(row, action) {
  184. var fixColumn = settings.right;
  185. var inc = 1;
  186. for (var i = 1; i <= fixColumn; i = i + inc) {
  187. var nth = inc > 1 ? i - 1 : i;
  188. var cell = $(row).find("> *:nth-last-child(" + nth + ")");
  189. var colspan = cell.prop("colspan");
  190. action(cell);
  191. inc = colspan;
  192. }
  193. }
  194. var defaults = {
  195. head: true,
  196. foot: false,
  197. left: 0,
  198. right: 0,
  199. 'z-index': 1,
  200. };
  201. var settings = $.extend({}, defaults, param);
  202. settings.head = Boolean(settings.head);
  203. settings.foot = Boolean(settings.foot);
  204. settings.table = this;
  205. settings.parent = $(settings.table).parent();
  206. setParent();
  207. if (settings.head === true) {
  208. fixHead();
  209. }
  210. if (settings.foot === true) {
  211. fixFoot();
  212. }
  213. if (settings.left > 0) {
  214. fixLeft();
  215. }
  216. if (settings.right > 0) {
  217. fixRight();
  218. }
  219. setCorner();
  220. $(settings.parent).trigger("scroll");
  221. $(window).resize(function () {
  222. $(settings.parent).trigger("scroll");
  223. });
  224. }
  225. };
  226. })(jQuery);
  227. /**
  228. * cellPos jQuery plugin
  229. * ---------------------
  230. * Get visual position of cell in HTML table (or its block like thead).
  231. * Return value is object with "top" and "left" properties set to row
  232. * and column index of top-left cell corner.
  233. * Example of use:
  234. * $("#myTable tbody td").each(function(){
  235. * $(this).text( $(this).cellPos().top +", "+ $(this).cellPos().left );
  236. * });
  237. */
  238. (function ($) {
  239. /* scan individual table and set "cellPos" data in the form { left: x-coord, top: y-coord } */
  240. function scanTable($table) {
  241. var m = [];
  242. $table.children("tr").each(function (y, row) {
  243. $(row).children("td, th").each(function (x, cell) {
  244. var $cell = $(cell),
  245. colspan = $cell.attr("colspan") | 0,
  246. rowspan = $cell.attr("rowspan") | 0,
  247. tx, ty;
  248. colspan = colspan ? colspan : 1;
  249. rowspan = rowspan ? rowspan : 1;
  250. // noinspection StatementWithEmptyBodyJS
  251. for (; m[y] && m[y][x]; ++x); //skip already occupied cells in current row
  252. for (tx = x; tx < x + colspan; ++tx) { //mark matrix elements occupied by current cell with true
  253. for (ty = y; ty < y + rowspan; ++ty) {
  254. if (!m[ty]) { //fill missing rows
  255. m[ty] = [];
  256. }
  257. m[ty][tx] = true;
  258. }
  259. }
  260. var pos = {top: y, left: x};
  261. $cell.data("cellPos", pos);
  262. });
  263. });
  264. }
  265. /* plugin */
  266. $.fn.cellPos = function (rescan) {
  267. var $cell = this.first(),
  268. pos = $cell.data("cellPos");
  269. if (!pos || rescan) {
  270. var $table = $cell.closest("table, thead, tbody, tfoot");
  271. scanTable($table);
  272. }
  273. pos = $cell.data("cellPos");
  274. return pos;
  275. }
  276. })(jQuery);