table-header.js 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. import ElCheckbox from 'element-ui/packages/checkbox';
  2. import ElTag from 'element-ui/packages/tag';
  3. import Vue from 'vue';
  4. import FilterPanel from './filter-panel.vue';
  5. export default {
  6. name: 'el-table-header',
  7. render(h) {
  8. return (
  9. <table
  10. class="el-table__header"
  11. cellspacing="0"
  12. cellpadding="0"
  13. border="0">
  14. {
  15. this._l(this.columns, column =>
  16. <colgroup
  17. name={ column.id }
  18. width={ column.realWidth || column.width }
  19. />)
  20. }
  21. {
  22. !this.fixed && this.layout.gutterWidth
  23. ? <colgroup name="gutter" width={ this.layout.scrollY ? this.layout.gutterWidth : '' }></colgroup>
  24. : ''
  25. }
  26. <thead>
  27. <tr>
  28. {
  29. this._l(this.columns, (column, cellIndex) =>
  30. <th
  31. on-mousemove={ ($event) => this.handleMouseMove($event, column) }
  32. on-mouseout={ this.handleMouseOut }
  33. on-mousedown={ ($event) => this.handleMouseDown($event, column) }
  34. class={ [column.id, column.order, column.align, this.isCellHidden(cellIndex) ? 'hidden' : ''] }>
  35. <div class={ ['cell', column.filteredValue && column.filteredValue.length > 0 ? 'highlight' : ''] }>
  36. {
  37. column.headerTemplate
  38. ? column.headerTemplate.call(this._renderProxy, h, column.label)
  39. : column.label
  40. }
  41. {
  42. column.sortable
  43. ? <span class="caret-wrapper" on-click={ ($event) => this.handleHeaderClick($event, column) }>
  44. <i class="sort-caret ascending"></i>
  45. <i class="sort-caret descending"></i>
  46. </span>
  47. : ''
  48. }
  49. {
  50. column.filterable
  51. ? <span class="el-table__column-filter-trigger" on-click={ ($event) => this.handleFilterClick($event, column) }><i class={ ['el-icon-arrow-down', column.filterOpened ? 'el-icon-arrow-up' : ''] }></i></span>
  52. : ''
  53. }
  54. </div>
  55. </th>
  56. )
  57. }
  58. {
  59. !this.fixed && this.layout.gutterWidth
  60. ? <th class="gutter" style={{ width: this.layout.scrollY ? this.layout.gutterWidth + 'px' : '0' }}></th>
  61. : ''
  62. }
  63. </tr>
  64. </thead>
  65. </table>
  66. );
  67. },
  68. props: {
  69. fixed: String,
  70. store: {
  71. required: true
  72. },
  73. layout: {
  74. required: true
  75. },
  76. border: Boolean
  77. },
  78. components: {
  79. ElCheckbox,
  80. ElTag
  81. },
  82. computed: {
  83. isAllSelected() {
  84. return this.store.states.isAllSelected;
  85. },
  86. columnsCount() {
  87. return this.store.states.columns.length;
  88. },
  89. leftFixedCount() {
  90. return this.store.states.fixedColumns.length;
  91. },
  92. rightFixedCount() {
  93. return this.store.states.rightFixedColumns.length;
  94. },
  95. columns() {
  96. return this.store.states.columns;
  97. }
  98. },
  99. created() {
  100. this.filterPanels = {};
  101. },
  102. beforeDestroy() {
  103. const panels = this.filterPanels;
  104. for (let prop in panels) {
  105. if (panels.hasOwnProperty(prop) && panels[prop]) {
  106. panels[prop].$destroy(true);
  107. }
  108. }
  109. },
  110. methods: {
  111. isCellHidden(index) {
  112. if (this.fixed === true || this.fixed === 'left') {
  113. return index >= this.leftFixedCount;
  114. } else if (this.fixed === 'right') {
  115. return index < this.columnsCount - this.rightFixedCount;
  116. } else {
  117. return (index < this.leftFixedCount) || (index >= this.columnsCount - this.rightFixedCount);
  118. }
  119. },
  120. toggleAllSelection() {
  121. this.store.commit('toggleAllSelection');
  122. },
  123. handleFilterClick(event, column) {
  124. event.stopPropagation();
  125. const target = event.target;
  126. const cell = target.parentNode;
  127. const table = this.$parent;
  128. let filterPanel = this.filterPanels[column.id];
  129. if (filterPanel && column.filterOpened) {
  130. filterPanel.showPopper = false;
  131. return;
  132. }
  133. if (!filterPanel) {
  134. filterPanel = new Vue(FilterPanel);
  135. this.filterPanels[column.id] = filterPanel;
  136. filterPanel.table = table;
  137. filterPanel.cell = cell;
  138. filterPanel.column = column;
  139. filterPanel.$mount(document.createElement('div'));
  140. }
  141. setTimeout(() => {
  142. filterPanel.showPopper = true;
  143. }, 16);
  144. },
  145. handleMouseDown(event, column) {
  146. if (this.draggingColumn && this.border) {
  147. this.dragging = true;
  148. this.$parent.resizeProxyVisible = true;
  149. const tableEl = this.$parent.$el;
  150. const tableLeft = tableEl.getBoundingClientRect().left;
  151. const columnEl = this.$el.querySelector(`th.${column.id}`);
  152. const columnRect = columnEl.getBoundingClientRect();
  153. const minLeft = columnRect.left - tableLeft + 30;
  154. columnEl.classList.add('noclick');
  155. this.dragState = {
  156. startMouseLeft: event.clientX,
  157. startLeft: columnRect.right - tableLeft,
  158. startColumnLeft: columnRect.left - tableLeft,
  159. tableLeft
  160. };
  161. const resizeProxy = this.$parent.$refs.resizeProxy;
  162. resizeProxy.style.left = this.dragState.startLeft + 'px';
  163. document.onselectstart = function() { return false; };
  164. document.ondragstart = function() { return false; };
  165. const handleMouseMove = (event) => {
  166. const deltaLeft = event.clientX - this.dragState.startMouseLeft;
  167. const proxyLeft = this.dragState.startLeft + deltaLeft;
  168. resizeProxy.style.left = Math.max(minLeft, proxyLeft) + 'px';
  169. };
  170. const handleMouseUp = () => {
  171. if (this.dragging) {
  172. const finalLeft = parseInt(resizeProxy.style.left, 10);
  173. const columnWidth = finalLeft - this.dragState.startColumnLeft;
  174. column.width = column.realWidth = columnWidth;
  175. this.store.scheduleLayout();
  176. document.body.style.cursor = '';
  177. this.dragging = false;
  178. this.draggingColumn = null;
  179. this.dragState = {};
  180. this.$parent.resizeProxyVisible = false;
  181. }
  182. document.removeEventListener('mousemove', handleMouseMove);
  183. document.removeEventListener('mouseup', handleMouseUp);
  184. document.onselectstart = null;
  185. document.ondragstart = null;
  186. setTimeout(function() {
  187. columnEl.classList.remove('noclick');
  188. }, 0);
  189. };
  190. document.addEventListener('mousemove', handleMouseMove);
  191. document.addEventListener('mouseup', handleMouseUp);
  192. }
  193. },
  194. handleMouseMove(event, column) {
  195. let target = event.target;
  196. while (target && target.tagName !== 'TH') {
  197. target = target.parentNode;
  198. }
  199. if (!column || !column.resizable) return;
  200. if (!this.dragging && this.border) {
  201. let rect = target.getBoundingClientRect();
  202. var bodyStyle = document.body.style;
  203. if (rect.width > 12 && rect.right - event.pageX < 8) {
  204. bodyStyle.cursor = 'col-resize';
  205. this.draggingColumn = column;
  206. } else if (!this.dragging) {
  207. bodyStyle.cursor = '';
  208. this.draggingColumn = null;
  209. }
  210. }
  211. },
  212. handleMouseOut() {
  213. document.body.style.cursor = '';
  214. },
  215. handleHeaderClick(event, column) {
  216. let target = event.target;
  217. while (target && target.tagName !== 'TH') {
  218. target = target.parentNode;
  219. }
  220. if (target && target.tagName === 'TH') {
  221. if (target.classList.contains('noclick')) {
  222. target.classList.remove('noclick');
  223. return;
  224. }
  225. }
  226. if (!column.sortable) return;
  227. const states = this.store.states;
  228. let sortProp = states.sortProp;
  229. let sortOrder;
  230. const sortingColumn = states.sortingColumn;
  231. if (sortingColumn !== column) {
  232. if (sortingColumn) {
  233. sortingColumn.order = null;
  234. }
  235. states.sortingColumn = column;
  236. sortProp = column.property;
  237. }
  238. if (!column.order) {
  239. sortOrder = column.order = 'ascending';
  240. } else if (column.order === 'ascending') {
  241. sortOrder = column.order = 'descending';
  242. } else {
  243. sortOrder = column.order = null;
  244. states.sortingColumn = null;
  245. sortProp = null;
  246. }
  247. states.sortProp = sortProp;
  248. states.sortOrder = sortOrder;
  249. this.store.commit('changeSortCondition');
  250. }
  251. },
  252. data() {
  253. return {
  254. draggingColumn: null,
  255. dragging: false,
  256. dragState: {}
  257. };
  258. }
  259. };