iview/src/components/table/table.vue

699 lines
29 KiB
Vue
Raw Normal View History

<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">
2017-03-09 14:11:22 +08:00
<div :class="[prefixCls + '-title']" v-if="showSlotHeader" ref="title"><slot name="header"></slot></div>
<div :class="[prefixCls + '-header']" v-if="showHeader" ref="header" @mousewheel="handleMouseWheel">
2016-11-27 20:43:54 +08:00
<table-head
2016-11-29 18:42:34 +08:00
:prefix-cls="prefixCls"
2017-03-09 14:11:22 +08:00
:styleObject="tableStyle"
2016-11-29 18:42:34 +08:00
:columns="cloneColumns"
:obj-data="objData"
:columns-width="columnsWidth"
2016-11-29 18:42:34 +08:00
:data="rebuildData"></table-head>
2016-11-27 20:43:54 +08:00
</div>
2017-03-09 14:11:22 +08:00
<div :class="[prefixCls + '-body']" :style="bodyStyle" ref="body" @scroll="handleBodyScroll"
v-show="!((!!localeNoDataText && (!data || data.length === 0)) || (!!localeNoFilteredDataText && (!rebuildData || rebuildData.length === 0)))">
2016-11-27 20:43:54 +08:00
<table-body
2017-03-09 14:11:22 +08:00
ref="tbody"
2016-11-29 18:42:34 +08:00
:prefix-cls="prefixCls"
2017-03-09 14:11:22 +08:00
:styleObject="tableStyle"
2016-11-29 18:42:34 +08:00
:columns="cloneColumns"
:data="rebuildData"
: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>
2017-01-13 01:53:34 -06:00
<div
:class="[prefixCls + '-tip']"
v-show="((!!localeNoDataText && (!data || data.length === 0)) || (!!localeNoFilteredDataText && (!rebuildData || rebuildData.length === 0)))">
2017-01-13 01:53:34 -06:00
<table cellspacing="0" cellpadding="0" border="0">
<tbody>
<tr>
<td :style="{ 'height': bodyStyle.height }">
<span v-html="localeNoDataText" v-if="!data || data.length === 0"></span>
<span v-html="localeNoFilteredDataText" v-else></span>
2017-01-13 01:53:34 -06:00
</td>
</tr>
</tbody>
</table>
</div>
<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"
2017-03-09 14:11:22 +08:00
:styleObject="fixedTableStyle"
2016-11-29 18:42:34 +08:00
:columns="leftFixedColumns"
:obj-data="objData"
: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>
2017-03-09 14:11:22 +08:00
<div :class="[prefixCls + '-fixed-body']" :style="fixedBodyStyle" ref="fixedBody">
2016-11-29 13:25:25 +08:00
<table-body
2016-11-30 13:17:55 +08:00
fixed="left"
2016-11-29 18:42:34 +08:00
:prefix-cls="prefixCls"
2017-03-09 14:11:22 +08:00
:styleObject="fixedTableStyle"
2016-11-29 18:42:34 +08:00
:columns="leftFixedColumns"
:data="rebuildData"
: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>
<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"
2017-03-09 14:11:22 +08:00
:styleObject="fixedRightTableStyle"
2016-11-29 18:42:34 +08:00
:columns="rightFixedColumns"
:obj-data="objData"
2017-03-09 14:11:22 +08:00
:columns-width="columnsWidth"
2016-11-29 18:42:34 +08:00
:data="rebuildData"></table-head>
2016-11-29 13:25:25 +08:00
</div>
2017-03-09 14:11:22 +08:00
<div :class="[prefixCls + '-fixed-body']" :style="fixedBodyStyle" ref="fixedRightBody">
2016-11-29 13:25:25 +08:00
<table-body
2016-11-30 13:17:55 +08:00
fixed="right"
2016-11-29 18:42:34 +08:00
:prefix-cls="prefixCls"
2017-03-09 14:11:22 +08:00
:styleObject="fixedRightTableStyle"
2016-11-29 18:42:34 +08:00
:columns="rightFixedColumns"
:data="rebuildData"
: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>
2017-03-09 14:11:22 +08:00
<div :class="[prefixCls + '-footer']" v-if="showSlotFooter" ref="footer"><slot name="footer"></slot></div>
</div>
</div>
</template>
<script>
2016-11-26 08:57:09 +08:00
import tableHead from './table-head.vue';
import tableBody from './table-body.vue';
2017-01-05 13:15:26 +08:00
import { oneOf, getStyle, deepCopy, getScrollBarSize } from '../../utils/assist';
import Csv from '../../utils/csv';
import ExportCsv from './export-csv';
import Locale from '../../mixins/locale';
const prefixCls = 'ivu-table';
export default {
2017-03-09 14:11:22 +08:00
name: 'Table',
mixins: [ Locale ],
2016-11-26 08:57:09 +08:00
components: { tableHead, tableBody },
props: {
data: {
type: Array,
default () {
2016-12-25 22:49:42 +08:00
return [];
}
},
columns: {
type: Array,
default () {
2016-12-25 22:49:42 +08:00
return [];
}
},
size: {
validator (value) {
return oneOf(value, ['small', 'large', 'default']);
}
},
2016-11-25 22:51:50 +08:00
width: {
type: [Number, String]
},
2016-11-24 17:32:43 +08:00
height: {
type: [Number, String]
},
stripe: {
type: Boolean,
default: false
},
border: {
type: Boolean,
default: false
},
showHeader: {
type: Boolean,
default: true
},
2016-11-24 15:27:46 +08:00
highlightRow: {
type: Boolean,
default: false
2016-11-24 17:32:43 +08:00
},
rowClassName: {
type: Function,
default () {
return '';
}
},
context: {
type: Object
},
noDataText: {
type: String
},
noFilteredDataText: {
type: String
}
},
data () {
return {
2016-11-29 11:04:48 +08:00
ready: false,
tableWidth: 0,
columnsWidth: {},
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,
2017-01-05 13:15:26 +08:00
bodyHeight: 0,
bodyRealHeight: 0,
2017-03-09 14:11:22 +08:00
scrollBarWidth: getScrollBarSize(),
currentContext: this.context,
cloneData: deepCopy(this.data) // when Cell has a button to delete row data, clickCurrentRow will throw an error, so clone a data
2016-12-25 22:49:42 +08:00
};
},
computed: {
localeNoDataText () {
if (this.noDataText === undefined) {
return this.t('i.table.noDataText');
} else {
return this.noDataText;
}
},
localeNoFilteredDataText () {
if (this.noFilteredDataText === undefined) {
return this.t('i.table.noFilteredDataText');
} else {
return this.noFilteredDataText;
}
},
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-12-25 22:49:42 +08:00
];
2016-11-29 13:25:25 +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-12-25 22:49:42 +08:00
];
2016-11-24 15:27:46 +08:00
},
2016-11-24 17:32:43 +08:00
styles () {
let style = {};
2017-01-05 13:15:26 +08:00
if (this.height) {
const height = (this.isLeftFixed || this.isRightFixed) ? parseInt(this.height) + this.scrollBarWidth : parseInt(this.height);
style.height = `${height}px`;
}
2016-12-25 22:49:42 +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 = {};
2017-01-05 13:15:26 +08:00
if (this.tableWidth !== 0) {
let width = '';
if (this.bodyHeight === 0) {
width = this.tableWidth;
} else {
if (this.bodyHeight > this.bodyRealHeight) {
width = this.tableWidth;
} else {
width = this.tableWidth - this.scrollBarWidth;
}
}
// const width = this.bodyHeight === 0 ? this.tableWidth : this.tableWidth - this.scrollBarWidth;
2017-01-05 13:15:26 +08:00
style.width = `${width}px`;
}
2016-11-24 15:27:46 +08:00
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) => {
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) => {
if (col.fixed && col.fixed === 'right') width += col._width;
2016-11-30 13:17:55 +08:00
});
2017-01-05 13:15:26 +08:00
width += this.scrollBarWidth;
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 = {};
2017-01-05 13:15:26 +08:00
if (this.bodyHeight !== 0) {
// add a height to resolve scroll bug when browser has a scrollBar in fixed type and height prop
const height = (this.isLeftFixed || this.isRightFixed) ? this.bodyHeight + this.scrollBarWidth : this.bodyHeight;
style.height = `${height}px`;
}
2016-11-24 17:32:43 +08:00
return style;
2016-11-27 20:43:54 +08:00
},
fixedBodyStyle () {
let style = {};
2017-01-05 13:15:26 +08:00
if (this.bodyHeight !== 0) {
let height = this.bodyHeight + this.scrollBarWidth - 1;
if (this.width && this.width < this.tableWidth){
height = this.bodyHeight;
}
// style.height = this.scrollBarWidth > 0 ? `${this.bodyHeight}px` : `${this.bodyHeight - 1}px`;
style.height = this.scrollBarWidth > 0 ? `${height}px` : `${height - 1}px`;
2017-01-05 13:15:26 +08:00
}
2016-11-27 20:43:54 +08:00
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);
},
isLeftFixed () {
return this.columns.some(col => col.fixed && col.fixed === 'left');
},
isRightFixed () {
return this.columns.some(col => col.fixed && col.fixed === 'right');
}
},
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 () {
this.$nextTick(() => {
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;
}
this.columnsWidth = {};
2016-11-25 15:47:28 +08:00
this.$nextTick(() => {
let columnsWidth = {};
2016-11-25 17:01:27 +08:00
let autoWidthIndex = -1;
if (allWidth) autoWidthIndex = this.cloneColumns.findIndex(cell => !cell.width);//todo 这行可能有问题
2016-11-25 17:01:27 +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];
let width = parseInt(getStyle($td[i], 'width'));
if (i === autoWidthIndex) {
width = parseInt(getStyle($td[i], 'width')) - 1;
}
if (column.width) width = column.width;
this.cloneColumns[i]._width = width;
columnsWidth[column._index] = {
width: width
2016-12-25 22:49:42 +08:00
};
}
this.columnsWidth = columnsWidth;
2016-11-25 17:01:27 +08:00
}
});
// get table real height,for fixed when set height prop,but height < table's height,show scrollBarWidth
this.bodyRealHeight = parseInt(getStyle(this.$refs.tbody.$el, 'height'));
});
},
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.cloneData[oldIndex]));
this.$emit('on-current-change', JSON.parse(JSON.stringify(this.cloneData[_index])), oldData);
2016-11-24 15:27:46 +08:00
},
clickCurrentRow (_index) {
this.highlightCurrentRow (_index);
this.$emit('on-row-click', JSON.parse(JSON.stringify(this.cloneData[_index])));
},
dblclickCurrentRow (_index) {
this.highlightCurrentRow (_index);
this.$emit('on-row-dblclick', JSON.parse(JSON.stringify(this.cloneData[_index])));
},
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) {
// this.rebuildData.forEach((data) => {
// if(this.objData[data._index]._isDisabled){
// this.objData[data._index]._isChecked = false;
// }else{
// this.objData[data._index]._isChecked = status;
// }
// });
for(const data of this.rebuildData){
if(this.objData[data._index]._isDisabled){
continue;
}else{
this.objData[data._index]._isChecked = status;
}
}
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 () {
2016-12-25 22:49:42 +08:00
if (this.height) {
2016-11-24 17:32:43 +08:00
this.$nextTick(() => {
2017-03-09 14:11:22 +08:00
const titleHeight = parseInt(getStyle(this.$refs.title, 'height')) || 0;
const headerHeight = parseInt(getStyle(this.$refs.header, 'height')) || 0;
const footerHeight = parseInt(getStyle(this.$refs.footer, 'height')) || 0;
2016-11-24 17:32:43 +08:00
this.bodyHeight = this.height - titleHeight - headerHeight - footerHeight;
2016-12-25 22:49:42 +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) {
2017-03-09 14:11:22 +08:00
if (this.showHeader) this.$refs.header.scrollLeft = event.target.scrollLeft;
if (this.isLeftFixed) this.$refs.fixedBody.scrollTop = event.target.scrollTop;
if (this.isRightFixed) this.$refs.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;
2017-03-09 14:11:22 +08:00
const $body = this.$refs.body;
2016-11-25 22:51:50 +08:00
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[key], b[key], type);
2016-11-29 15:35:06 +08:00
} else {
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;
if(newRow._disabled){
newRow._isDisabled = newRow._disabled;
}else{
newRow._isDisabled = false;
}
2017-02-20 09:53:10 +08:00
if (newRow._checked) {
2017-02-28 15:10:07 +08:00
newRow._isChecked = newRow._checked;
2017-02-20 09:53:10 +08:00
} else {
newRow._isChecked = false;
}
2017-02-20 09:53:10 +08:00
if (newRow._highlight) {
newRow._isHighlight = newRow._highlight;
} else {
newRow._isHighlight = false;
}
2016-11-28 15:43:19 +08:00
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;
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);
},
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);
}
},
2017-03-09 14:11:22 +08:00
created () {
if (!this.context) this.currentContext = this.$parent;
2017-03-09 14:11:22 +08:00
this.showSlotHeader = this.$refs.title !== undefined;
this.showSlotFooter = this.$refs.footer !== undefined;
2016-11-30 13:17:55 +08:00
this.rebuildData = this.makeDataWithSortAndFilter();
2016-11-24 17:32:43 +08:00
},
2017-03-09 14:11:22 +08:00
mounted () {
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);
window.addEventListener('resize', this.handleResize, false);
},
beforeDestroy () {
window.removeEventListener('resize', this.handleResize, false);
},
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();
// here will trigger before clickCurrentRow, so use async
setTimeout(() => {
this.cloneData = deepCopy(this.data);
}, 0);
},
deep: true
},
columns: {
handler () {
// 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();
},
deep: true
2016-11-24 17:32:43 +08:00
},
height () {
this.fixedHeader();
}
}
2016-12-25 22:49:42 +08:00
};
</script>