2016-11-25 15:47:28 +08:00
|
|
|
|
<template>
|
2017-03-09 15:24:40 +08:00
|
|
|
|
<div :class="classes" ref="cell">
|
2016-11-28 14:23:49 +08:00
|
|
|
|
<template v-if="renderType === 'index'">{{naturalIndex + 1}}</template>
|
2016-11-25 22:51:50 +08:00
|
|
|
|
<template v-if="renderType === 'selection'">
|
2017-03-09 14:11:22 +08:00
|
|
|
|
<Checkbox :value="checked" @on-change="toggleSelect" :disabled="disabled"></Checkbox>
|
2016-11-25 22:51:50 +08:00
|
|
|
|
</template>
|
2017-03-09 14:11:22 +08:00
|
|
|
|
<template v-if="renderType === 'normal'"><span v-html="row[column.key]"></span></template>
|
2016-11-25 15:47:28 +08:00
|
|
|
|
</div>
|
|
|
|
|
</template>
|
|
|
|
|
<script>
|
2017-03-09 14:11:22 +08:00
|
|
|
|
import Vue from 'vue';
|
2016-11-25 22:51:50 +08:00
|
|
|
|
import Checkbox from '../checkbox/checkbox.vue';
|
|
|
|
|
|
2016-11-25 15:47:28 +08:00
|
|
|
|
export default {
|
2017-03-09 14:11:22 +08:00
|
|
|
|
name: 'TableCell',
|
2016-11-25 22:51:50 +08:00
|
|
|
|
components: { Checkbox },
|
2016-11-25 15:47:28 +08:00
|
|
|
|
props: {
|
|
|
|
|
prefixCls: String,
|
|
|
|
|
row: Object,
|
|
|
|
|
column: Object,
|
2016-11-28 14:23:49 +08:00
|
|
|
|
naturalIndex: Number, // index of rebuildData
|
|
|
|
|
index: Number, // _index of data
|
2016-11-26 08:57:09 +08:00
|
|
|
|
checked: Boolean,
|
2017-02-22 17:30:39 +08:00
|
|
|
|
disabled: Boolean,
|
2016-11-30 13:17:55 +08:00
|
|
|
|
fixed: {
|
|
|
|
|
type: [Boolean, String],
|
|
|
|
|
default: false
|
|
|
|
|
}
|
2016-11-25 15:47:28 +08:00
|
|
|
|
},
|
|
|
|
|
data () {
|
|
|
|
|
return {
|
|
|
|
|
renderType: '',
|
2016-12-01 11:02:06 +08:00
|
|
|
|
uid: -1,
|
2017-03-17 09:50:11 +08:00
|
|
|
|
context: this.$parent.$parent.currentContext
|
2016-12-25 22:49:42 +08:00
|
|
|
|
};
|
2016-11-25 15:47:28 +08:00
|
|
|
|
},
|
2016-11-25 22:51:50 +08:00
|
|
|
|
computed: {
|
|
|
|
|
classes () {
|
|
|
|
|
return [
|
|
|
|
|
`${this.prefixCls}-cell`,
|
|
|
|
|
{
|
2017-01-02 13:42:32 -06:00
|
|
|
|
[`${this.prefixCls}-hidden`]: !this.fixed && this.column.fixed && (this.column.fixed === 'left' || this.column.fixed === 'right'),
|
|
|
|
|
[`${this.prefixCls}-cell-ellipsis`]: this.column.ellipsis || false
|
2016-11-25 22:51:50 +08:00
|
|
|
|
}
|
2016-12-25 22:49:42 +08:00
|
|
|
|
];
|
2016-11-25 22:51:50 +08:00
|
|
|
|
}
|
|
|
|
|
},
|
2016-11-25 15:47:28 +08:00
|
|
|
|
methods: {
|
|
|
|
|
compile () {
|
|
|
|
|
if (this.column.render) {
|
2017-05-04 12:05:29 +08:00
|
|
|
|
// 兼容真 Render,后期废弃旧用法
|
|
|
|
|
let isRealRender = false;
|
|
|
|
|
try {
|
|
|
|
|
this.column.render(this.row, this.column, this.index);
|
|
|
|
|
}
|
|
|
|
|
catch (err) {
|
|
|
|
|
isRealRender = true;
|
|
|
|
|
}
|
2017-03-15 15:27:04 +08:00
|
|
|
|
|
2017-05-04 12:05:29 +08:00
|
|
|
|
if (isRealRender) {
|
|
|
|
|
const component = new Vue({
|
2017-05-04 15:22:31 +08:00
|
|
|
|
functional: true,
|
2017-05-04 12:05:29 +08:00
|
|
|
|
render: (h) => {
|
2017-05-12 15:45:28 +08:00
|
|
|
|
return this.column.render(h, {
|
|
|
|
|
row: this.row,
|
|
|
|
|
column: this.column,
|
|
|
|
|
index: this.index
|
|
|
|
|
});
|
2017-05-04 12:05:29 +08:00
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
const Cell = component.$mount();
|
|
|
|
|
this.$refs.cell.appendChild(Cell.$el);
|
|
|
|
|
} else {
|
|
|
|
|
const $parent = this.context;
|
|
|
|
|
const template = this.column.render(this.row, this.column, this.index);
|
|
|
|
|
const cell = document.createElement('div');
|
|
|
|
|
cell.innerHTML = template;
|
2017-04-27 15:29:05 +08:00
|
|
|
|
|
2017-05-04 12:05:29 +08:00
|
|
|
|
this.$el.innerHTML = '';
|
|
|
|
|
let methods = {};
|
|
|
|
|
Object.keys($parent).forEach(key => {
|
|
|
|
|
const func = $parent[key];
|
|
|
|
|
if (typeof(func) === 'function' && (func.name === 'boundFn' || func.name === 'n')) {
|
|
|
|
|
methods[key] = func;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
const res = Vue.compile(cell.outerHTML);
|
|
|
|
|
// 获取父组件使用的局部 component
|
|
|
|
|
const components = {};
|
|
|
|
|
Object.getOwnPropertyNames($parent.$options.components).forEach(item => {
|
|
|
|
|
components[item] = $parent.$options.components[item];
|
|
|
|
|
});
|
2017-04-27 15:29:05 +08:00
|
|
|
|
|
2017-05-04 12:05:29 +08:00
|
|
|
|
const component = new Vue({
|
|
|
|
|
render: res.render,
|
|
|
|
|
staticRenderFns: res.staticRenderFns,
|
|
|
|
|
methods: methods,
|
|
|
|
|
data () {
|
|
|
|
|
return $parent._data;
|
|
|
|
|
},
|
|
|
|
|
components: components
|
|
|
|
|
});
|
2017-05-09 15:04:59 +08:00
|
|
|
|
if ($parent.$store != undefined) {
|
|
|
|
|
component.$store = $parent.$store;
|
|
|
|
|
}
|
2017-05-04 12:05:29 +08:00
|
|
|
|
component.row = this.row;
|
|
|
|
|
component.column = this.column;
|
2017-03-15 15:27:04 +08:00
|
|
|
|
|
2017-05-04 12:05:29 +08:00
|
|
|
|
const Cell = component.$mount();
|
|
|
|
|
this.$refs.cell.appendChild(Cell.$el);
|
|
|
|
|
}
|
2016-11-25 15:47:28 +08:00
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
destroy () {
|
2017-03-15 15:27:04 +08:00
|
|
|
|
|
2016-11-25 22:51:50 +08:00
|
|
|
|
},
|
2016-11-28 14:23:49 +08:00
|
|
|
|
toggleSelect () {
|
|
|
|
|
this.$parent.$parent.toggleSelect(this.index);
|
2016-11-25 15:47:28 +08:00
|
|
|
|
}
|
|
|
|
|
},
|
2017-03-09 14:11:22 +08:00
|
|
|
|
created () {
|
2016-11-25 15:47:28 +08:00
|
|
|
|
if (this.column.type === 'index') {
|
|
|
|
|
this.renderType = 'index';
|
2016-11-25 22:51:50 +08:00
|
|
|
|
} else if (this.column.type === 'selection') {
|
|
|
|
|
this.renderType = 'selection';
|
2016-11-25 15:47:28 +08:00
|
|
|
|
} else if (this.column.render) {
|
|
|
|
|
this.renderType = 'render';
|
|
|
|
|
} else {
|
|
|
|
|
this.renderType = 'normal';
|
|
|
|
|
}
|
|
|
|
|
},
|
2017-03-09 14:11:22 +08:00
|
|
|
|
mounted () {
|
|
|
|
|
this.$nextTick(() => {
|
|
|
|
|
this.compile();
|
|
|
|
|
});
|
2016-11-25 15:47:28 +08:00
|
|
|
|
},
|
|
|
|
|
beforeDestroy () {
|
|
|
|
|
this.destroy();
|
|
|
|
|
},
|
|
|
|
|
watch: {
|
2016-11-28 14:23:49 +08:00
|
|
|
|
naturalIndex () {
|
2016-11-25 15:47:28 +08:00
|
|
|
|
this.destroy();
|
|
|
|
|
this.compile();
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-12-25 22:49:42 +08:00
|
|
|
|
};
|
|
|
|
|
</script>
|