123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307 |
- import ElCheckbox from 'element-ui/packages/checkbox';
- import ElTag from 'element-ui/packages/tag';
- import Vue from 'vue';
- import FilterPanel from './filter-panel.vue';
- export default {
- name: 'el-table-header',
- render(h) {
- return (
- <table
- class="el-table__header"
- cellspacing="0"
- cellpadding="0"
- border="0">
- {
- this._l(this.columns, column =>
- <colgroup
- name={ column.id }
- width={ column.realWidth || column.width }
- />)
- }
- {
- !this.fixed && this.layout.gutterWidth
- ? <colgroup name="gutter" width={ this.layout.scrollY ? this.layout.gutterWidth : '' }></colgroup>
- : ''
- }
- <thead>
- <tr>
- {
- this._l(this.columns, (column, cellIndex) =>
- <th
- on-mousemove={ ($event) => this.handleMouseMove($event, column) }
- on-mouseout={ this.handleMouseOut }
- on-mousedown={ ($event) => this.handleMouseDown($event, column) }
- class={ [column.id, column.order, column.align, this.isCellHidden(cellIndex) ? 'hidden' : ''] }>
- <div class={ ['cell', column.filteredValue && column.filteredValue.length > 0 ? 'highlight' : ''] }>
- {
- column.headerTemplate
- ? column.headerTemplate.call(this._renderProxy, h, column.label)
- : column.label
- }
- {
- column.sortable
- ? <span class="caret-wrapper" on-click={ ($event) => this.handleHeaderClick($event, column) }>
- <i class="sort-caret ascending"></i>
- <i class="sort-caret descending"></i>
- </span>
- : ''
- }
- {
- column.filterable
- ? <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>
- : ''
- }
- </div>
- </th>
- )
- }
- {
- !this.fixed && this.layout.gutterWidth
- ? <th class="gutter" style={{ width: this.layout.scrollY ? this.layout.gutterWidth + 'px' : '0' }}></th>
- : ''
- }
- </tr>
- </thead>
- </table>
- );
- },
- props: {
- fixed: String,
- store: {
- required: true
- },
- layout: {
- required: true
- },
- border: Boolean
- },
- components: {
- ElCheckbox,
- ElTag
- },
- computed: {
- isAllSelected() {
- return this.store.states.isAllSelected;
- },
- columnsCount() {
- return this.store.states.columns.length;
- },
- leftFixedCount() {
- return this.store.states.fixedColumns.length;
- },
- rightFixedCount() {
- return this.store.states.rightFixedColumns.length;
- },
- columns() {
- return this.store.states.columns;
- }
- },
- created() {
- this.filterPanels = {};
- },
- beforeDestroy() {
- const panels = this.filterPanels;
- for (let prop in panels) {
- if (panels.hasOwnProperty(prop) && panels[prop]) {
- panels[prop].$destroy(true);
- }
- }
- },
- methods: {
- isCellHidden(index) {
- if (this.fixed === true || this.fixed === 'left') {
- return index >= this.leftFixedCount;
- } else if (this.fixed === 'right') {
- return index < this.columnsCount - this.rightFixedCount;
- } else {
- return (index < this.leftFixedCount) || (index >= this.columnsCount - this.rightFixedCount);
- }
- },
- toggleAllSelection() {
- this.store.commit('toggleAllSelection');
- },
- handleFilterClick(event, column) {
- event.stopPropagation();
- const target = event.target;
- const cell = target.parentNode;
- const table = this.$parent;
- let filterPanel = this.filterPanels[column.id];
- if (filterPanel && column.filterOpened) {
- filterPanel.showPopper = false;
- return;
- }
- if (!filterPanel) {
- filterPanel = new Vue(FilterPanel);
- this.filterPanels[column.id] = filterPanel;
- filterPanel.table = table;
- filterPanel.cell = cell;
- filterPanel.column = column;
- filterPanel.$mount(document.createElement('div'));
- }
- setTimeout(() => {
- filterPanel.showPopper = true;
- }, 16);
- },
- handleMouseDown(event, column) {
- if (this.draggingColumn && this.border) {
- this.dragging = true;
- this.$parent.resizeProxyVisible = true;
- const tableEl = this.$parent.$el;
- const tableLeft = tableEl.getBoundingClientRect().left;
- const columnEl = this.$el.querySelector(`th.${column.id}`);
- const columnRect = columnEl.getBoundingClientRect();
- const minLeft = columnRect.left - tableLeft + 30;
- columnEl.classList.add('noclick');
- this.dragState = {
- startMouseLeft: event.clientX,
- startLeft: columnRect.right - tableLeft,
- startColumnLeft: columnRect.left - tableLeft,
- tableLeft
- };
- const resizeProxy = this.$parent.$refs.resizeProxy;
- resizeProxy.style.left = this.dragState.startLeft + 'px';
- document.onselectstart = function() { return false; };
- document.ondragstart = function() { return false; };
- const handleMouseMove = (event) => {
- const deltaLeft = event.clientX - this.dragState.startMouseLeft;
- const proxyLeft = this.dragState.startLeft + deltaLeft;
- resizeProxy.style.left = Math.max(minLeft, proxyLeft) + 'px';
- };
- const handleMouseUp = () => {
- if (this.dragging) {
- const finalLeft = parseInt(resizeProxy.style.left, 10);
- const columnWidth = finalLeft - this.dragState.startColumnLeft;
- column.width = column.realWidth = columnWidth;
- this.store.scheduleLayout();
- document.body.style.cursor = '';
- this.dragging = false;
- this.draggingColumn = null;
- this.dragState = {};
- this.$parent.resizeProxyVisible = false;
- }
- document.removeEventListener('mousemove', handleMouseMove);
- document.removeEventListener('mouseup', handleMouseUp);
- document.onselectstart = null;
- document.ondragstart = null;
- setTimeout(function() {
- columnEl.classList.remove('noclick');
- }, 0);
- };
- document.addEventListener('mousemove', handleMouseMove);
- document.addEventListener('mouseup', handleMouseUp);
- }
- },
- handleMouseMove(event, column) {
- let target = event.target;
- while (target && target.tagName !== 'TH') {
- target = target.parentNode;
- }
- if (!column || !column.resizable) return;
- if (!this.dragging && this.border) {
- let rect = target.getBoundingClientRect();
- var bodyStyle = document.body.style;
- if (rect.width > 12 && rect.right - event.pageX < 8) {
- bodyStyle.cursor = 'col-resize';
- this.draggingColumn = column;
- } else if (!this.dragging) {
- bodyStyle.cursor = '';
- this.draggingColumn = null;
- }
- }
- },
- handleMouseOut() {
- document.body.style.cursor = '';
- },
- handleHeaderClick(event, column) {
- let target = event.target;
- while (target && target.tagName !== 'TH') {
- target = target.parentNode;
- }
- if (target && target.tagName === 'TH') {
- if (target.classList.contains('noclick')) {
- target.classList.remove('noclick');
- return;
- }
- }
- if (!column.sortable) return;
- const states = this.store.states;
- let sortProp = states.sortProp;
- let sortOrder;
- const sortingColumn = states.sortingColumn;
- if (sortingColumn !== column) {
- if (sortingColumn) {
- sortingColumn.order = null;
- }
- states.sortingColumn = column;
- sortProp = column.property;
- }
- if (!column.order) {
- sortOrder = column.order = 'ascending';
- } else if (column.order === 'ascending') {
- sortOrder = column.order = 'descending';
- } else {
- sortOrder = column.order = null;
- states.sortingColumn = null;
- sortProp = null;
- }
- states.sortProp = sortProp;
- states.sortOrder = sortOrder;
- this.store.commit('changeSortCondition');
- }
- },
- data() {
- return {
- draggingColumn: null,
- dragging: false,
- dragState: {}
- };
- }
- };
|