cell.vue
3.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
<template>
<div :class="classes">
<template v-if="renderType === 'index'">{{index + 1}}</template>
<template v-if="renderType === 'selection'">
<Checkbox :checked="checked" @on-change="toggleSelect(index)"></Checkbox>
</template>
<template v-if="renderType === 'normal'">{{{ row[column.key] }}}</template>
</div>
</template>
<script>
import Checkbox from '../checkbox/checkbox.vue';
export default {
components: { Checkbox },
props: {
prefixCls: String,
row: Object,
column: Object,
index: Number,
checked: Boolean,
fixed: Boolean
},
data () {
return {
renderType: '',
uid: -1
}
},
computed: {
classes () {
return [
`${this.prefixCls}-cell`,
{
[`${this.prefixCls}-hidden`]: !this.fixed && this.column.fixed && (this.column.fixed === 'left' || this.column.fixed === 'right')
}
]
}
},
methods: {
compile () {
if (this.column.render) {
const $parent = this.$parent.$parent.$parent;
const template = this.column.render(this.row, this.column, this.index);
const cell = document.createElement('div');
cell.innerHTML = template;
const _oldParentChildLen = $parent.$children.length;
$parent.$compile(cell);
const _newParentChildLen = $parent.$children.length;
if (_oldParentChildLen !== _newParentChildLen) { // if render normal html node, do not tag
this.uid = $parent.$children[$parent.$children.length - 1]._uid; // tag it, and delete when data or columns update
}
this.$el.innerHTML = '';
this.$el.appendChild(cell);
}
},
destroy () {
const $parent = this.$parent.$parent.$parent;
for (let i = 0; i < $parent.$children.length; i++) {
if ($parent.$children[i]._uid === this.uid) {
$parent.$children[i].$destroy();
}
}
},
toggleSelect (index) {
this.$parent.$parent.toggleSelect(index);
}
},
compiled () {
if (this.column.type === 'index') {
this.renderType = 'index';
} else if (this.column.type === 'selection') {
this.renderType = 'selection';
} else if (this.column.render) {
this.renderType = 'render';
} else {
this.renderType = 'normal';
}
},
ready () {
this.compile();
},
beforeDestroy () {
this.destroy();
},
watch: {
index () {
this.destroy();
this.compile();
}
}
}
</script>