iview/src/components/table/table.vue

507 lines
20 KiB
Vue
Raw Normal View History

<template>
2016-11-29 13:25:25 +08:00
<div :class="wrapClasses" :style="styles">
<div :class="classes" :style="styles">
<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 13:25:25 +08:00
:prefix-cls="prefixCls"
:style="tableStyle"
:columns="cloneColumns"
2016-11-29 16:29:52 +08:00
:obj-data="objData"
: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 13:25:25 +08:00
v-ref:tbody
:prefix-cls="prefixCls"
:style="tableStyle"
:columns="cloneColumns"
:data="rebuildData"
:obj-data="objData"></table-body>
2016-11-27 20:43:54 +08:00
</div>
2016-11-29 13:25:25 +08:00
<div :class="[prefixCls + '-fixed']">
<div :class="[prefixCls + '-fixed-header']" v-if="showHeader">
<table-head
fixed
:prefix-cls="prefixCls"
:style="fixedTableStyle"
:columns="leftFixedColumns"
2016-11-29 16:29:52 +08:00
:obj-data="objData"
: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
fixed
:prefix-cls="prefixCls"
:style="fixedTableStyle"
:columns="leftFixedColumns"
:data="rebuildData"
:obj-data="objData"></table-body>
</div>
2016-11-27 20:43:54 +08:00
</div>
2016-11-29 13:25:25 +08:00
<div :class="[prefixCls + '-fixed-right']">
<div :class="[prefixCls + '-fixed-header']" v-if="showHeader">
<table-head
fixed
:prefix-cls="prefixCls"
:style="fixedRightTableStyle"
:columns="rightFixedColumns"
2016-11-29 16:29:52 +08:00
:obj-data="objData"
: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
fixed
:prefix-cls="prefixCls"
:style="fixedRightTableStyle"
:columns="rightFixedColumns"
:data="rebuildData"
:obj-data="objData"></table-body>
</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>
</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';
const prefixCls = 'ivu-table';
export default {
2016-11-26 08:57:09 +08:00
components: { tableHead, tableBody },
props: {
data: {
type: Array,
default () {
return []
}
},
columns: {
type: Array,
default () {
return []
}
},
size: {
validator (value) {
return oneOf(value, ['small', 'large']);
}
},
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 '';
}
}
},
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-28 14:23:49 +08:00
rebuildData: this.makeData(), // 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
}
},
computed: {
2016-11-29 13:25:25 +08:00
wrapClasses () {
return [
`${prefixCls}-wrapper`,
{
[`${prefixCls}-hide`]: !this.ready
}
]
},
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-header`]: this.showSlotHeader,
[`${prefixCls}-with-footer`]: this.showSlotFooter,
[`${prefixCls}-with-fixed-top`]: !!this.height
}
]
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 = {};
if (this.leftFixedColumns.length) style.width = this.leftFixedColumns.reduce((a, b) => a + b);
return style;
},
fixedRightTableStyle () {
let style = {};
if (this.rightFixedColumns.length) style.width = this.rightFixedColumns.reduce((a, b) => a + b);
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 = [];
this.cloneColumns.forEach((col) => {
if (col.fixed && col.fixed === 'left') {
left.push(col);
}
});
return left;
},
rightFixedColumns () {
let right = [];
this.cloneColumns.forEach((col) => {
if (col.fixed && col.fixed === 'right') {
right.push(col);
}
});
return 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(() => {
2016-11-25 15:47:28 +08:00
const allWidth = !this.columns.some(cell => !cell.width);
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-25 15:47:28 +08:00
this.$nextTick(() => {
this.columnsWidth = [];
2016-11-25 17:01:27 +08:00
let autoWidthIndex = -1;
2016-11-25 15:47:28 +08:00
if (allWidth) autoWidthIndex = this.cloneColumns.findIndex(cell => !cell.width);
2016-11-25 17:01:27 +08:00
2016-11-26 08:57:09 +08:00
const $td = this.$refs.tbody.$el.querySelectorAll('tbody tr')[0].querySelectorAll('td');
2016-11-25 17:01:27 +08:00
for (let i = 0; i < $td.length; i++) { // can not use forEach in Firefox
if (i === autoWidthIndex) {
this.columnsWidth.push(parseInt(getStyle($td[i], 'width')) - 1);
2016-11-25 15:47:28 +08:00
} else {
2016-11-25 17:01:27 +08:00
this.columnsWidth.push(parseInt(getStyle($td[i], 'width')));
}
2016-11-25 17:01:27 +08:00
}
});
});
},
setCellWidth (column, index) {
return column.width ? column.width : this.columnsWidth[index];
2016-11-24 15:27:46 +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 = {};
let index = -1;
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
index = i;
}
}
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-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;
if (this.leftFixedColumns.length) this.$els.fixedBody.scrollTop = event.target.scrollTop;
if (this.rightFixedColumns.length) 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 {
return type === 'asc' ? a[key] > b[key] : a[key] < b[key];
}
});
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;
for (let i = 0; i < this.cloneColumns.length; i++) {
if (this.cloneColumns[i]._sortType !== 'normal') {
sortType = this.cloneColumns[i]._sortType;
sortIndex = i;
break;
}
}
if (sortType !== 'normal') data = this.sortData(data, sortType, sortIndex);
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-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-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-11-24 17:32:43 +08:00
compiled () {
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, '') !== '';
},
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);
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();
},
deep: true
},
columns: {
handler () {
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();
}
}
}
</script>