Blame view

WebRoot/static/js/tablescroller.js 11.4 KB
d3d7b2ca   孙向锦   修改表格固定
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
  //********************************************************************************************************************************************
  //*
  //*  TableScroller
  //*  creates a horizontal and vertical scrolling table with pinned row and column headers
  //*
  //*  element - the dom reference to the table that will be converted to a scrollable table
  //*  options:
  //*     width - the width of the table
  //*     height - the height of the table
  //*     pinnedRows - the number of pinned rows
  //*     pinnedCols - the number of pinned columns
  //*     container - the container id for the scrollable table
  //*     removeOriginal - indicator for removing the orignal table
  //*
  //*  returns:
  //*     ref {
  //*         options - the original options used to configure the scroller
  //*         corner - dom reference to corner frame
  //*         cornerTable - dom reference to corner table
  //*         scrollableColumns - dom reference to scrollable columns frame
  //*         scrollableColumnsTable - dom reference to scrollable columns table
  //*         scrollableRows - dom reference to scrollable rows frame
  //*         scrollableRowsTable - dom reference to scrollable rows table
  //*         scrollableWindow - dom reference to scrollable data frame
  //*         scrollableWindowTable - dom reference to scrollable data table
  //*     }
  //*
  //********************************************************************************************************************************************
  (function () {
  
      if (!Object.assign) {
          Object.defineProperty(Object, 'assign', {
              enumerable: false,
              configurable: true,
              writable: true,
              value: function (target) {
                  'use strict';
                  if (target === undefined || target === null) {
                      throw new TypeError('Cannot convert first argument to object');
                  }
  
                  var to = Object(target);
                  for (var i = 1; i < arguments.length; i++) {
                      var nextSource = arguments[i];
                      if (nextSource === undefined || nextSource === null) {
                          continue;
                      }
                      nextSource = Object(nextSource);
  
                      var keysArray = Object.keys(nextSource);
                      for (var nextIndex = 0, len = keysArray.length; nextIndex < len; nextIndex++) {
                          var nextKey = keysArray[nextIndex];
                          var desc = Object.getOwnPropertyDescriptor(nextSource, nextKey);
                          if (desc !== undefined && desc.enumerable) {
                              to[nextKey] = nextSource[nextKey];
                          }
                      }
                  }
                  return to;
              }
          });
      }
  
      var cloneTableSection = function (table, rowStart, rowEnd, columnStart, columnEnd) {
  
          var cloneTable = table.cloneNode();
          cloneTable.removeAttribute("id");
  
          var rows = table.getElementsByTagName("tr");
          for (var i = rowStart; (rowEnd == -1 || i < rowEnd) && i < rows.length; i++) {
              var row = rows[i];
              var parent = row.parentNode;
              var parentName = parent.nodeName.toLowerCase();
              if (parentName != "table" && cloneTable.getElementsByTagName(parentName).length == 0) {
                  parent = parent.cloneNode();
                  cloneTable.appendChild(parent);
              }
              else {
                  parent = cloneTable.getElementsByTagName(parentName)[0];
              }
  
              var cloneRow = row.cloneNode();
              var columns = row.getElementsByTagName("th");
              if (columns.length == 0)
                  columns = row.getElementsByTagName("td");
  
              for (var j = columnStart; (columnEnd == -1 || j < columnEnd) && j < columns.length; j++) {
                  cloneRow.appendChild(columns[j].cloneNode(true));
              }
  
              parent.appendChild(cloneRow);
          }
  
          return cloneTable;
      };
  
      var getWidth = function (element) {
          var cssWidth = getComputedStyle(element, null).getPropertyValue("width");
          return parseFloat(cssWidth.replace("px", ""));
      }
  
      var getHeight = function (element) {
          var cssHeight = getComputedStyle(element, null).getPropertyValue("height");
          return parseFloat(cssHeight.replace("px", ""));
      }
  
      var alignTableColumnWidths = function (table1, table2) {
          if (table1.childNodes.length > 0 && table2.childNodes.length > 0) {
              var table1Cells = table1.getElementsByTagName("tr")[0].getElementsByTagName("th");
              if (table1Cells.length == 0)
                  table1Cells = table1.getElementsByTagName("tr")[0].getElementsByTagName("td");
  
              var table2Cells = table2.getElementsByTagName("tr")[0].getElementsByTagName("th");
              if (table2Cells.length == 0)
                  table2Cells = table2.getElementsByTagName("tr")[0].getElementsByTagName("td");
  
              for (var i = 0; i < table1Cells.length; i++) {
                  if (getWidth(table1Cells[i]) > getWidth(table2Cells[i])) {
                      table2Cells[i].style.width = getWidth(table1Cells[i]) + "px";
                  }
                  else {
                      table1Cells[i].style.width = getWidth(table2Cells[i]) + "px";
                  }
              }
          }
      }
  
      this.TableScroller = function (element, options) {
  
          if (typeof element == undefined || element == null || element.nodeName.toLowerCase() != "table")
              throw "Invalid table element specified";
  
          if (typeof options == undefined || options == null) throw "Options must be specified";
  
          var defaults = {
              width: element.offsetWidth,
              height: element.offsetHeight,
              pinnedRows: 1,
              pinnedCols: 0,
              container: ""
          };
  
          options = Object.assign(defaults, options);
  
          var containerDiv = null;
          if (options.container == "") {
              var id = "tablescroller-1";
              containerDiv = document.createElement("div");
              containerDiv.setAttribute("id", id);
  
              options.container = "#" + id;
              element.parentNode.appendChild(containerDiv);
          }
          else {
              containerDiv = document.getElementById(options.container.substr(1));
          }
  
          containerDiv.style.width = options.width + "px";
          containerDiv.style.height = options.height + "px";
  
          var ref = {
              options: options,
              corner: null,
              cornerTable: null,
              scrollableColumns: null,
              scrollableColumnsTable: null,
              scrollableRows: null,
              scrollableRowsTable: null,
              scrollableWindow: null,
              scrollableWindowTable: null
          };
  
          // corner section window and table
          if (options.pinnedRows > 0 && options.pinnedCols > 0) {
              ref.corner = document.createElement("div");
              ref.corner.className = "corner-frame";
              ref.cornerTable = cloneTableSection(element, 0, options.pinnedRows, 0, options.pinnedCols);
              ref.cornerTable.className = ref.cornerTable.className + " corner-table";
              containerDiv.appendChild(ref.corner);
              ref.corner.appendChild(ref.cornerTable);
          }
  
          // scrollable columns window and table
          if (options.pinnedRows > 0) {
              ref.scrollableColumns = document.createElement("div");
              ref.scrollableColumns.className = "scrollable-columns-frame";
              ref.scrollableColumnsTable = cloneTableSection(element, 0, options.pinnedRows, options.pinnedCols, -1);
              ref.scrollableColumnsTable.className = ref.scrollableColumnsTable.className + " scrollable-columns-table";
              containerDiv.appendChild(ref.scrollableColumns);
              ref.scrollableColumns.appendChild(ref.scrollableColumnsTable);
          }
  
          // scrollable rows window and table
          if (options.pinnedCols > 0) {
              ref.scrollableRows = document.createElement("div");
              ref.scrollableRows.className = "scrollable-rows-frame";
              ref.scrollableRowsTable = cloneTableSection(element, options.pinnedRows, -1, 0, options.pinnedCols);
              ref.scrollableRowsTable.className = ref.scrollableRowsTable.className + " scrollable-rows-table";
              containerDiv.appendChild(ref.scrollableRows);
              ref.scrollableRows.appendChild(ref.scrollableRowsTable);
          }
  
          ref.scrollableWindow = document.createElement("div");
          ref.scrollableWindow.className = "scrollable-data-frame";
          ref.scrollableWindowTable = cloneTableSection(element, options.pinnedRows, -1, options.pinnedCols, -1);
          ref.scrollableWindowTable.className = ref.scrollableWindowTable.className + " scrollable-data-table";
          containerDiv.appendChild(ref.scrollableWindow);
          ref.scrollableWindow.appendChild(ref.scrollableWindowTable);
  
          // adjust scrollable rows column widths
          if (ref.corner != null) {
              alignTableColumnWidths(ref.cornerTable, ref.scrollableRowsTable);
          }
  
          //adjust scrollable columns window width
          if (ref.scrollableColumnsTable != null) {
              var scwWidth = getWidth(ref.scrollableColumnsTable);
              if (scwWidth > getWidth(ref.scrollableWindowTable)) ref.scrollableWindowTable.style.width = getWidth(ref.scrollableColumnsTable) + "px";
              else ref.scrollableColumnsTable.style.width = getWidth(ref.scrollableWindowTable) + "px";
  
              //adjust scrollable columns column widths
              alignTableColumnWidths(ref.scrollableColumnsTable, ref.scrollableWindowTable);
          }
  
          // adjust width
          var width = ref.scrollableRows != null ? options.width - getWidth(ref.scrollableRows) : options.width;
          ref.scrollableWindow.style.width = width + "px";
  
          // adjust height
          var height = ref.scrollableColumns != null ? options.height - getHeight(ref.scrollableColumns) : options.height;
          ref.scrollableWindow.style.height = height + "px";
  
          // update scrollable column window or scrollable row window as the user scrolls through the scrollable window
          ref.scrollableWindow.addEventListener("scroll", function () {
              if (options.pinnedRows > 0) ref.scrollableColumns.scrollLeft = ref.scrollableWindow.scrollLeft;
              if (options.pinnedCols > 0) ref.scrollableRows.scrollTop = ref.scrollableWindow.scrollTop;
          });
  
          var pinnedColsHeight = ref.corner != null ? getHeight(containerDiv) - getHeight(ref.corner) : 0;
          if (ref.scrollableColumns != null && pinnedColsHeight != 0 && pinnedColsHeight < getHeight(ref.scrollableColumns)) ref.scrollableColumns.style.height = pinnedColsHeight + "px";
  
          // adjust scrollable columns frame to scrollable window with scroll bar width
          if (ref.scrollableColumns != null) {
              var scrollbarWidth = ref.scrollableWindow.offsetWidth - ref.scrollableWindow.clientWidth;
              ref.scrollableColumns.style.width = (width - scrollbarWidth) + "px";
          }
  
          // adjust scrollable rows frame to scrollable window with scroll bar height
          if (ref.scrollableRows != null) {
              var scrollbarHeight = ref.scrollableWindow.offsetHeight - ref.scrollableWindow.clientHeight;
              ref.scrollableRows.style.height = (height - scrollbarHeight) + "px";
          }
  
          if (options.removeOriginal == true) {
              element.style.display = "none";
          }
  
          return ref;
      }
  }());