2016-10-25 13:35:41 +00:00
|
|
|
import ElCheckbox from 'element-ui/packages/checkbox';
|
|
|
|
import ElTag from 'element-ui/packages/tag';
|
2016-11-04 10:44:19 +00:00
|
|
|
import objectAssign from 'element-ui/src/utils/merge';
|
2016-11-23 10:24:32 +00:00
|
|
|
import { getValueByPath } from './util';
|
2016-08-16 08:07:18 +00:00
|
|
|
|
|
|
|
let columnIdSeed = 1;
|
|
|
|
|
|
|
|
const defaults = {
|
|
|
|
default: {
|
2016-10-27 17:24:13 +00:00
|
|
|
order: ''
|
2016-08-16 08:07:18 +00:00
|
|
|
},
|
|
|
|
selection: {
|
|
|
|
width: 48,
|
|
|
|
minWidth: 48,
|
|
|
|
realWidth: 48,
|
2016-12-09 06:44:00 +00:00
|
|
|
order: '',
|
|
|
|
className: 'el-table-column--selection'
|
2016-08-16 08:07:18 +00:00
|
|
|
},
|
2016-11-12 15:08:17 +00:00
|
|
|
expand: {
|
|
|
|
width: 48,
|
|
|
|
minWidth: 48,
|
|
|
|
realWidth: 48,
|
|
|
|
order: ''
|
|
|
|
},
|
2016-08-16 08:07:18 +00:00
|
|
|
index: {
|
|
|
|
width: 48,
|
|
|
|
minWidth: 48,
|
|
|
|
realWidth: 48,
|
2016-10-27 17:24:13 +00:00
|
|
|
order: ''
|
2016-08-16 08:07:18 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const forced = {
|
|
|
|
selection: {
|
2016-11-04 23:30:23 +00:00
|
|
|
renderHeader: function(h) {
|
2016-10-27 13:45:21 +00:00
|
|
|
return <el-checkbox
|
2016-10-12 11:18:34 +00:00
|
|
|
nativeOn-click={ this.toggleAllSelection }
|
2016-11-03 06:42:18 +00:00
|
|
|
domProps-value={ this.isAllSelected } />;
|
2016-10-12 11:18:34 +00:00
|
|
|
},
|
2016-11-04 23:30:23 +00:00
|
|
|
renderCell: function(h, { row, column, store, $index }) {
|
2016-10-12 11:18:34 +00:00
|
|
|
return <el-checkbox
|
2016-11-03 06:42:18 +00:00
|
|
|
domProps-value={ store.isSelected(row) }
|
2016-10-12 11:18:34 +00:00
|
|
|
disabled={ column.selectable ? !column.selectable.call(null, row, $index) : false }
|
2016-11-04 23:30:23 +00:00
|
|
|
on-input={ () => { store.commit('rowSelectedChanged', row); } } />;
|
2016-10-12 11:18:34 +00:00
|
|
|
},
|
2016-08-16 08:07:18 +00:00
|
|
|
sortable: false,
|
|
|
|
resizable: false
|
|
|
|
},
|
|
|
|
index: {
|
2016-11-04 23:30:23 +00:00
|
|
|
renderHeader: function(h, { column }) {
|
|
|
|
return column.label || '#';
|
2016-10-12 11:18:34 +00:00
|
|
|
},
|
2016-11-04 23:30:23 +00:00
|
|
|
renderCell: function(h, { $index }) {
|
2016-10-12 11:18:34 +00:00
|
|
|
return <div>{ $index + 1 }</div>;
|
|
|
|
},
|
2016-08-16 08:07:18 +00:00
|
|
|
sortable: false
|
2016-11-12 15:08:17 +00:00
|
|
|
},
|
|
|
|
expand: {
|
|
|
|
renderHeader: function(h, {}) {
|
|
|
|
return '';
|
|
|
|
},
|
|
|
|
renderCell: function(h, { row, store }, proxy) {
|
|
|
|
const expanded = store.states.expandRows.indexOf(row) > -1;
|
|
|
|
return <div class={ 'el-table__expand-icon ' + (expanded ? 'el-table__expand-icon--expanded' : '') }
|
|
|
|
on-click={ () => proxy.handleExpandClick(row) }>
|
|
|
|
<i class='el-icon el-icon-arrow-right'></i>
|
|
|
|
</div>;
|
|
|
|
},
|
|
|
|
sortable: false,
|
|
|
|
resizable: false,
|
|
|
|
className: 'el-table__expand-column'
|
2016-08-16 08:07:18 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const getDefaultColumn = function(type, options) {
|
|
|
|
const column = {};
|
|
|
|
|
|
|
|
objectAssign(column, defaults[type || 'default']);
|
|
|
|
|
|
|
|
for (let name in options) {
|
|
|
|
if (options.hasOwnProperty(name)) {
|
|
|
|
const value = options[name];
|
|
|
|
if (typeof value !== 'undefined') {
|
|
|
|
column[name] = value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-12 11:18:34 +00:00
|
|
|
if (!column.minWidth) {
|
|
|
|
column.minWidth = 80;
|
|
|
|
}
|
|
|
|
|
|
|
|
column.realWidth = column.width || column.minWidth;
|
|
|
|
|
2016-08-16 08:07:18 +00:00
|
|
|
return column;
|
|
|
|
};
|
|
|
|
|
2016-11-23 10:24:32 +00:00
|
|
|
const DEFAULT_RENDER_CELL = function(h, { row, column }) {
|
|
|
|
const property = column.property;
|
|
|
|
if (column && column.formatter) {
|
|
|
|
return column.formatter(row, column);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (property && property.indexOf('.') === -1) {
|
|
|
|
return row[property];
|
|
|
|
}
|
|
|
|
|
|
|
|
return getValueByPath(row, property);
|
2016-11-04 23:30:23 +00:00
|
|
|
};
|
|
|
|
|
2016-08-16 08:07:18 +00:00
|
|
|
export default {
|
|
|
|
name: 'el-table-column',
|
|
|
|
|
|
|
|
props: {
|
|
|
|
type: {
|
|
|
|
type: String,
|
|
|
|
default: 'default'
|
|
|
|
},
|
|
|
|
label: String,
|
2016-11-13 06:39:24 +00:00
|
|
|
className: String,
|
2016-08-16 08:07:18 +00:00
|
|
|
property: String,
|
2016-10-12 11:18:34 +00:00
|
|
|
prop: String,
|
2016-08-16 08:07:18 +00:00
|
|
|
width: {},
|
|
|
|
minWidth: {},
|
2016-11-04 23:30:23 +00:00
|
|
|
renderHeader: Function,
|
2016-08-16 08:07:18 +00:00
|
|
|
sortable: {
|
2016-12-30 03:11:20 +00:00
|
|
|
type: [String, Boolean],
|
2016-08-16 08:07:18 +00:00
|
|
|
default: false
|
|
|
|
},
|
2016-10-27 17:24:13 +00:00
|
|
|
sortMethod: Function,
|
2016-08-16 08:07:18 +00:00
|
|
|
resizable: {
|
|
|
|
type: Boolean,
|
|
|
|
default: true
|
|
|
|
},
|
2016-11-18 07:00:22 +00:00
|
|
|
context: {},
|
2016-12-20 10:21:15 +00:00
|
|
|
columnKey: String,
|
2016-09-20 22:20:36 +00:00
|
|
|
align: String,
|
2016-12-21 03:03:22 +00:00
|
|
|
headerAlign: String,
|
2016-11-04 08:27:51 +00:00
|
|
|
showTooltipWhenOverflow: Boolean,
|
|
|
|
showOverflowTooltip: Boolean,
|
2016-10-12 11:18:34 +00:00
|
|
|
fixed: [Boolean, String],
|
|
|
|
formatter: Function,
|
2016-10-19 10:53:31 +00:00
|
|
|
selectable: Function,
|
2016-10-27 13:45:21 +00:00
|
|
|
reserveSelection: Boolean,
|
|
|
|
filterMethod: Function,
|
|
|
|
filters: Array,
|
|
|
|
filterMultiple: {
|
|
|
|
type: Boolean,
|
|
|
|
default: true
|
|
|
|
}
|
2016-08-16 08:07:18 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
data() {
|
|
|
|
return {
|
2016-11-23 12:32:23 +00:00
|
|
|
isSubColumn: false,
|
2016-08-30 07:36:21 +00:00
|
|
|
columns: []
|
2016-08-16 08:07:18 +00:00
|
|
|
};
|
|
|
|
},
|
|
|
|
|
2016-08-30 07:36:21 +00:00
|
|
|
beforeCreate() {
|
|
|
|
this.row = {};
|
|
|
|
this.column = {};
|
|
|
|
this.$index = 0;
|
|
|
|
},
|
|
|
|
|
2016-08-16 08:07:18 +00:00
|
|
|
components: {
|
|
|
|
ElCheckbox,
|
|
|
|
ElTag
|
|
|
|
},
|
|
|
|
|
2016-10-12 11:18:34 +00:00
|
|
|
computed: {
|
|
|
|
owner() {
|
|
|
|
let parent = this.$parent;
|
|
|
|
while (parent && !parent.tableId) {
|
|
|
|
parent = parent.$parent;
|
|
|
|
}
|
|
|
|
return parent;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2016-08-16 08:07:18 +00:00
|
|
|
created() {
|
2016-09-02 11:12:00 +00:00
|
|
|
this.customRender = this.$options.render;
|
2016-12-20 02:47:16 +00:00
|
|
|
this.$options.render = h => h('div', this.$slots.default);
|
2016-09-02 11:12:00 +00:00
|
|
|
|
2016-12-20 10:21:15 +00:00
|
|
|
let columnId = this.columnId = this.columnKey || ((this.$parent.tableId || (this.$parent.columnId + '_')) + 'column_' + columnIdSeed++);
|
2016-08-16 08:07:18 +00:00
|
|
|
|
|
|
|
let parent = this.$parent;
|
2016-10-12 11:18:34 +00:00
|
|
|
let owner = this.owner;
|
2016-11-23 12:32:23 +00:00
|
|
|
this.isSubColumn = owner !== parent;
|
2016-08-16 08:07:18 +00:00
|
|
|
|
|
|
|
let type = this.type;
|
|
|
|
|
|
|
|
let width = this.width;
|
|
|
|
if (width !== undefined) {
|
|
|
|
width = parseInt(width, 10);
|
|
|
|
if (isNaN(width)) {
|
|
|
|
width = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let minWidth = this.minWidth;
|
|
|
|
if (minWidth !== undefined) {
|
|
|
|
minWidth = parseInt(minWidth, 10);
|
|
|
|
if (isNaN(minWidth)) {
|
|
|
|
minWidth = 80;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let isColumnGroup = false;
|
|
|
|
|
|
|
|
let column = getDefaultColumn(type, {
|
|
|
|
id: columnId,
|
|
|
|
label: this.label,
|
2016-11-13 06:39:24 +00:00
|
|
|
className: this.className,
|
2016-11-04 23:30:23 +00:00
|
|
|
property: this.prop || this.property,
|
2016-08-16 08:07:18 +00:00
|
|
|
type,
|
2016-11-23 10:24:32 +00:00
|
|
|
renderCell: null,
|
2016-11-04 23:30:23 +00:00
|
|
|
renderHeader: this.renderHeader,
|
2016-08-16 08:07:18 +00:00
|
|
|
minWidth,
|
|
|
|
width,
|
|
|
|
isColumnGroup,
|
2016-11-18 07:00:22 +00:00
|
|
|
context: this.context,
|
2016-09-20 22:20:36 +00:00
|
|
|
align: this.align ? 'is-' + this.align : null,
|
2016-12-21 03:03:22 +00:00
|
|
|
headerAlign: this.headerAlign ? 'is-' + this.headerAlign : (this.align ? 'is-' + this.align : null),
|
2016-12-30 03:11:20 +00:00
|
|
|
sortable: this.sortable === '' ? true : this.sortable,
|
2016-10-27 17:24:13 +00:00
|
|
|
sortMethod: this.sortMethod,
|
2016-08-16 08:07:18 +00:00
|
|
|
resizable: this.resizable,
|
2016-11-04 08:27:51 +00:00
|
|
|
showOverflowTooltip: this.showOverflowTooltip || this.showTooltipWhenOverflow,
|
2016-10-12 11:18:34 +00:00
|
|
|
formatter: this.formatter,
|
|
|
|
selectable: this.selectable,
|
2016-10-19 10:53:31 +00:00
|
|
|
reserveSelection: this.reserveSelection,
|
2016-12-30 03:11:20 +00:00
|
|
|
fixed: this.fixed === '' ? true : this.fixed,
|
2016-10-27 13:45:21 +00:00
|
|
|
filterMethod: this.filterMethod,
|
|
|
|
filters: this.filters,
|
|
|
|
filterable: this.filters || this.filterMethod,
|
|
|
|
filterMultiple: this.filterMultiple,
|
|
|
|
filterOpened: false,
|
|
|
|
filteredValue: []
|
2016-08-16 08:07:18 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
objectAssign(column, forced[type] || {});
|
|
|
|
|
2016-11-12 15:08:17 +00:00
|
|
|
this.columnConfig = column;
|
|
|
|
|
2016-11-04 23:30:23 +00:00
|
|
|
let renderCell = column.renderCell;
|
2016-08-16 08:07:18 +00:00
|
|
|
let _self = this;
|
2016-08-30 07:36:21 +00:00
|
|
|
|
2016-11-12 15:08:17 +00:00
|
|
|
if (type === 'expand') {
|
|
|
|
owner.renderExpanded = function(h, data) {
|
2016-12-28 10:11:25 +00:00
|
|
|
return _self.$scopedSlots.default
|
|
|
|
? _self.$scopedSlots.default(data)
|
|
|
|
: _self.$slots.default;
|
2016-11-12 15:08:17 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
column.renderCell = function(h, data) {
|
|
|
|
return <div class="cell">{ renderCell(h, data, this._renderProxy) }</div>;
|
|
|
|
};
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-11-04 23:30:23 +00:00
|
|
|
column.renderCell = function(h, data) {
|
2016-12-28 10:11:25 +00:00
|
|
|
// 未来版本移除
|
2016-08-16 08:07:18 +00:00
|
|
|
if (_self.$vnode.data.inlineTemplate) {
|
2016-11-04 23:30:23 +00:00
|
|
|
renderCell = function() {
|
2016-11-18 07:00:22 +00:00
|
|
|
data._self = _self.context || data._self;
|
2016-11-13 03:44:29 +00:00
|
|
|
if (Object.prototype.toString.call(data._self) === '[object Object]') {
|
|
|
|
for (let prop in data._self) {
|
|
|
|
if (!data.hasOwnProperty(prop)) {
|
|
|
|
data[prop] = data._self[prop];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-11-18 07:00:22 +00:00
|
|
|
// 静态内容会缓存到 _staticTrees 内,不改的话获取的静态数据就不是内部 context
|
|
|
|
data._staticTrees = _self._staticTrees;
|
|
|
|
data.$options.staticRenderFns = _self.$options.staticRenderFns;
|
2016-09-02 11:12:00 +00:00
|
|
|
return _self.customRender.call(data);
|
2016-08-16 08:07:18 +00:00
|
|
|
};
|
2016-12-20 02:47:16 +00:00
|
|
|
} else if (_self.$scopedSlots.default) {
|
|
|
|
renderCell = () => _self.$scopedSlots.default(data);
|
2016-10-12 11:18:34 +00:00
|
|
|
}
|
2016-08-16 08:07:18 +00:00
|
|
|
|
2016-11-23 10:24:32 +00:00
|
|
|
if (!renderCell) {
|
|
|
|
renderCell = DEFAULT_RENDER_CELL;
|
|
|
|
}
|
|
|
|
|
2016-11-04 08:27:51 +00:00
|
|
|
return _self.showOverflowTooltip || _self.showTooltipWhenOverflow
|
2016-08-16 08:07:18 +00:00
|
|
|
? <el-tooltip
|
|
|
|
effect={ this.effect }
|
|
|
|
placement="top"
|
|
|
|
disabled={ this.tooltipDisabled }>
|
2016-11-23 10:24:32 +00:00
|
|
|
<div class="cell">{ renderCell(h, data) }</div>
|
|
|
|
<span slot="content">{ renderCell(h, data) }</span>
|
2016-08-16 08:07:18 +00:00
|
|
|
</el-tooltip>
|
2016-11-23 10:24:32 +00:00
|
|
|
: <div class="cell">{ renderCell(h, data) }</div>;
|
2016-08-16 08:07:18 +00:00
|
|
|
};
|
|
|
|
},
|
|
|
|
|
|
|
|
destroyed() {
|
2016-10-12 11:18:34 +00:00
|
|
|
if (!this.$parent) return;
|
|
|
|
this.owner.store.commit('removeColumn', this.columnConfig);
|
2016-08-16 08:07:18 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
watch: {
|
|
|
|
label(newVal) {
|
|
|
|
if (this.columnConfig) {
|
|
|
|
this.columnConfig.label = newVal;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2016-10-12 11:18:34 +00:00
|
|
|
prop(newVal) {
|
|
|
|
if (this.columnConfig) {
|
|
|
|
this.columnConfig.property = newVal;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2016-08-16 08:07:18 +00:00
|
|
|
property(newVal) {
|
|
|
|
if (this.columnConfig) {
|
|
|
|
this.columnConfig.property = newVal;
|
|
|
|
}
|
2016-11-04 08:27:51 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
filters(newVal) {
|
|
|
|
if (this.columnConfig) {
|
|
|
|
this.columnConfig.filters = newVal;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
filterMultiple(newVal) {
|
|
|
|
if (this.columnConfig) {
|
|
|
|
this.columnConfig.filterMultiple = newVal;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
align(newVal) {
|
|
|
|
if (this.columnConfig) {
|
2016-11-24 10:34:02 +00:00
|
|
|
this.columnConfig.align = newVal ? 'is-' + newVal : null;
|
2016-11-04 08:27:51 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2016-12-21 03:03:22 +00:00
|
|
|
headerAlign(newVal) {
|
|
|
|
if (this.columnConfig) {
|
|
|
|
this.columnConfig.headerAlign = newVal ? 'is-' + newVal : this.align;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2016-11-04 08:27:51 +00:00
|
|
|
width(newVal) {
|
|
|
|
if (this.columnConfig) {
|
|
|
|
this.columnConfig.width = newVal;
|
2016-11-23 22:54:32 +00:00
|
|
|
this.owner.store.scheduleLayout();
|
2016-11-04 08:27:51 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
minWidth(newVal) {
|
|
|
|
if (this.columnConfig) {
|
|
|
|
this.columnConfig.minWidth = newVal;
|
2016-11-23 22:54:32 +00:00
|
|
|
this.owner.store.scheduleLayout();
|
2016-11-04 08:27:51 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
fixed(newVal) {
|
|
|
|
if (this.columnConfig) {
|
|
|
|
this.columnConfig.fixed = newVal;
|
2016-11-23 22:54:32 +00:00
|
|
|
this.owner.store.scheduleLayout();
|
2016-11-04 08:27:51 +00:00
|
|
|
}
|
2016-08-16 08:07:18 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
mounted() {
|
2016-10-12 11:18:34 +00:00
|
|
|
const owner = this.owner;
|
|
|
|
const parent = this.$parent;
|
2016-08-16 08:07:18 +00:00
|
|
|
let columnIndex;
|
|
|
|
|
2016-11-23 12:32:23 +00:00
|
|
|
if (!this.isSubColumn) {
|
2016-08-16 08:07:18 +00:00
|
|
|
columnIndex = [].indexOf.call(parent.$refs.hiddenColumns.children, this.$el);
|
|
|
|
} else {
|
|
|
|
columnIndex = [].indexOf.call(parent.$el.children, this.$el);
|
|
|
|
}
|
|
|
|
|
2016-11-23 12:32:23 +00:00
|
|
|
owner.store.commit('insertColumn', this.columnConfig, columnIndex, this.isSubColumn ? parent.columnConfig : null);
|
2016-08-16 08:07:18 +00:00
|
|
|
}
|
|
|
|
};
|