header.js 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. window.checkData = [];
  2. window.sort=require('../utilities/sort');
  3. window.Header = function getHeader(object) {
  4. let _targetDom = object.el ? document.getElementById(object.el) : document.getElementsByTagName("table")[0];//基点元素
  5. let _columns = object.column; //列名
  6. let _fixedTop = object.fixedTop || 0; //同级浮动元素高度,使当前元素追加该元素高度浮动
  7. let _isCheckAllBox = object.isCheckAllBox || true;//是否开启全选框
  8. let _data = object.data || []; //被排序数据
  9. let _restorationColumn = object.restorationColumn || 'id'; //恢复原数据基准字段
  10. let _is_restorationColumn_asc = object.is_restorationColumn_asc || false; //恢复原数据基准字段的排序类型 true:asc false:desc
  11. let _before = object.before;
  12. let sortType = {};
  13. let columnArr = [];
  14. let table = document.createElement("table");
  15. function getTargetChildNode(dom) {
  16. if (!dom || dom.tagName==='INPUT')return dom;
  17. return getTargetChildNode(dom.firstElementChild);
  18. }
  19. function createHeaderBefore() {
  20. let tr = document.createElement("tr");
  21. _before.forEach(be=>{
  22. let th = document.createElement("th");
  23. if (be.colspan)th.colSpan = be.colspan;
  24. if (be.font){
  25. let font = document.createElement("span");
  26. font.className = be.font;
  27. th.appendChild(font);
  28. }
  29. if (be.value)th.appendChild(document.createTextNode(be.value));
  30. if (be.class)th.className = be.class;
  31. tr.appendChild(th);
  32. });
  33. table.appendChild(tr);
  34. }
  35. function createHeader() {
  36. let div = document.createElement("div");
  37. div.style.position = "sticky";
  38. div.style.position = "-webkit-sticky";
  39. div.style.top = _fixedTop+"px";
  40. table.style.backgroundColor = "white";
  41. div.style.zIndex = "999";
  42. table.className = "table table-bordered mb-0 text-center";
  43. let tr = document.createElement("tr");
  44. let firstTr = _targetDom.getElementsByTagName("tr")[0];
  45. let tds = [];
  46. if (!firstTr){
  47. tr.className = "text-nowrap";
  48. }else tds = firstTr.children;
  49. if (_isCheckAllBox){
  50. let th = document.createElement("th");
  51. if (firstTr) th.style.minWidth = tds[0].offsetWidth+"px";
  52. th.className = "text-left";
  53. let check = document.createElement("input");
  54. check.type = "checkbox";
  55. check.id = "checkAll";
  56. let trs = _targetDom.children;
  57. if (firstTr){
  58. while(trs[0].tagName !== 'TR')trs = trs[0].children;
  59. check.onchange = function () {
  60. if (event.target.checked){
  61. for (let i=0;i<trs.length;i++){
  62. let checkbox = getTargetChildNode(trs[i].children[0]);
  63. if (checkbox && !checkbox.checked){
  64. checkData.push(checkbox.value);
  65. checkbox.checked = true;
  66. }
  67. }
  68. }else{
  69. checkData = [];
  70. for (let i=0;i<trs.length;i++){
  71. let checkbox = getTargetChildNode(trs[i].children[0]);
  72. if (checkbox && checkbox.checked)checkbox.checked = false;
  73. }
  74. }
  75. };
  76. for (let i=0;i<trs.length;i++){
  77. let checkbox = getTargetChildNode(trs[i].children[0]);
  78. if (checkbox) checkbox.onchange = function () {
  79. if (event.target.checked)checkData.push(checkbox.value);
  80. else checkData.splice(checkData.indexOf(checkbox.value),1);
  81. if (checkData.length === _data.length && !check.checked)check.checked=true;
  82. if (checkData.length !== _data.length && check.checked) check.checked=false;
  83. }
  84. }
  85. }
  86. th.appendChild(check);
  87. tr.appendChild(th);
  88. }
  89. let i = (_isCheckAllBox && firstTr) ? 1 : 0;
  90. for (i;i<(tds.length>0 ? tds.length : _columns.length);i++){
  91. let th = document.createElement("th");
  92. if (firstTr) th.style.minWidth = (tds[i].offsetWidth-0.4)+"px";
  93. let column = _columns[(_isCheckAllBox && firstTr) ? i-1 : i];
  94. if (_columns && column){
  95. switch (column.type) {
  96. case "multi":
  97. th = multiColumn(th,column);
  98. break;
  99. default:
  100. th = defaultColumn(th,column);
  101. }
  102. }
  103. tr.appendChild(th);
  104. }
  105. table.appendChild(tr);
  106. div.appendChild(table);
  107. _targetDom.parentNode.insertBefore(div,_targetDom);
  108. }
  109. function multiColumn(th,column) {
  110. if (column.title){
  111. let div = document.createElement("div");
  112. div.className="w-100 text-center";
  113. div.appendChild(document.createTextNode(column.title));
  114. th.appendChild(div);
  115. }
  116. let div = document.createElement("div");
  117. div.className="row text-center";
  118. if (column.rows){
  119. column.rows.forEach(row=>{
  120. let node = document.createElement("div");
  121. node.className = "col-"+(row.col ? row.col : 1);
  122. node.appendChild(document.createTextNode(row.value));
  123. div.appendChild(node);
  124. });
  125. th.appendChild(div);
  126. }
  127. return th;
  128. }
  129. function defaultColumn(th,column) {
  130. if (column.style)for (let key in column.style)if (column.style.hasOwnProperty(key)) th.style[key] = column.style[key];
  131. if (column.class)th.className = column.class;
  132. let span = document.createElement("span");
  133. span.style.display = "inline-block";
  134. if (!column.neglect){
  135. th.style.cursor = "pointer";
  136. let font = document.createElement("span");
  137. font.className = "fa fa-sort";
  138. span.appendChild(font);
  139. th.onclick = rule(column,font);
  140. }
  141. span.appendChild(document.createTextNode((column.value ? column.value : '')));
  142. th.appendChild(span);
  143. return th;
  144. }
  145. //点击事件触发的排序规则
  146. function rule(column,columnSort) {
  147. return function () {
  148. if (!sortType[column.name]){
  149. sortType[column.name] = 'asc';
  150. columnArr.push(column.name);
  151. columnSort.className = "fa fa-sort-asc";
  152. let columnArrTemp = [];
  153. columnArrTemp.push.apply(columnArrTemp,columnArr);
  154. window.sort.sort(_data,columnArrTemp,sortType);
  155. return ;
  156. }
  157. if (sortType[column.name] === 'asc'){
  158. sortType[column.name] = 'desc';
  159. columnSort.className = "fa fa-sort-desc";
  160. let columnArrTemp = [];
  161. columnArrTemp.push.apply(columnArrTemp,columnArr);
  162. window.sort.sort(_data,columnArrTemp,sortType);
  163. return ;
  164. }
  165. if (sortType[column.name] === 'desc'){
  166. delete sortType[column.name];
  167. columnArr.some(function (name, index) {
  168. if (name === column.name){
  169. columnArr.splice(index,1);
  170. return true;
  171. }
  172. });
  173. columnSort.className = "fa fa-sort";
  174. if (columnArr.length === 0){
  175. //希尔排序
  176. let arr = [];
  177. arr.push.apply(arr,_data);
  178. let len = arr.length;
  179. for(let gap = Math.floor(len / 2); gap > 0; gap = Math.floor(gap / 2)) {
  180. for(let i = gap; i < len; i++) {
  181. let j = i;
  182. let current = arr[i];
  183. if (_is_restorationColumn_asc){
  184. while(j - gap >= 0 && Number(current[_restorationColumn]) < Number(arr[j - gap][_restorationColumn])) {
  185. arr[j] = arr[j - gap];
  186. j = j - gap;
  187. }
  188. }else{
  189. while(j - gap >= 0 && Number(current[_restorationColumn]) > Number(arr[j - gap][_restorationColumn])) {
  190. arr[j] = arr[j - gap];
  191. j = j - gap;
  192. }
  193. }
  194. arr[j] = current;
  195. }
  196. }
  197. _data.length = 0;
  198. _data.push.apply(_data,arr);
  199. return;
  200. }
  201. let columnArrTemp = [];
  202. columnArrTemp.push.apply(columnArrTemp,columnArr);
  203. window.sort.sort(_data,columnArrTemp,sortType);
  204. }
  205. }
  206. }
  207. //初始化
  208. this.init = function() {
  209. if (_before)createHeaderBefore();
  210. createHeader();
  211. };
  212. };