iview/src/components/table/cell.vue

113 lines
3.7 KiB
Vue
Raw Normal View History

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,
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,
2017-03-09 14:11:22 +08:00
content: this.$parent.$parent.currentContent
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-25 15:47:28 +08:00
this.$el.innerHTML = '';
2017-03-09 14:11:22 +08:00
let methods = {};
Object.keys($parent).forEach(key => {
const func = $parent[key];
2017-03-09 15:24:40 +08:00
if (typeof(func) === 'function' && func.name === 'boundFn') {
methods[key] = func;
}
});
2017-03-09 14:11:22 +08:00
const res = Vue.compile(cell.outerHTML);
// todo 临时解决方案
2017-03-09 15:24:40 +08:00
const component = new Vue({
2017-03-09 14:11:22 +08:00
render: res.render,
staticRenderFns: res.staticRenderFns,
methods: methods,
data () {
return $parent._data;
}
2017-03-09 14:11:22 +08:00
});
2017-03-09 15:24:40 +08:00
const Cell = component.$mount();
this.$refs.cell.appendChild(Cell.$el);
2016-11-25 15:47:28 +08:00
}
},
destroy () {
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>