iview/src/components/table/cell.vue

101 lines
3.5 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-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'">
<Checkbox :checked="checked" @on-change="toggleSelect" :disabled="disabled"></Checkbox>
2016-11-25 22:51:50 +08:00
</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-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,
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: '',
uid: -1,
content: this.$parent.$parent.content
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`,
{
[`${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) {
const $parent = this.content;
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;
2017-01-16 12:29:03 +08:00
$parent.$compile(cell); // todo 这里无法触发 ready 钩子
2016-11-26 08:57:09 +08:00
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 () {
const $parent = this.content;
2016-11-26 08:57:09 +08:00
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
},
2016-11-28 14:23:49 +08:00
toggleSelect () {
this.$parent.$parent.toggleSelect(this.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: {
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>