/
index.vue
120 lines (111 loc) · 3.29 KB
/
index.vue
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
<!--需要使用组件实例ref属性只能在父组件定义ref属性,使用this.$refs.<父组件ref属性名>.$children[0]获取-->
<template>
<el-table
v-bind="$attrs"
v-on="$listeners"
:data="data"
:empty-text="emptyText"
:stripe="stripe"
>
<slot name="font" />
<template v-for="(column, index) in columns">
<!--正常表头(不需要处理)-->
<el-table-column
v-if="isCommonTableColumn(column) && !column.hidden"
:key="index"
v-bind="column.attrs || {}"
>
</el-table-column>
<el-table-column
v-else-if="!column.hidden"
:key="index"
v-bind="column.attrs || {}"
>
<template v-slot="scope">
<!--插槽/作用域插槽(Vue2.6+)-->
<!--eg. <template v-slot:testSlot="{scope}" >-->
<span v-if="column.slot">
<slot :name="column.slot" :scope="scope" />
</span>
<!--操作图标-->
<div v-else-if="column.operations">
<template v-for="operation in column.operations">
<el-tooltip
v-if="operation.name"
effect="light"
:key="operation.svgName"
:content="operation.name"
placement="top-end"
>
<base-icon
:key="operation.svgName"
:class="[column.attrs.className, operation.className]"
:name="operation.svgName"
@click.native="handleOperation(operation.event, scope.row)"
>
</base-icon>
</el-tooltip>
<base-icon
v-else
:key="operation.svgName"
:class="[column.attrs.className, operation.className]"
:name="operation.svgName"
@click.native="handleOperation(operation.event, scope.row)"
>
</base-icon>
</template>
</div>
<span v-else>{{ scope.row[column.attr] }}</span>
</template>
</el-table-column>
</template>
<slot />
</el-table>
</template>
<script>
import { proxyProp } from "../../util/proxyProp";
import { findComponentUpwardByProp } from "../../util/findComponents";
export default {
name: "base-table",
props: {
columns: {
type: Array,
required: true
},
data: {
type: Array,
required: true
}
},
computed: {
emptyText() {
return this.$attrs["empty-text"] || "没有符合条件的数据";
},
stripe() {
return this.$attrs.stripe !== false;
}
},
mounted() {
//代理父组件的columns属性
let parentComponent = findComponentUpwardByProp(this, "columns");
if (parentComponent) {
parentComponent.columns = parentComponent.columns.map(column =>
proxyProp(column)
);
} else {
throw new Error("can not find parentComponent");
}
},
methods: {
// 是否是一个常规的table-column(有以下标签就不是常规table-column)
isCommonTableColumn(column) {
const specialColumnList = ["slot"];
return !specialColumnList.some(option => column[option]);
},
//点击操作按钮触发的事件
handleOperation(event, row) {
this.$emit(event, row);
}
}
};
</script>