iview/src/components/table/cell.vue

93 lines
3.2 KiB
Vue
Raw Normal View History

2016-11-25 15:47:28 +08:00
<template>
2016-11-25 22:51:50 +08:00
<div :class="classes">
2016-11-25 15:47:28 +08:00
<template v-if="renderType === 'index'">{{index + 1}}</template>
2016-11-25 22:51:50 +08:00
<template v-if="renderType === 'selection'">
<Checkbox :checked="checked" @on-change="toggleSelect(index)"></Checkbox>
</template>
2016-11-25 15:47:28 +08:00
<template v-if="renderType === 'normal'">{{{ row[column.key] }}}</template>
</div>
</template>
<script>
2016-11-25 22:51:50 +08:00
import Checkbox from '../checkbox/checkbox.vue';
2016-11-25 15:47:28 +08:00
export default {
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-25 22:51:50 +08:00
index: Number,
2016-11-26 08:57:09 +08:00
checked: Boolean,
fixed: Boolean
2016-11-25 15:47:28 +08:00
},
data () {
return {
renderType: '',
uid: -1
}
},
2016-11-25 22:51:50 +08:00
computed: {
classes () {
return [
`${this.prefixCls}-cell`,
{
2016-11-26 08:57:09 +08:00
[`${this.prefixCls}-hidden`]: !this.fixed && this.column.fixed && (this.column.fixed === 'left' || this.column.fixed === 'right')
2016-11-25 22:51:50 +08:00
}
]
}
},
2016-11-25 15:47:28 +08:00
methods: {
compile () {
if (this.column.render) {
2016-11-26 08:57:09 +08:00
const $parent = this.$parent.$parent.$parent;
2016-11-25 15:47:28 +08:00
const template = this.column.render(this.row, this.column, this.index);
const cell = document.createElement('div');
cell.innerHTML = template;
2016-11-26 08:57:09 +08:00
const _oldParentChildLen = $parent.$children.length;
$parent.$compile(cell);
const _newParentChildLen = $parent.$children.length;
2016-11-25 15:47:28 +08:00
if (_oldParentChildLen !== _newParentChildLen) { // if render normal html node, do not tag
2016-11-26 08:57:09 +08:00
this.uid = $parent.$children[$parent.$children.length - 1]._uid; // tag it, and delete when data or columns update
2016-11-25 15:47:28 +08:00
}
this.$el.innerHTML = '';
this.$el.appendChild(cell);
}
},
destroy () {
2016-11-26 08:57:09 +08:00
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();
2016-11-25 15:47:28 +08:00
}
}
2016-11-25 22:51:50 +08:00
},
toggleSelect (index) {
2016-11-26 08:57:09 +08:00
this.$parent.$parent.toggleSelect(index);
2016-11-25 15:47:28 +08:00
}
},
compiled () {
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';
}
},
ready () {
this.compile();
},
beforeDestroy () {
this.destroy();
},
watch: {
index () {
this.destroy();
this.compile();
}
}
}
</script>