element/packages/table/src/table-column.js

344 lines
8.0 KiB
JavaScript
Raw Normal View History

2016-10-25 13:35:41 +00:00
import ElCheckbox from 'element-ui/packages/checkbox';
import ElTag from 'element-ui/packages/tag';
import objectAssign from 'element-ui/src/utils/merge';
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,
order: '',
className: 'el-table-column--selection'
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
nativeOn-click={ this.toggleAllSelection }
2016-11-03 06:42:18 +00:00
domProps-value={ this.isAllSelected } />;
},
2016-11-04 23:30:23 +00:00
renderCell: function(h, { row, column, store, $index }) {
return <el-checkbox
2016-11-03 06:42:18 +00:00
domProps-value={ store.isSelected(row) }
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-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-11-04 23:30:23 +00:00
renderCell: function(h, { $index }) {
return <div>{ $index + 1 }</div>;
},
2016-08-16 08:07:18 +00:00
sortable: 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;
}
}
}
if (!column.minWidth) {
column.minWidth = 80;
}
column.realWidth = column.width || column.minWidth;
2016-08-16 08:07:18 +00:00
return column;
};
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,
className: String,
2016-08-16 08:07:18 +00:00
property: String,
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-10-27 17:24:13 +00:00
type: [Boolean, String],
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: {},
columnKey: String,
align: String,
showTooltipWhenOverflow: Boolean,
showOverflowTooltip: Boolean,
fixed: [Boolean, String],
formatter: Function,
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
},
render() {
return (<div>{ this._t('default') }</div>);
},
2016-08-16 08:07:18 +00:00
data() {
return {
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
},
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) => {
return (<div>{ this._t('default') }</div>);
};
2016-09-02 11:12:00 +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;
let owner = this.owner;
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,
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,
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,
align: this.align ? 'is-' + this.align : null,
2016-08-16 08:07:18 +00:00
sortable: this.sortable,
2016-10-27 17:24:13 +00:00
sortMethod: this.sortMethod,
2016-08-16 08:07:18 +00:00
resizable: this.resizable,
showOverflowTooltip: this.showOverflowTooltip || this.showTooltipWhenOverflow,
formatter: this.formatter,
selectable: this.selectable,
reserveSelection: this.reserveSelection,
2016-10-27 13:45:21 +00:00
fixed: this.fixed,
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-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-04 23:30:23 +00:00
column.renderCell = function(h, data) {
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;
if (Object.prototype.toString.call(data._self) === '[object Object]') {
for (let prop in data._self) {
if (!data.hasOwnProperty(prop)) {
2016-11-18 11:14:56 +00:00
// _self.$set(data, prop, data._self[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-08-16 08:07:18 +00:00
if (!renderCell) {
renderCell = DEFAULT_RENDER_CELL;
}
return _self.showOverflowTooltip || _self.showTooltipWhenOverflow
2016-08-16 08:07:18 +00:00
? <el-tooltip
effect={ this.effect }
placement="top"
disabled={ this.tooltipDisabled }>
<div class="cell">{ renderCell(h, data) }</div>
<span slot="content">{ renderCell(h, data) }</span>
2016-08-16 08:07:18 +00:00
</el-tooltip>
: <div class="cell">{ renderCell(h, data) }</div>;
2016-08-16 08:07:18 +00:00
};
this.columnConfig = column;
},
destroyed() {
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;
}
},
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;
}
},
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;
}
},
width(newVal) {
if (this.columnConfig) {
this.columnConfig.width = newVal;
this.owner.store.scheduleLayout();
}
},
minWidth(newVal) {
if (this.columnConfig) {
this.columnConfig.minWidth = newVal;
this.owner.store.scheduleLayout();
}
},
fixed(newVal) {
if (this.columnConfig) {
this.columnConfig.fixed = newVal;
this.owner.store.scheduleLayout();
}
2016-08-16 08:07:18 +00:00
}
},
mounted() {
const owner = this.owner;
const parent = this.$parent;
2016-08-16 08:07:18 +00:00
let columnIndex;
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);
}
owner.store.commit('insertColumn', this.columnConfig, columnIndex, this.isSubColumn ? parent.columnConfig : null);
2016-08-16 08:07:18 +00:00
}
};