iview/src/components/table/table.vue

317 lines
13 KiB
Vue
Raw Normal View History

<template>
2016-11-24 17:32:43 +08:00
<div :class="classes" :style="styles">
<div :class="[prefixCls + '-title']" v-if="showSlotHeader" v-el:title><slot name="header"></slot></div>
2016-11-25 17:01:27 +08:00
<div :class="[prefixCls + '-header']" v-if="showHeader" v-el:header @mousewheel="handleMouseWheel">
2016-11-24 15:27:46 +08:00
<table cellspacing="0" cellpadding="0" border="0" :style="tableStyle">
<colgroup>
2016-11-25 08:50:06 +08:00
<col v-for="column in cloneColumns" :width="setCellWidth(column, $index)">
</colgroup>
<thead
is="table-head"
2016-11-24 15:27:46 +08:00
:prefix-cls="prefixCls"
:clone-data.sync="cloneData"
2016-11-25 08:50:06 +08:00
:columns="cloneColumns"></thead>
</table>
</div>
2016-11-25 17:01:27 +08:00
<div :class="[prefixCls + '-body']" :style="bodyStyle" @scroll="handleBodyScroll">
2016-11-24 15:27:46 +08:00
<table cellspacing="0" cellpadding="0" border="0" :style="tableStyle" v-el:tbody>
<colgroup>
2016-11-25 08:50:06 +08:00
<col v-for="column in cloneColumns" :width="setCellWidth(column, $index)">
</colgroup>
<tbody :class="[prefixCls + '-tbody']" v-el:render>
2016-11-24 15:27:46 +08:00
<tr
v-for="(index, row) in data"
2016-11-25 08:50:06 +08:00
:class="[prefixCls + '-row', rowClsName(index), {[prefixCls + '-row-highlight']: cloneData[index] && cloneData[index]._isHighlight, [prefixCls + '-row-hover']: cloneData[index] && cloneData[index]._isHover}]"
@mouseenter.stop="handleMouseIn(index)"
@mouseleave.stop="handleMouseOut(index)"
2016-11-24 15:27:46 +08:00
@click.stop="highlightCurrentRow(index)">
2016-11-25 08:50:06 +08:00
<td v-for="column in cloneColumns" :class="alignCls(column)">
2016-11-25 15:47:28 +08:00
<div :class="[prefixCls + '-cell']" v-if="column.type === 'selection'">
<Checkbox :checked="cloneData[index] && cloneData[index]._isChecked" @on-change="toggleSelect(index)"></Checkbox>
2016-11-24 15:27:46 +08:00
</div>
2016-11-25 15:47:28 +08:00
<Cell v-else :prefix-cls="prefixCls" :row="row" :column="column" :index="index"></Cell>
<!--<div :class="[prefixCls + '-cell']" v-else>-->
<!--{{{ renderRow(row, column, index) }}}-->
<!--</div>-->
<!--<div :class="[prefixCls + '-cell']">-->
<!--<template v-if="column.type === 'selection'">-->
<!--<Checkbox :checked="cloneData[index] && cloneData[index]._isChecked" @on-change="toggleSelect(index)"></Checkbox>-->
<!--</template>-->
<!--<template v-else>{{{ renderRow(row, column, index) }}}</template>-->
<!--</div>-->
2016-11-24 15:27:46 +08:00
</td>
</tr>
</tbody>
</table>
2016-11-25 08:50:06 +08:00
</div>
<div :class="[prefixCls + '-fixed']">
</div>
<div :class="[prefixCls + '-fixed-right']">
</div>
2016-11-24 17:32:43 +08:00
<div :class="[prefixCls + '-footer']" v-if="showSlotFooter" v-el:footer><slot name="footer"></slot></div>
</div>
</template>
<script>
import TableHead from './table-head.vue';
2016-11-24 15:27:46 +08:00
import Checkbox from '../checkbox/checkbox.vue';
2016-11-25 15:47:28 +08:00
import Cell from './cell.vue';
2016-11-24 15:27:46 +08:00
import Mixin from './mixin';
import { oneOf, getStyle, deepCopy } from '../../utils/assist';
const prefixCls = 'ivu-table';
export default {
2016-11-24 15:27:46 +08:00
mixins: [ Mixin ],
2016-11-25 15:47:28 +08:00
components: { TableHead, Checkbox, Cell },
props: {
data: {
type: Array,
default () {
return []
}
},
columns: {
type: Array,
default () {
return []
}
},
size: {
validator (value) {
return oneOf(value, ['small', 'large']);
}
},
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 {
tableWidth: 0,
columnsWidth: [],
prefixCls: prefixCls,
2016-11-24 15:27:46 +08:00
compiledUids: [],
2016-11-24 17:32:43 +08:00
cloneData: deepCopy(this.data),
2016-11-25 08:50:06 +08:00
cloneColumns: deepCopy(this.columns),
2016-11-25 15:47:28 +08:00
leftFixedColumns: [],
rightFixedColumns: [],
centerColumns: [],
2016-11-24 17:32:43 +08:00
showSlotHeader: true,
showSlotFooter: true,
bodyHeight: 0
}
},
computed: {
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`;
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
},
bodyStyle () {
let style = {};
if (this.bodyHeight !== 0) style.height = `${this.bodyHeight}px`;
return style;
}
},
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
const $td = this.$els.tbody.querySelectorAll('tbody tr')[0].querySelectorAll('td');
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-25 08:50:06 +08:00
assignRow (index, obj) {
return Object.assign({}, this.cloneData[index], obj);
},
handleMouseIn (index) {
if (this.cloneData[index]._isHover) return;
const row = this.assignRow(index, {
_isHover: true
});
this.cloneData.$set(index, row);
},
handleMouseOut (index) {
const row = this.assignRow(index, {
_isHover: false
});
this.cloneData.$set(index, row);
},
2016-11-24 15:27:46 +08:00
highlightCurrentRow (index) {
if (!this.highlightRow || this.cloneData[index]._isHighlight) return;
let oldIndex = -1;
this.cloneData.forEach((item, index) => {
if (item._isHighlight) {
oldIndex = index;
item._isHighlight = false;
return true;
}
});
2016-11-25 08:50:06 +08:00
const row = this.assignRow(index, {
2016-11-24 15:27:46 +08:00
_isHighlight: true
});
this.cloneData.$set(index, row);
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);
},
getSelection () {
let selectionIndexes = [];
this.cloneData.forEach((data, index) => {
if (data._isChecked) selectionIndexes.push(index);
});
return JSON.parse(JSON.stringify(this.data.filter((data, index) => selectionIndexes.indexOf(index) > -1)));
},
toggleSelect (index) {
const status = !this.cloneData[index]._isChecked;
2016-11-25 08:50:06 +08:00
const row = this.assignRow(index, {
2016-11-24 15:27:46 +08:00
_isChecked: status
});
this.cloneData.$set(index, row);
const selection = this.getSelection();
if (status) {
this.$emit('on-select', selection, JSON.parse(JSON.stringify(this.data[index])));
}
this.$emit('on-selection-change', selection);
},
selectAll () {
this.$emit('on-select-all', this.getSelection());
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
},
parseColumns () {
let left = [];
let right = [];
let center = [];
this.cloneColumns.forEach((col) => {
if (col.fixed && col.fixed === 'left') {
left.push(col);
} else if (col.fixed && col.fixed === 'right') {
right.push(col);
} else {
center.push(col);
}
});
2016-11-25 15:47:28 +08:00
this.leftFixedColumns = left;
this.rightFixedColumns = right;
this.centerColumns = center;
2016-11-25 08:50:06 +08:00
this.cloneColumns = left.concat(center).concat(right);
2016-11-25 17:01:27 +08:00
},
handleBodyScroll (event) {
this.$els.header.scrollLeft = event.target.scrollLeft;
// todo 固定时上下滚动,固定的表头也滚动 scrollTop
},
handleMouseWheel () {
console.log(111)
}
},
2016-11-24 17:32:43 +08:00
compiled () {
2016-11-25 08:50:06 +08:00
this.parseColumns();
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, '') !== '';
},
ready () {
2016-11-25 15:47:28 +08:00
this.handleResize();
2016-11-24 17:32:43 +08:00
this.fixedHeader();
window.addEventListener('resize', this.handleResize, false);
},
beforeDestroy () {
window.removeEventListener('resize', this.handleResize, false);
},
watch: {
data: {
handler () {
2016-11-24 15:27:46 +08:00
this.cloneData = deepCopy(this.data);
2016-11-25 15:47:28 +08:00
this.handleResize();
},
deep: true
},
columns: {
handler () {
2016-11-25 08:50:06 +08:00
this.parseColumns();
2016-11-25 15:47:28 +08:00
this.handleResize();
},
deep: true
2016-11-24 17:32:43 +08:00
},
height () {
this.fixedHeader();
}
}
}
</script>