2016-11-12 18:57:08 +00:00
|
|
|
import { getValueByPath, getCell, getColumnByCell, getRowIdentity } from './util';
|
2016-08-16 08:07:18 +00:00
|
|
|
|
|
|
|
export default {
|
|
|
|
props: {
|
2016-10-12 11:18:34 +00:00
|
|
|
store: {
|
|
|
|
required: true
|
|
|
|
},
|
|
|
|
layout: {
|
|
|
|
required: true
|
|
|
|
},
|
|
|
|
rowClassName: [String, Function],
|
2016-11-04 08:15:33 +00:00
|
|
|
fixed: String,
|
|
|
|
highlight: Boolean
|
2016-08-16 08:07:18 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
render(h) {
|
|
|
|
return (
|
|
|
|
<table
|
|
|
|
class="el-table__body"
|
|
|
|
cellspacing="0"
|
|
|
|
cellpadding="0"
|
|
|
|
border="0">
|
2016-10-12 11:18:34 +00:00
|
|
|
{
|
|
|
|
this._l(this.columns, column =>
|
2016-11-01 12:32:11 +00:00
|
|
|
<col
|
2016-10-12 11:18:34 +00:00
|
|
|
name={ column.id }
|
|
|
|
width={ column.realWidth || column.width }
|
|
|
|
/>)
|
|
|
|
}
|
2016-08-16 08:07:18 +00:00
|
|
|
<tbody>
|
|
|
|
{
|
|
|
|
this._l(this.data, (row, $index) =>
|
|
|
|
<tr
|
2016-11-12 18:57:08 +00:00
|
|
|
key={ this.$parent.rowKey ? this.getKeyOfRow(row, $index) : $index }
|
2016-08-16 08:07:18 +00:00
|
|
|
on-click={ ($event) => this.handleClick($event, row) }
|
|
|
|
on-mouseenter={ _ => this.handleMouseEnter($index) }
|
2016-11-03 13:00:09 +00:00
|
|
|
on-mouseleave={ _ => this.handleMouseLeave() }
|
2016-10-12 11:18:34 +00:00
|
|
|
class={ this.getRowClass(row, $index) }>
|
2016-08-16 08:07:18 +00:00
|
|
|
{
|
2016-10-21 15:13:34 +00:00
|
|
|
this._l(this.columns, (column, cellIndex) =>
|
2016-08-16 08:07:18 +00:00
|
|
|
<td
|
2016-11-13 06:39:24 +00:00
|
|
|
class={ [column.id, column.align, column.className || '', this.isCellHidden(cellIndex) ? 'is-hidden' : '' ] }
|
2016-08-16 08:07:18 +00:00
|
|
|
on-mouseenter={ ($event) => this.handleCellMouseEnter($event, row) }
|
|
|
|
on-mouseleave={ this.handleCellMouseLeave }>
|
|
|
|
{
|
2016-11-12 18:31:04 +00:00
|
|
|
column.renderCell.call(this._renderProxy, h, { row, column, $index, store: this.store, _self: this.$parent.$vnode.context })
|
2016-08-16 08:07:18 +00:00
|
|
|
}
|
|
|
|
</td>
|
2016-10-12 11:18:34 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
{
|
|
|
|
!this.fixed && this.layout.scrollY && this.layout.gutterWidth ? <td class="gutter" /> : ''
|
2016-08-16 08:07:18 +00:00
|
|
|
}
|
|
|
|
</tr>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
</tbody>
|
|
|
|
</table>
|
|
|
|
);
|
|
|
|
},
|
|
|
|
|
2016-11-12 18:31:04 +00:00
|
|
|
watch: {
|
|
|
|
'store.states.hoverRow'(newVal, oldVal) {
|
|
|
|
const el = this.$el;
|
|
|
|
if (!el) return;
|
|
|
|
const rows = el.querySelectorAll('tr');
|
|
|
|
const oldRow = rows[oldVal];
|
|
|
|
const newRow = rows[newVal];
|
|
|
|
if (oldRow) {
|
|
|
|
oldRow.classList.remove('hover-row');
|
|
|
|
}
|
|
|
|
if (newRow) {
|
|
|
|
newRow.classList.add('hover-row');
|
|
|
|
}
|
|
|
|
},
|
|
|
|
'store.states.currentRow'(newVal, oldVal) {
|
|
|
|
if (!this.highlight) return;
|
|
|
|
const el = this.$el;
|
|
|
|
if (!el) return;
|
|
|
|
const data = this.store.states.data;
|
|
|
|
const rows = el.querySelectorAll('tr');
|
|
|
|
const oldRow = rows[data.indexOf(oldVal)];
|
|
|
|
const newRow = rows[data.indexOf(newVal)];
|
|
|
|
if (oldRow) {
|
|
|
|
oldRow.classList.remove('current-row');
|
|
|
|
}
|
|
|
|
if (newRow) {
|
|
|
|
newRow.classList.add('current-row');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2016-10-12 11:18:34 +00:00
|
|
|
computed: {
|
|
|
|
data() {
|
|
|
|
return this.store.states.data;
|
|
|
|
},
|
2016-10-21 15:13:34 +00:00
|
|
|
|
|
|
|
columnsCount() {
|
|
|
|
return this.store.states.columns.length;
|
|
|
|
},
|
|
|
|
|
|
|
|
leftFixedCount() {
|
|
|
|
return this.store.states.fixedColumns.length;
|
|
|
|
},
|
|
|
|
|
|
|
|
rightFixedCount() {
|
|
|
|
return this.store.states.rightFixedColumns.length;
|
2016-10-12 11:18:34 +00:00
|
|
|
},
|
2016-10-21 15:13:34 +00:00
|
|
|
|
2016-10-12 11:18:34 +00:00
|
|
|
columns() {
|
|
|
|
return this.store.states.columns;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2016-08-16 08:07:18 +00:00
|
|
|
data() {
|
|
|
|
return {
|
|
|
|
tooltipDisabled: true
|
|
|
|
};
|
|
|
|
},
|
|
|
|
|
|
|
|
methods: {
|
2016-11-12 18:57:08 +00:00
|
|
|
getKeyOfRow(row, index) {
|
|
|
|
const rowKey = this.$parent.rowKey;
|
|
|
|
if (rowKey) {
|
|
|
|
return getRowIdentity(row, rowKey);
|
|
|
|
}
|
|
|
|
return index;
|
|
|
|
},
|
|
|
|
|
2016-10-21 15:13:34 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2016-10-12 11:18:34 +00:00
|
|
|
getRowClass(row, index) {
|
|
|
|
const classes = [];
|
2016-09-02 07:41:52 +00:00
|
|
|
|
2016-10-12 11:18:34 +00:00
|
|
|
const rowClassName = this.rowClassName;
|
|
|
|
if (typeof rowClassName === 'string') {
|
|
|
|
classes.push(rowClassName);
|
|
|
|
} else if (typeof rowClassName === 'function') {
|
|
|
|
classes.push(rowClassName.apply(null, [row, index]) || '');
|
2016-08-26 06:01:14 +00:00
|
|
|
}
|
2016-10-12 11:18:34 +00:00
|
|
|
|
|
|
|
return classes.join(' ');
|
2016-08-26 06:01:14 +00:00
|
|
|
},
|
|
|
|
|
2016-08-16 08:07:18 +00:00
|
|
|
handleCellMouseEnter(event, row) {
|
2016-10-12 11:18:34 +00:00
|
|
|
const table = this.$parent;
|
2016-08-16 08:07:18 +00:00
|
|
|
const cell = getCell(event);
|
|
|
|
|
|
|
|
if (cell) {
|
2016-10-12 11:18:34 +00:00
|
|
|
const column = getColumnByCell(table, cell);
|
|
|
|
const hoverState = table.hoverState = { cell, column, row };
|
2016-10-21 09:53:49 +00:00
|
|
|
table.$emit('cell-mouse-enter', hoverState.row, hoverState.column, hoverState.cell, event);
|
2016-08-16 08:07:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 判断是否text-overflow, 如果是就显示tooltip
|
2016-10-12 11:18:34 +00:00
|
|
|
const cellChild = event.target.querySelector('.cell');
|
2016-08-16 08:07:18 +00:00
|
|
|
|
2016-09-07 06:18:17 +00:00
|
|
|
this.tooltipDisabled = cellChild.scrollWidth <= cellChild.offsetWidth;
|
2016-08-16 08:07:18 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
handleCellMouseLeave(event) {
|
|
|
|
const cell = getCell(event);
|
2016-10-19 10:53:31 +00:00
|
|
|
if (!cell) return;
|
2016-08-16 08:07:18 +00:00
|
|
|
|
2016-10-19 10:53:31 +00:00
|
|
|
const oldHoverState = this.$parent.hoverState;
|
2016-10-21 09:53:49 +00:00
|
|
|
this.$parent.$emit('cell-mouse-leave', oldHoverState.row, oldHoverState.column, oldHoverState.cell, event);
|
2016-08-16 08:07:18 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
handleMouseEnter(index) {
|
2016-10-12 11:18:34 +00:00
|
|
|
this.store.commit('setHoverRow', index);
|
2016-08-16 08:07:18 +00:00
|
|
|
},
|
|
|
|
|
2016-11-03 13:00:09 +00:00
|
|
|
handleMouseLeave() {
|
|
|
|
this.store.commit('setHoverRow', null);
|
|
|
|
},
|
|
|
|
|
2016-08-16 08:07:18 +00:00
|
|
|
handleClick(event, row) {
|
2016-10-12 11:18:34 +00:00
|
|
|
const table = this.$parent;
|
2016-08-16 08:07:18 +00:00
|
|
|
const cell = getCell(event);
|
|
|
|
|
|
|
|
if (cell) {
|
2016-10-12 11:18:34 +00:00
|
|
|
const column = getColumnByCell(table, cell);
|
2016-08-16 08:07:18 +00:00
|
|
|
if (column) {
|
2016-10-19 10:53:31 +00:00
|
|
|
table.$emit('cell-click', row, column, cell, event);
|
2016-08-16 08:07:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-04 08:15:33 +00:00
|
|
|
this.store.commit('setCurrentRow', row);
|
|
|
|
|
2016-10-19 10:53:31 +00:00
|
|
|
table.$emit('row-click', row, event);
|
2016-08-16 08:07:18 +00:00
|
|
|
},
|
|
|
|
|
2016-11-12 18:31:04 +00:00
|
|
|
getCellContent(row, property, column) {
|
2016-08-16 08:07:18 +00:00
|
|
|
if (column && column.formatter) {
|
|
|
|
return column.formatter(row, column);
|
|
|
|
}
|
|
|
|
|
2016-11-12 18:31:04 +00:00
|
|
|
if (property && property.indexOf('.') === -1) {
|
|
|
|
return row[property];
|
|
|
|
}
|
|
|
|
|
2016-08-16 08:07:18 +00:00
|
|
|
return getValueByPath(row, property);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|