2016-10-25 13:35:41 +00:00
|
|
|
import ElCheckbox from 'element-ui/packages/checkbox';
|
|
|
|
import ElTag from 'element-ui/packages/tag';
|
2016-08-16 08:07:18 +00:00
|
|
|
import objectAssign from 'object-assign';
|
|
|
|
|
|
|
|
let columnIdSeed = 1;
|
|
|
|
|
|
|
|
const defaults = {
|
|
|
|
default: {
|
|
|
|
direction: ''
|
|
|
|
},
|
|
|
|
selection: {
|
|
|
|
width: 48,
|
|
|
|
minWidth: 48,
|
|
|
|
realWidth: 48,
|
|
|
|
direction: ''
|
|
|
|
},
|
|
|
|
index: {
|
|
|
|
width: 48,
|
|
|
|
minWidth: 48,
|
|
|
|
realWidth: 48,
|
|
|
|
direction: ''
|
|
|
|
},
|
|
|
|
filter: {
|
|
|
|
headerTemplate: function(h) { return <span>filter header</span>; },
|
|
|
|
direction: ''
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const forced = {
|
|
|
|
selection: {
|
2016-10-12 11:18:34 +00:00
|
|
|
headerTemplate: function(h) {
|
|
|
|
return <div><el-checkbox
|
|
|
|
nativeOn-click={ this.toggleAllSelection }
|
|
|
|
domProps-value={ this.isAllSelected }
|
|
|
|
on-input={ (value) => { this.$emit('allselectedchange', value); } } />
|
|
|
|
</div>;
|
|
|
|
},
|
|
|
|
template: function(h, { row, column, store, $index }) {
|
|
|
|
return <el-checkbox
|
|
|
|
domProps-value={ row.$selected }
|
|
|
|
disabled={ column.selectable ? !column.selectable.call(null, row, $index) : false }
|
|
|
|
on-input={ (value) => { row.$selected = value; store.commit('rowSelectedChanged', row); } } />;
|
|
|
|
},
|
2016-08-16 08:07:18 +00:00
|
|
|
sortable: false,
|
|
|
|
resizable: false
|
|
|
|
},
|
|
|
|
index: {
|
2016-09-21 03:39:14 +00:00
|
|
|
// headerTemplate: function(h) { return <div>#</div>; },
|
2016-10-12 11:18:34 +00:00
|
|
|
headerTemplate: function(h, label) {
|
|
|
|
return <div>{ label || '#' }</div>;
|
|
|
|
},
|
2016-10-19 10:53:31 +00:00
|
|
|
template: 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
|
|
|
|
},
|
|
|
|
filter: {
|
2016-10-12 11:18:34 +00:00
|
|
|
headerTemplate: function(h) {
|
|
|
|
return <div>#</div>;
|
|
|
|
},
|
|
|
|
template: function(h, { row, column }) {
|
|
|
|
return <el-tag type="primary" style="height: 16px; line-height: 16px; min-width: 40px; text-align: center">{ row[column.property] }</el-tag>;
|
|
|
|
},
|
2016-08-16 08:07:18 +00:00
|
|
|
resizable: false
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
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;
|
|
|
|
};
|
|
|
|
|
|
|
|
export default {
|
|
|
|
name: 'el-table-column',
|
|
|
|
|
|
|
|
props: {
|
|
|
|
type: {
|
|
|
|
type: String,
|
|
|
|
default: 'default'
|
|
|
|
},
|
|
|
|
label: String,
|
|
|
|
property: String,
|
2016-10-12 11:18:34 +00:00
|
|
|
prop: String,
|
2016-08-16 08:07:18 +00:00
|
|
|
width: {},
|
|
|
|
minWidth: {},
|
|
|
|
template: String,
|
|
|
|
sortable: {
|
|
|
|
type: Boolean,
|
|
|
|
default: false
|
|
|
|
},
|
|
|
|
resizable: {
|
|
|
|
type: Boolean,
|
|
|
|
default: true
|
|
|
|
},
|
2016-09-20 22:20:36 +00:00
|
|
|
align: String,
|
2016-09-02 05:56:47 +00:00
|
|
|
showTooltipWhenOverflow: {
|
|
|
|
type: Boolean,
|
|
|
|
default: false
|
|
|
|
},
|
2016-10-12 11:18:34 +00:00
|
|
|
fixed: [Boolean, String],
|
|
|
|
formatter: Function,
|
2016-10-19 10:53:31 +00:00
|
|
|
selectable: Function,
|
|
|
|
reserveSelection: Boolean
|
2016-08-16 08:07:18 +00:00
|
|
|
},
|
|
|
|
|
2016-09-02 11:12:00 +00:00
|
|
|
render() {},
|
2016-08-16 08:07:18 +00:00
|
|
|
|
|
|
|
data() {
|
|
|
|
return {
|
|
|
|
isChildColumn: 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;
|
|
|
|
this.$options.render = (h) => h('div');
|
|
|
|
|
2016-10-12 11:18:34 +00:00
|
|
|
let columnId = this.columnId = (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;
|
|
|
|
this.isChildColumn = 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 template;
|
|
|
|
|
2016-10-12 11:18:34 +00:00
|
|
|
let property = this.prop || this.property;
|
2016-08-16 08:07:18 +00:00
|
|
|
if (property) {
|
2016-08-30 07:36:21 +00:00
|
|
|
template = function(h, { row }, parent) {
|
2016-10-12 11:18:34 +00:00
|
|
|
return <span>{ parent.getCellContent(row, property, columnId) }</span>;
|
2016-08-16 08:07:18 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
let column = getDefaultColumn(type, {
|
|
|
|
id: columnId,
|
|
|
|
label: this.label,
|
2016-10-24 11:01:55 +00:00
|
|
|
property,
|
2016-08-16 08:07:18 +00:00
|
|
|
type,
|
|
|
|
template,
|
|
|
|
minWidth,
|
|
|
|
width,
|
|
|
|
isColumnGroup,
|
2016-09-20 22:20:36 +00:00
|
|
|
align: this.align ? 'is-' + this.align : null,
|
2016-08-16 08:07:18 +00:00
|
|
|
sortable: this.sortable,
|
|
|
|
resizable: this.resizable,
|
2016-09-02 05:56:47 +00:00
|
|
|
showTooltipWhenOverflow: 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-10-12 11:18:34 +00:00
|
|
|
fixed: this.fixed
|
2016-08-16 08:07:18 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
objectAssign(column, forced[type] || {});
|
|
|
|
|
|
|
|
let renderColumn = column.template;
|
|
|
|
let _self = this;
|
2016-08-30 07:36:21 +00:00
|
|
|
|
2016-08-16 08:07:18 +00:00
|
|
|
column.template = function(h, data) {
|
|
|
|
if (_self.$vnode.data.inlineTemplate) {
|
2016-08-30 07:36:21 +00:00
|
|
|
renderColumn = function() {
|
2016-09-01 07:25:35 +00:00
|
|
|
data._staticTrees = _self._staticTrees;
|
|
|
|
data.$options = {};
|
|
|
|
data.$options.staticRenderFns = _self.$options.staticRenderFns;
|
|
|
|
data._renderProxy = _self._renderProxy;
|
|
|
|
data._m = _self._m;
|
|
|
|
|
2016-09-02 11:12:00 +00:00
|
|
|
return _self.customRender.call(data);
|
2016-08-16 08:07:18 +00:00
|
|
|
};
|
2016-10-12 11:18:34 +00:00
|
|
|
}
|
2016-08-16 08:07:18 +00:00
|
|
|
|
|
|
|
return _self.showTooltipWhenOverflow
|
|
|
|
? <el-tooltip
|
|
|
|
effect={ this.effect }
|
|
|
|
placement="top"
|
|
|
|
disabled={ this.tooltipDisabled }>
|
2016-08-30 07:36:21 +00:00
|
|
|
<div class="cell">{ renderColumn(h, data, this._renderProxy) }</div>
|
|
|
|
<span slot="content">{ renderColumn(h, data, this._renderProxy) }</span>
|
2016-08-16 08:07:18 +00:00
|
|
|
</el-tooltip>
|
2016-08-30 07:36:21 +00:00
|
|
|
: <div class="cell">{ renderColumn(h, data, this._renderProxy) }</div>;
|
2016-08-16 08:07:18 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
this.columnConfig = column;
|
|
|
|
},
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
if (!this.isChildColumn) {
|
|
|
|
columnIndex = [].indexOf.call(parent.$refs.hiddenColumns.children, this.$el);
|
|
|
|
} else {
|
|
|
|
columnIndex = [].indexOf.call(parent.$el.children, this.$el);
|
|
|
|
}
|
|
|
|
|
2016-10-12 11:18:34 +00:00
|
|
|
owner.store.commit('insertColumn', this.columnConfig, columnIndex);
|
2016-08-16 08:07:18 +00:00
|
|
|
}
|
|
|
|
};
|