iview/src/components/table/cell.vue

167 lines
6.3 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>
2017-06-02 10:35:18 +08:00
<template v-if="renderType === 'expand' && !row._disableExpand">
2017-05-27 15:13:10 +08:00
<div :class="expandCls" @click="toggleExpand">
<Icon type="ios-arrow-right"></Icon>
</div>
</template>
2016-11-25 15:47:28 +08:00
</div>
</template>
<script>
2017-03-09 14:11:22 +08:00
import Vue from 'vue';
2017-05-27 15:13:10 +08:00
import Icon from '../icon/icon.vue';
2016-11-25 22:51:50 +08:00
import Checkbox from '../checkbox/checkbox.vue';
2017-05-22 10:49:46 +08:00
import { findComponentUpward } from '../../utils/assist';
2016-11-25 22:51:50 +08:00
2016-11-25 15:47:28 +08:00
export default {
2017-03-09 14:11:22 +08:00
name: 'TableCell',
2017-05-27 15:13:10 +08:00
components: { Icon, 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,
2017-05-27 15:13:10 +08:00
expanded: 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,
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`,
{
[`${this.prefixCls}-hidden`]: !this.fixed && this.column.fixed && (this.column.fixed === 'left' || this.column.fixed === 'right'),
2017-05-27 15:13:10 +08:00
[`${this.prefixCls}-cell-ellipsis`]: this.column.ellipsis || false,
[`${this.prefixCls}-cell-with-expand`]: this.renderType === 'expand'
2016-11-25 22:51:50 +08:00
}
2016-12-25 22:49:42 +08:00
];
2017-05-27 15:13:10 +08:00
},
expandCls () {
return [
`${this.prefixCls}-cell-expand`,
{
[`${this.prefixCls}-cell-expand-expanded`]: this.expanded
}
2017-05-27 16:19:28 +08:00
];
2016-11-25 22:51:50 +08:00
}
},
2016-11-25 15:47:28 +08:00
methods: {
compile () {
2017-05-27 15:13:10 +08:00
if (this.column.render && this.renderType === 'render') {
// 兼容真 Render后期废弃旧用法
2017-05-22 10:49:46 +08:00
let isRealRender = true;
const Table = findComponentUpward(this, 'Table');
if (Table.context) isRealRender = false;
if (isRealRender) {
2017-05-26 14:00:42 +08:00
this.$el.innerHTML = '';
const component = new Vue({
2017-05-04 15:22:31 +08:00
functional: true,
render: (h) => {
2017-05-12 15:45:28 +08:00
return this.column.render(h, {
row: this.row,
column: this.column,
index: this.index
});
}
});
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;
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];
});
const component = new Vue({
render: res.render,
staticRenderFns: res.staticRenderFns,
methods: methods,
data () {
return $parent._data;
},
components: components
});
if ($parent.$store != undefined) {
component.$store = $parent.$store;
}
component.row = this.row;
component.column = this.column;
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);
2017-05-27 15:13:10 +08:00
},
toggleExpand () {
this.$parent.$parent.toggleExpand(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';
2017-05-27 15:13:10 +08:00
} else if (this.column.type === 'expand') {
this.renderType = 'expand';
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>