2016-11-23 16:27:17 +08:00
|
|
|
<template>
|
2016-11-29 13:25:25 +08:00
|
|
|
<div :class="wrapClasses" :style="styles">
|
2016-11-29 18:42:34 +08:00
|
|
|
<div :class="classes">
|
2016-11-29 13:25:25 +08:00
|
|
|
<div :class="[prefixCls + '-title']" v-if="showSlotHeader" v-el:title><slot name="header"></slot></div>
|
|
|
|
<div :class="[prefixCls + '-header']" v-if="showHeader" v-el:header @mousewheel="handleMouseWheel">
|
2016-11-27 20:43:54 +08:00
|
|
|
<table-head
|
2016-11-29 18:42:34 +08:00
|
|
|
:prefix-cls="prefixCls"
|
|
|
|
:style="tableStyle"
|
|
|
|
:columns="cloneColumns"
|
|
|
|
:obj-data="objData"
|
2016-11-30 16:12:10 +08:00
|
|
|
:columns-width="columnsWidth"
|
2016-11-29 18:42:34 +08:00
|
|
|
:data="rebuildData"></table-head>
|
2016-11-27 20:43:54 +08:00
|
|
|
</div>
|
2016-11-29 13:25:25 +08:00
|
|
|
<div :class="[prefixCls + '-body']" :style="bodyStyle" v-el:body @scroll="handleBodyScroll">
|
2016-11-27 20:43:54 +08:00
|
|
|
<table-body
|
2016-11-29 18:42:34 +08:00
|
|
|
v-ref:tbody
|
|
|
|
:prefix-cls="prefixCls"
|
|
|
|
:style="tableStyle"
|
|
|
|
:columns="cloneColumns"
|
|
|
|
:data="rebuildData"
|
2016-11-30 16:12:10 +08:00
|
|
|
:columns-width="columnsWidth"
|
2016-11-29 18:42:34 +08:00
|
|
|
:obj-data="objData"></table-body>
|
2016-11-27 20:43:54 +08:00
|
|
|
</div>
|
2016-12-01 11:14:26 +08:00
|
|
|
<div :class="[prefixCls + '-fixed']" :style="fixedTableStyle" v-if="isLeftFixed">
|
2016-11-29 13:25:25 +08:00
|
|
|
<div :class="[prefixCls + '-fixed-header']" v-if="showHeader">
|
|
|
|
<table-head
|
2016-11-30 13:17:55 +08:00
|
|
|
fixed="left"
|
2016-11-29 18:42:34 +08:00
|
|
|
:prefix-cls="prefixCls"
|
|
|
|
:style="fixedTableStyle"
|
|
|
|
:columns="leftFixedColumns"
|
|
|
|
:obj-data="objData"
|
2016-11-30 14:56:32 +08:00
|
|
|
:columns-width.sync="columnsWidth"
|
2016-11-29 18:42:34 +08:00
|
|
|
:data="rebuildData"></table-head>
|
2016-11-29 13:25:25 +08:00
|
|
|
</div>
|
|
|
|
<div :class="[prefixCls + '-fixed-body']" :style="fixedBodyStyle" v-el:fixed-body>
|
|
|
|
<table-body
|
2016-11-30 13:17:55 +08:00
|
|
|
fixed="left"
|
2016-11-29 18:42:34 +08:00
|
|
|
:prefix-cls="prefixCls"
|
|
|
|
:style="fixedTableStyle"
|
|
|
|
:columns="leftFixedColumns"
|
|
|
|
:data="rebuildData"
|
2016-11-30 16:12:10 +08:00
|
|
|
:columns-width="columnsWidth"
|
2016-11-29 18:42:34 +08:00
|
|
|
:obj-data="objData"></table-body>
|
2016-11-29 13:25:25 +08:00
|
|
|
</div>
|
2016-11-27 20:43:54 +08:00
|
|
|
</div>
|
2016-12-01 11:14:26 +08:00
|
|
|
<div :class="[prefixCls + '-fixed-right']" :style="fixedRightTableStyle" v-if="isRightFixed">
|
2016-11-29 13:25:25 +08:00
|
|
|
<div :class="[prefixCls + '-fixed-header']" v-if="showHeader">
|
|
|
|
<table-head
|
2016-11-30 13:17:55 +08:00
|
|
|
fixed="right"
|
2016-11-29 18:42:34 +08:00
|
|
|
:prefix-cls="prefixCls"
|
|
|
|
:style="fixedRightTableStyle"
|
|
|
|
:columns="rightFixedColumns"
|
|
|
|
:obj-data="objData"
|
2016-11-30 14:56:32 +08:00
|
|
|
:columns-width.sync="columnsWidth"
|
2016-11-29 18:42:34 +08:00
|
|
|
:data="rebuildData"></table-head>
|
2016-11-29 13:25:25 +08:00
|
|
|
</div>
|
|
|
|
<div :class="[prefixCls + '-fixed-body']" :style="fixedBodyStyle" v-el:fixed-right-body>
|
|
|
|
<table-body
|
2016-11-30 13:17:55 +08:00
|
|
|
fixed="right"
|
2016-11-29 18:42:34 +08:00
|
|
|
:prefix-cls="prefixCls"
|
|
|
|
:style="fixedRightTableStyle"
|
|
|
|
:columns="rightFixedColumns"
|
|
|
|
:data="rebuildData"
|
2016-11-30 16:12:10 +08:00
|
|
|
:columns-width="columnsWidth"
|
2016-11-29 18:42:34 +08:00
|
|
|
:obj-data="objData"></table-body>
|
2016-11-29 13:25:25 +08:00
|
|
|
</div>
|
2016-11-27 20:43:54 +08:00
|
|
|
</div>
|
2016-11-29 13:25:25 +08:00
|
|
|
<div :class="[prefixCls + '-footer']" v-if="showSlotFooter" v-el:footer><slot name="footer"></slot></div>
|
2016-11-23 16:27:17 +08:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</template>
|
|
|
|
<script>
|
2016-11-26 08:57:09 +08:00
|
|
|
import tableHead from './table-head.vue';
|
|
|
|
import tableBody from './table-body.vue';
|
2016-11-24 15:27:46 +08:00
|
|
|
import { oneOf, getStyle, deepCopy } from '../../utils/assist';
|
2016-12-09 13:24:39 +08:00
|
|
|
import Csv from '../../utils/csv';
|
|
|
|
import ExportCsv from './export-csv';
|
2016-11-23 16:27:17 +08:00
|
|
|
const prefixCls = 'ivu-table';
|
|
|
|
|
|
|
|
export default {
|
2016-11-26 08:57:09 +08:00
|
|
|
components: { tableHead, tableBody },
|
2016-11-23 16:27:17 +08:00
|
|
|
props: {
|
|
|
|
data: {
|
|
|
|
type: Array,
|
|
|
|
default () {
|
|
|
|
return []
|
|
|
|
}
|
|
|
|
},
|
|
|
|
columns: {
|
|
|
|
type: Array,
|
|
|
|
default () {
|
|
|
|
return []
|
|
|
|
}
|
|
|
|
},
|
|
|
|
size: {
|
|
|
|
validator (value) {
|
2016-12-02 11:23:46 +08:00
|
|
|
return oneOf(value, ['small', 'large', 'default']);
|
2016-11-23 16:27:17 +08:00
|
|
|
}
|
|
|
|
},
|
2016-11-25 22:51:50 +08:00
|
|
|
width: {
|
|
|
|
type: [Number, String]
|
|
|
|
},
|
2016-11-24 17:32:43 +08:00
|
|
|
height: {
|
|
|
|
type: [Number, String]
|
|
|
|
},
|
2016-11-23 16:27:17 +08:00
|
|
|
stripe: {
|
|
|
|
type: Boolean,
|
|
|
|
default: false
|
|
|
|
},
|
|
|
|
border: {
|
|
|
|
type: Boolean,
|
|
|
|
default: false
|
|
|
|
},
|
|
|
|
showHeader: {
|
|
|
|
type: Boolean,
|
|
|
|
default: true
|
|
|
|
},
|
2016-11-24 15:27:46 +08:00
|
|
|
highlightRow: {
|
2016-11-23 16:27:17 +08:00
|
|
|
type: Boolean,
|
|
|
|
default: false
|
2016-11-24 17:32:43 +08:00
|
|
|
},
|
|
|
|
rowClassName: {
|
|
|
|
type: Function,
|
|
|
|
default () {
|
|
|
|
return '';
|
|
|
|
}
|
2016-12-01 11:02:06 +08:00
|
|
|
},
|
|
|
|
content: {
|
|
|
|
type: Object
|
2016-11-23 16:27:17 +08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
data () {
|
|
|
|
return {
|
2016-11-29 11:04:48 +08:00
|
|
|
ready: false,
|
2016-11-23 18:38:39 +08:00
|
|
|
tableWidth: 0,
|
2016-11-30 16:12:10 +08:00
|
|
|
columnsWidth: {},
|
2016-11-23 16:27:17 +08:00
|
|
|
prefixCls: prefixCls,
|
2016-11-24 15:27:46 +08:00
|
|
|
compiledUids: [],
|
2016-11-28 16:53:19 +08:00
|
|
|
objData: this.makeObjData(), // checkbox or highlight-row
|
2016-11-30 13:17:55 +08:00
|
|
|
rebuildData: [], // for sort or filter
|
2016-11-28 16:53:19 +08:00
|
|
|
cloneColumns: this.makeColumns(),
|
2016-11-24 17:32:43 +08:00
|
|
|
showSlotHeader: true,
|
|
|
|
showSlotFooter: true,
|
|
|
|
bodyHeight: 0
|
2016-11-23 16:27:17 +08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
computed: {
|
2016-11-29 13:25:25 +08:00
|
|
|
wrapClasses () {
|
|
|
|
return [
|
|
|
|
`${prefixCls}-wrapper`,
|
|
|
|
{
|
2016-12-02 15:15:54 +08:00
|
|
|
[`${prefixCls}-hide`]: !this.ready,
|
|
|
|
[`${prefixCls}-with-header`]: this.showSlotHeader,
|
|
|
|
[`${prefixCls}-with-footer`]: this.showSlotFooter
|
2016-11-29 13:25:25 +08:00
|
|
|
}
|
|
|
|
]
|
|
|
|
},
|
2016-11-23 16:27:17 +08:00
|
|
|
classes () {
|
|
|
|
return [
|
|
|
|
`${prefixCls}`,
|
|
|
|
{
|
2016-11-24 15:27:46 +08:00
|
|
|
[`${prefixCls}-${this.size}`]: !!this.size,
|
|
|
|
[`${prefixCls}-border`]: this.border,
|
2016-11-24 17:32:43 +08:00
|
|
|
[`${prefixCls}-stripe`]: this.stripe,
|
|
|
|
[`${prefixCls}-with-fixed-top`]: !!this.height
|
2016-11-23 16:27:17 +08:00
|
|
|
}
|
|
|
|
]
|
2016-11-24 15:27:46 +08:00
|
|
|
},
|
2016-11-24 17:32:43 +08:00
|
|
|
styles () {
|
|
|
|
let style = {};
|
|
|
|
if (!!this.height) style.height = `${this.height}px`;
|
2016-11-25 22:51:50 +08:00
|
|
|
if (!!this.width) style.width = `${this.width}px`;
|
2016-11-24 17:32:43 +08:00
|
|
|
return style;
|
|
|
|
},
|
2016-11-24 15:27:46 +08:00
|
|
|
tableStyle () {
|
|
|
|
let style = {};
|
|
|
|
if (this.tableWidth !== 0) style.width = `${this.tableWidth}px`;
|
|
|
|
return style;
|
2016-11-24 17:32:43 +08:00
|
|
|
},
|
2016-11-26 08:57:09 +08:00
|
|
|
fixedTableStyle () {
|
|
|
|
let style = {};
|
2016-11-30 13:17:55 +08:00
|
|
|
let width = 0;
|
|
|
|
this.leftFixedColumns.forEach((col) => {
|
2016-11-30 16:12:10 +08:00
|
|
|
if (col.fixed && col.fixed === 'left') width += col._width;
|
2016-11-30 13:17:55 +08:00
|
|
|
});
|
|
|
|
style.width = `${width}px`;
|
2016-11-26 08:57:09 +08:00
|
|
|
return style;
|
|
|
|
},
|
|
|
|
fixedRightTableStyle () {
|
|
|
|
let style = {};
|
2016-11-30 13:17:55 +08:00
|
|
|
let width = 0;
|
|
|
|
this.rightFixedColumns.forEach((col) => {
|
2016-11-30 16:12:10 +08:00
|
|
|
if (col.fixed && col.fixed === 'right') width += col._width;
|
2016-11-30 13:17:55 +08:00
|
|
|
});
|
|
|
|
style.width = `${width}px`;
|
2016-11-26 08:57:09 +08:00
|
|
|
return style;
|
|
|
|
},
|
2016-11-24 17:32:43 +08:00
|
|
|
bodyStyle () {
|
|
|
|
let style = {};
|
|
|
|
if (this.bodyHeight !== 0) style.height = `${this.bodyHeight}px`;
|
|
|
|
return style;
|
2016-11-27 20:43:54 +08:00
|
|
|
},
|
|
|
|
fixedBodyStyle () {
|
|
|
|
let style = {};
|
|
|
|
if (this.bodyHeight !== 0) style.height = `${this.bodyHeight - 1}px`;
|
|
|
|
return style;
|
2016-11-28 16:53:19 +08:00
|
|
|
},
|
|
|
|
leftFixedColumns () {
|
|
|
|
let left = [];
|
2016-11-30 13:17:55 +08:00
|
|
|
let other = [];
|
2016-11-28 16:53:19 +08:00
|
|
|
this.cloneColumns.forEach((col) => {
|
|
|
|
if (col.fixed && col.fixed === 'left') {
|
|
|
|
left.push(col);
|
2016-11-30 13:17:55 +08:00
|
|
|
} else {
|
|
|
|
other.push(col);
|
2016-11-28 16:53:19 +08:00
|
|
|
}
|
|
|
|
});
|
2016-11-30 13:17:55 +08:00
|
|
|
return left.concat(other);
|
2016-11-28 16:53:19 +08:00
|
|
|
},
|
|
|
|
rightFixedColumns () {
|
|
|
|
let right = [];
|
2016-11-30 13:17:55 +08:00
|
|
|
let other = [];
|
2016-11-28 16:53:19 +08:00
|
|
|
this.cloneColumns.forEach((col) => {
|
|
|
|
if (col.fixed && col.fixed === 'right') {
|
|
|
|
right.push(col);
|
2016-11-30 13:17:55 +08:00
|
|
|
} else {
|
|
|
|
other.push(col);
|
2016-11-28 16:53:19 +08:00
|
|
|
}
|
|
|
|
});
|
2016-11-30 13:17:55 +08:00
|
|
|
return right.concat(other);
|
2016-12-01 11:14:26 +08:00
|
|
|
},
|
|
|
|
isLeftFixed () {
|
|
|
|
return this.columns.some(col => col.fixed && col.fixed === 'left');
|
|
|
|
},
|
|
|
|
isRightFixed () {
|
|
|
|
return this.columns.some(col => col.fixed && col.fixed === 'right');
|
2016-11-23 16:27:17 +08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
methods: {
|
2016-11-24 17:32:43 +08:00
|
|
|
rowClsName (index) {
|
|
|
|
return this.rowClassName(this.data[index], index);
|
|
|
|
},
|
2016-11-25 15:47:28 +08:00
|
|
|
handleResize () {
|
2016-11-23 16:27:17 +08:00
|
|
|
this.$nextTick(() => {
|
2016-11-30 16:12:10 +08:00
|
|
|
const allWidth = !this.columns.some(cell => !cell.width); // each column set a width
|
2016-11-25 15:47:28 +08:00
|
|
|
if (allWidth) {
|
|
|
|
this.tableWidth = this.columns.map(cell => cell.width).reduce((a, b) => a + b);
|
|
|
|
} else {
|
|
|
|
this.tableWidth = parseInt(getStyle(this.$el, 'width')) - 1;
|
2016-11-23 16:27:17 +08:00
|
|
|
}
|
2016-11-30 16:12:10 +08:00
|
|
|
this.columnsWidth = {};
|
2016-11-25 15:47:28 +08:00
|
|
|
this.$nextTick(() => {
|
2016-11-30 16:12:10 +08:00
|
|
|
let columnsWidth = {};
|
2016-11-25 17:01:27 +08:00
|
|
|
let autoWidthIndex = -1;
|
2016-11-30 16:12:10 +08:00
|
|
|
if (allWidth) autoWidthIndex = this.cloneColumns.findIndex(cell => !cell.width);//todo 这行可能有问题
|
2016-11-25 17:01:27 +08:00
|
|
|
|
2016-12-01 11:02:06 +08:00
|
|
|
if (this.data.length) {
|
|
|
|
const $td = this.$refs.tbody.$el.querySelectorAll('tbody tr')[0].querySelectorAll('td');
|
|
|
|
for (let i = 0; i < $td.length; i++) { // can not use forEach in Firefox
|
|
|
|
const column = this.cloneColumns[i];
|
2016-11-30 16:12:10 +08:00
|
|
|
|
2016-12-01 11:02:06 +08:00
|
|
|
let width = parseInt(getStyle($td[i], 'width'));
|
|
|
|
if (i === autoWidthIndex) {
|
|
|
|
width = parseInt(getStyle($td[i], 'width')) - 1;
|
|
|
|
}
|
|
|
|
if (column.width) width = column.width;
|
2016-11-30 16:12:10 +08:00
|
|
|
|
2016-12-01 11:02:06 +08:00
|
|
|
this.cloneColumns[i]._width = width;
|
2016-11-30 16:12:10 +08:00
|
|
|
|
2016-12-01 11:02:06 +08:00
|
|
|
columnsWidth[column._index] = {
|
|
|
|
width: width
|
|
|
|
}
|
2016-11-23 16:27:17 +08:00
|
|
|
}
|
2016-12-01 11:02:06 +08:00
|
|
|
this.columnsWidth = columnsWidth;
|
2016-11-25 17:01:27 +08:00
|
|
|
}
|
2016-11-23 18:38:39 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
2016-11-28 15:43:19 +08:00
|
|
|
handleMouseIn (_index) {
|
|
|
|
if (this.objData[_index]._isHover) return;
|
|
|
|
this.objData[_index]._isHover = true;
|
2016-11-25 08:50:06 +08:00
|
|
|
},
|
2016-11-28 15:43:19 +08:00
|
|
|
handleMouseOut (_index) {
|
|
|
|
this.objData[_index]._isHover = false;
|
2016-11-25 08:50:06 +08:00
|
|
|
},
|
2016-11-28 15:43:19 +08:00
|
|
|
highlightCurrentRow (_index) {
|
|
|
|
if (!this.highlightRow || this.objData[_index]._isHighlight) return;
|
2016-11-24 15:27:46 +08:00
|
|
|
|
|
|
|
let oldIndex = -1;
|
2016-11-28 15:43:19 +08:00
|
|
|
for (let i in this.objData) {
|
|
|
|
if (this.objData[i]._isHighlight) {
|
|
|
|
oldIndex = parseInt(i);
|
|
|
|
this.objData[i]._isHighlight = false;
|
2016-11-24 15:27:46 +08:00
|
|
|
}
|
2016-11-28 15:43:19 +08:00
|
|
|
}
|
|
|
|
this.objData[_index]._isHighlight = true;
|
|
|
|
const oldData = oldIndex < 0 ? null : JSON.parse(JSON.stringify(this.data[oldIndex]));
|
|
|
|
this.$emit('on-current-change', JSON.parse(JSON.stringify(this.data[_index])), oldData);
|
2016-11-24 15:27:46 +08:00
|
|
|
},
|
|
|
|
getSelection () {
|
|
|
|
let selectionIndexes = [];
|
2016-11-28 15:43:19 +08:00
|
|
|
for (let i in this.objData) {
|
|
|
|
if (this.objData[i]._isChecked) selectionIndexes.push(parseInt(i));
|
|
|
|
}
|
2016-11-24 15:27:46 +08:00
|
|
|
return JSON.parse(JSON.stringify(this.data.filter((data, index) => selectionIndexes.indexOf(index) > -1)));
|
|
|
|
},
|
2016-11-28 15:43:19 +08:00
|
|
|
toggleSelect (_index) {
|
2016-11-28 14:23:49 +08:00
|
|
|
let data = {};
|
2016-11-28 15:43:19 +08:00
|
|
|
|
|
|
|
for (let i in this.objData) {
|
|
|
|
if (parseInt(i) === _index) {
|
|
|
|
data = this.objData[i];
|
2016-11-28 14:23:49 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
const status = !data._isChecked;
|
2016-11-28 15:43:19 +08:00
|
|
|
|
|
|
|
this.objData[_index]._isChecked = status;
|
2016-11-24 15:27:46 +08:00
|
|
|
|
|
|
|
const selection = this.getSelection();
|
|
|
|
if (status) {
|
2016-11-28 14:23:49 +08:00
|
|
|
this.$emit('on-select', selection, JSON.parse(JSON.stringify(this.data[_index])));
|
2016-11-24 15:27:46 +08:00
|
|
|
}
|
|
|
|
this.$emit('on-selection-change', selection);
|
|
|
|
},
|
2016-11-26 10:50:24 +08:00
|
|
|
selectAll (status) {
|
2016-11-29 16:29:52 +08:00
|
|
|
this.rebuildData.forEach((data) => {
|
|
|
|
this.objData[data._index]._isChecked = status;
|
|
|
|
});
|
2016-11-26 10:50:24 +08:00
|
|
|
|
2016-11-27 22:56:05 +08:00
|
|
|
const selection = this.getSelection();
|
2016-11-26 10:50:24 +08:00
|
|
|
if (status) {
|
2016-11-27 22:56:05 +08:00
|
|
|
this.$emit('on-select-all', selection);
|
2016-11-26 10:50:24 +08:00
|
|
|
}
|
2016-11-27 22:56:05 +08:00
|
|
|
this.$emit('on-selection-change', selection);
|
2016-11-24 17:32:43 +08:00
|
|
|
},
|
|
|
|
fixedHeader () {
|
|
|
|
if (!!this.height) {
|
|
|
|
this.$nextTick(() => {
|
|
|
|
const titleHeight = parseInt(getStyle(this.$els.title, 'height')) || 0;
|
|
|
|
const headerHeight = parseInt(getStyle(this.$els.header, 'height')) || 0;
|
|
|
|
const footerHeight = parseInt(getStyle(this.$els.footer, 'height')) || 0;
|
|
|
|
this.bodyHeight = this.height - titleHeight - headerHeight - footerHeight;
|
|
|
|
})
|
2016-12-02 11:23:46 +08:00
|
|
|
} else {
|
|
|
|
this.bodyHeight = 0;
|
2016-11-24 17:32:43 +08:00
|
|
|
}
|
2016-11-25 08:50:06 +08:00
|
|
|
},
|
2016-11-29 08:43:47 +08:00
|
|
|
hideColumnFilter () {
|
|
|
|
this.cloneColumns.forEach((col) => col._filterVisible = false);
|
|
|
|
},
|
2016-11-25 17:01:27 +08:00
|
|
|
handleBodyScroll (event) {
|
2016-11-27 20:43:54 +08:00
|
|
|
if (this.showHeader) this.$els.header.scrollLeft = event.target.scrollLeft;
|
2016-12-01 11:14:26 +08:00
|
|
|
if (this.isLeftFixed) this.$els.fixedBody.scrollTop = event.target.scrollTop;
|
|
|
|
if (this.isRightFixed) this.$els.fixedRightBody.scrollTop = event.target.scrollTop;
|
2016-11-29 08:43:47 +08:00
|
|
|
this.hideColumnFilter();
|
2016-11-25 17:01:27 +08:00
|
|
|
},
|
2016-11-25 22:51:50 +08:00
|
|
|
handleMouseWheel (event) {
|
|
|
|
const deltaX = event.deltaX;
|
|
|
|
const $body = this.$els.body;
|
|
|
|
|
|
|
|
if (deltaX > 0) {
|
|
|
|
$body.scrollLeft = $body.scrollLeft + 10;
|
|
|
|
} else {
|
|
|
|
$body.scrollLeft = $body.scrollLeft - 10;
|
|
|
|
}
|
2016-11-27 22:56:05 +08:00
|
|
|
},
|
2016-11-29 15:35:06 +08:00
|
|
|
sortData (data, type, index) {
|
|
|
|
const key = this.cloneColumns[index].key;
|
|
|
|
data.sort((a, b) => {
|
|
|
|
if (this.cloneColumns[index].sortMethod) {
|
|
|
|
return this.cloneColumns[index].sortMethod(a, b);
|
|
|
|
} else {
|
2016-12-01 16:11:14 +08:00
|
|
|
if (type === 'asc') {
|
|
|
|
return a[key] > b[key] ? 1 : -1;
|
|
|
|
} else if (type === 'desc') {
|
|
|
|
return a[key] < b[key] ? 1 : -1;
|
|
|
|
}
|
2016-11-29 15:35:06 +08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
return data;
|
|
|
|
},
|
2016-11-27 22:56:05 +08:00
|
|
|
handleSort (index, type) {
|
2016-11-28 16:53:19 +08:00
|
|
|
this.cloneColumns.forEach((col) => col._sortType = 'normal');
|
|
|
|
|
|
|
|
const key = this.cloneColumns[index].key;
|
2016-11-28 21:30:59 +08:00
|
|
|
if (this.cloneColumns[index].sortable !== 'custom') { // custom is for remote sort
|
2016-11-29 15:35:06 +08:00
|
|
|
if (type === 'normal') {
|
2016-11-29 16:05:14 +08:00
|
|
|
this.rebuildData = this.makeDataWithFilter();
|
2016-11-29 15:35:06 +08:00
|
|
|
} else {
|
|
|
|
this.rebuildData = this.sortData(this.rebuildData, type, index);
|
2016-11-28 21:30:59 +08:00
|
|
|
}
|
2016-11-27 22:56:05 +08:00
|
|
|
}
|
2016-11-28 16:53:19 +08:00
|
|
|
this.cloneColumns[index]._sortType = type;
|
|
|
|
|
|
|
|
this.$emit('on-sort-change', {
|
|
|
|
column: JSON.parse(JSON.stringify(this.columns[this.cloneColumns[index]._index])),
|
|
|
|
key: key,
|
|
|
|
order: type
|
2016-11-29 15:35:06 +08:00
|
|
|
});
|
2016-11-28 14:23:49 +08:00
|
|
|
},
|
2016-11-29 11:04:48 +08:00
|
|
|
handleFilterHide (index) { // clear checked that not filter now
|
|
|
|
if (!this.cloneColumns[index]._isFiltered) this.cloneColumns[index]._filterChecked = [];
|
|
|
|
},
|
2016-11-29 14:55:25 +08:00
|
|
|
filterData (data, column) {
|
|
|
|
return data.filter((row) => {
|
|
|
|
let status = !column._filterChecked.length;
|
2016-11-29 11:04:48 +08:00
|
|
|
for (let i = 0; i < column._filterChecked.length; i++) {
|
|
|
|
status = column.filterMethod(column._filterChecked[i], row);
|
|
|
|
if (status) break;
|
|
|
|
}
|
|
|
|
return status;
|
|
|
|
});
|
2016-11-29 14:55:25 +08:00
|
|
|
},
|
|
|
|
filterOtherData (data, index) {
|
|
|
|
this.cloneColumns.forEach((col, colIndex) => {
|
|
|
|
if (colIndex !== index) {
|
|
|
|
data = this.filterData(data, col);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return data;
|
|
|
|
},
|
|
|
|
handleFilter (index) {
|
|
|
|
const column = this.cloneColumns[index];
|
2016-11-29 15:35:06 +08:00
|
|
|
let filterData = this.makeDataWithSort();
|
2016-11-29 14:55:25 +08:00
|
|
|
|
|
|
|
// filter others first, after filter this column
|
|
|
|
filterData = this.filterOtherData(filterData, index);
|
|
|
|
this.rebuildData = this.filterData(filterData, column);
|
|
|
|
|
2016-11-29 11:04:48 +08:00
|
|
|
this.cloneColumns[index]._isFiltered = true;
|
|
|
|
this.cloneColumns[index]._filterVisible = false;
|
|
|
|
},
|
2016-11-29 13:25:25 +08:00
|
|
|
handleFilterSelect (index, value) {
|
|
|
|
this.cloneColumns[index]._filterChecked = [value];
|
|
|
|
this.handleFilter(index);
|
|
|
|
},
|
2016-11-29 11:04:48 +08:00
|
|
|
handleFilterReset (index) {
|
|
|
|
this.cloneColumns[index]._isFiltered = false;
|
2016-11-29 13:25:25 +08:00
|
|
|
this.cloneColumns[index]._filterVisible = false;
|
|
|
|
this.cloneColumns[index]._filterChecked = [];
|
2016-11-29 14:55:25 +08:00
|
|
|
|
2016-11-29 15:35:06 +08:00
|
|
|
let filterData = this.makeDataWithSort();
|
2016-11-29 14:55:25 +08:00
|
|
|
filterData = this.filterOtherData(filterData, index);
|
|
|
|
this.rebuildData = filterData;
|
2016-11-29 11:04:48 +08:00
|
|
|
},
|
2016-11-28 14:23:49 +08:00
|
|
|
makeData () {
|
|
|
|
let data = deepCopy(this.data);
|
|
|
|
data.forEach((row, index) => row._index = index);
|
|
|
|
return data;
|
2016-11-28 15:43:19 +08:00
|
|
|
},
|
2016-11-29 15:35:06 +08:00
|
|
|
makeDataWithSort () {
|
|
|
|
let data = this.makeData();
|
|
|
|
let sortType = 'normal';
|
|
|
|
let sortIndex = -1;
|
2016-12-16 14:15:11 +08:00
|
|
|
let isCustom = false;
|
|
|
|
|
2016-11-29 15:35:06 +08:00
|
|
|
for (let i = 0; i < this.cloneColumns.length; i++) {
|
|
|
|
if (this.cloneColumns[i]._sortType !== 'normal') {
|
|
|
|
sortType = this.cloneColumns[i]._sortType;
|
|
|
|
sortIndex = i;
|
2016-12-16 14:15:11 +08:00
|
|
|
isCustom = this.cloneColumns[i].sortable === 'custom';
|
2016-11-29 15:35:06 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2016-12-16 14:15:11 +08:00
|
|
|
if (sortType !== 'normal' && !isCustom) data = this.sortData(data, sortType, sortIndex);
|
2016-11-29 15:35:06 +08:00
|
|
|
return data;
|
|
|
|
},
|
2016-11-29 16:05:14 +08:00
|
|
|
makeDataWithFilter () {
|
|
|
|
let data = this.makeData();
|
|
|
|
this.cloneColumns.forEach(col => data = this.filterData(data, col));
|
|
|
|
return data;
|
|
|
|
},
|
|
|
|
makeDataWithSortAndFilter () {
|
|
|
|
let data = this.makeDataWithSort();
|
|
|
|
this.cloneColumns.forEach(col => data = this.filterData(data, col));
|
|
|
|
return data;
|
|
|
|
},
|
2016-11-28 15:43:19 +08:00
|
|
|
makeObjData () {
|
|
|
|
let data = {};
|
|
|
|
this.data.forEach((row, index) => {
|
|
|
|
const newRow = deepCopy(row);// todo 直接替换
|
|
|
|
newRow._isHover = false;
|
|
|
|
newRow._isChecked = false;
|
|
|
|
newRow._isHighlight = false;
|
|
|
|
data[index] = newRow;
|
|
|
|
});
|
|
|
|
return data;
|
2016-11-28 16:53:19 +08:00
|
|
|
},
|
|
|
|
makeColumns () {
|
|
|
|
let columns = deepCopy(this.columns);
|
|
|
|
let left = [];
|
|
|
|
let right = [];
|
|
|
|
let center = [];
|
|
|
|
|
|
|
|
columns.forEach((column, index) => {
|
|
|
|
column._index = index;
|
2016-11-30 16:12:10 +08:00
|
|
|
column._width = column.width ? column.width : ''; // update in handleResize()
|
2016-11-29 08:43:47 +08:00
|
|
|
column._sortType = 'normal';
|
|
|
|
column._filterVisible = false;
|
|
|
|
column._isFiltered = false;
|
|
|
|
column._filterChecked = [];
|
2016-11-28 16:53:19 +08:00
|
|
|
|
2016-11-29 11:04:48 +08:00
|
|
|
if ('filterMultiple' in column) {
|
|
|
|
column._filterMultiple = column.filterMultiple;
|
|
|
|
} else {
|
|
|
|
column._filterMultiple = true;
|
|
|
|
}
|
2016-11-30 13:17:55 +08:00
|
|
|
if ('filteredValue' in column) {
|
|
|
|
column._filterChecked = column.filteredValue;
|
|
|
|
column._isFiltered = true;
|
|
|
|
}
|
2016-11-29 11:04:48 +08:00
|
|
|
|
2016-11-28 16:53:19 +08:00
|
|
|
if (column.fixed && column.fixed === 'left') {
|
|
|
|
left.push(column);
|
|
|
|
} else if (column.fixed && column.fixed === 'right') {
|
|
|
|
right.push(column);
|
|
|
|
} else {
|
|
|
|
center.push(column);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return left.concat(center).concat(right);
|
2016-12-09 13:24:39 +08:00
|
|
|
},
|
|
|
|
exportCsv (params) {
|
|
|
|
if (params.filename) {
|
|
|
|
if (params.filename.indexOf('.csv') === -1) {
|
|
|
|
params.filename += '.csv';
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
params.filename = 'table.csv';
|
|
|
|
}
|
|
|
|
|
|
|
|
let columns = [];
|
|
|
|
let datas = [];
|
|
|
|
if (params.columns && params.data) {
|
|
|
|
columns = params.columns;
|
|
|
|
datas = params.data;
|
|
|
|
} else {
|
|
|
|
columns = this.columns;
|
|
|
|
if (!('original' in params)) params.original = true;
|
|
|
|
datas = params.original ? this.data : this.rebuildData;
|
|
|
|
}
|
|
|
|
|
|
|
|
let noHeader = false;
|
|
|
|
if ('noHeader' in params) noHeader = params.noHeader;
|
|
|
|
|
|
|
|
const data = Csv(columns, datas, ',', noHeader);
|
|
|
|
ExportCsv.download(params.filename, data);
|
2016-11-23 16:27:17 +08:00
|
|
|
}
|
|
|
|
},
|
2016-11-24 17:32:43 +08:00
|
|
|
compiled () {
|
2016-12-01 11:02:06 +08:00
|
|
|
if (!this.content) this.content = this.$parent;
|
2016-11-24 17:32:43 +08:00
|
|
|
this.showSlotHeader = this.$els.title.innerHTML.replace(/\n/g, '').replace(/<!--[\w\W\r\n]*?-->/gmi, '') !== '';
|
|
|
|
this.showSlotFooter = this.$els.footer.innerHTML.replace(/\n/g, '').replace(/<!--[\w\W\r\n]*?-->/gmi, '') !== '';
|
2016-11-30 13:17:55 +08:00
|
|
|
this.rebuildData = this.makeDataWithSortAndFilter();
|
2016-11-24 17:32:43 +08:00
|
|
|
},
|
2016-11-23 16:27:17 +08:00
|
|
|
ready () {
|
2016-11-25 15:47:28 +08:00
|
|
|
this.handleResize();
|
2016-11-24 17:32:43 +08:00
|
|
|
this.fixedHeader();
|
2016-11-29 11:04:48 +08:00
|
|
|
this.$nextTick(() => this.ready = true);
|
2016-11-23 18:38:39 +08:00
|
|
|
window.addEventListener('resize', this.handleResize, false);
|
|
|
|
},
|
|
|
|
beforeDestroy () {
|
|
|
|
window.removeEventListener('resize', this.handleResize, false);
|
2016-11-23 16:27:17 +08:00
|
|
|
},
|
|
|
|
watch: {
|
|
|
|
data: {
|
|
|
|
handler () {
|
2016-11-28 15:43:19 +08:00
|
|
|
this.objData = this.makeObjData();
|
2016-11-29 16:05:14 +08:00
|
|
|
this.rebuildData = this.makeDataWithSortAndFilter();
|
2016-11-25 15:47:28 +08:00
|
|
|
this.handleResize();
|
2016-11-23 16:27:17 +08:00
|
|
|
},
|
|
|
|
deep: true
|
|
|
|
},
|
|
|
|
columns: {
|
|
|
|
handler () {
|
2016-12-02 11:23:46 +08:00
|
|
|
// todo 这里有性能问题,可能是左右固定计算属性影响的
|
2016-11-28 16:53:19 +08:00
|
|
|
this.cloneColumns = this.makeColumns();
|
2016-11-29 16:05:14 +08:00
|
|
|
this.rebuildData = this.makeDataWithSortAndFilter();
|
2016-11-25 15:47:28 +08:00
|
|
|
this.handleResize();
|
2016-11-23 16:27:17 +08:00
|
|
|
},
|
|
|
|
deep: true
|
2016-11-24 17:32:43 +08:00
|
|
|
},
|
|
|
|
height () {
|
|
|
|
this.fixedHeader();
|
2016-11-23 16:27:17 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
</script>
|