index.vue
2.94 KB
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
<style>
.eagle-form-table .eagle-form-table__body td {
padding: 5px;
}
.eagle-form-table__label {
text-align: right !important;
padding: 5px 15px !important;
font-weight: bold;
background-color: #ecf7ff;
}
.eagle-form-table__value {
overflow: hidden;
}
</style>
<template>
<div class="eagle-form-table el-table el-table--fit el-table--border el-table--enable-row-transition el-table--small">
<div v-if="fullProps.title || $scopedSlots['title'] || $slots['title']" class="el-table__header-wrapper">
<table class="el-table__header w-full" cellspacing="0" cellpadding="0">
<thead>
<tr class="el-table__row">
<!-- 标题插槽 -->
<th v-if="$scopedSlots['title'] || $slots['title']">
<slot name="title"></slot>
</th>
<th v-else class="is-center">{{ fullProps.title }}</th>
</tr>
</thead>
</table>
</div>
<div class="el-table__body-wrapper text-lg">
<table class="el-table__body w-full" cellspacing="0" cellpadding="0">
<tbody class="eagle-form-table__body">
<tr v-for="(item, index) in fullProps.list" :key="index" class="el-table__row">
<template v-if="(item instanceof Array)">
<template v-for="(col, idx) in item">
<td class="eagle-form-table__label" :style="labelStyle" :key="`${index}-${idx}-label`">{{ col.label }}</td>
<td class="eagle-form-table__value" :key="`${index}-${idx}-value`" :colspan="getColspan({ item, col, idx })">
<component :is="col.type" v-bind="col.props" size="mini" />
</td>
</template>
</template>
<template v-else>
<td class="eagle-form-table__label" :style="labelStyle">{{ item.label }}</td>
<td class="eagle-form-table__value" :colspan="maxColSpan - 1">
<component :is="item.type" v-bind="item.props" size="mini" />
</td>
</template>
</tr>
</tbody>
</table>
</div>
</div>
</template>
<script>
export default {
name: 'form-table',
data() {
return {
fullProps: { ...this.$attrs, ...this.$props },
labelStyle: { width: '80px' },
}
},
computed: {
// 配置列表中的最大列数
maxColNum() {
let number = 1;
this.fullProps.list.forEach(item => {
if (item instanceof Array) {
if (item.length > number) {
number = item.length;
}
}
});
return number;
},
// 配置列表的最大占位数
maxColSpan() {
return this.maxColNum * 2;
},
},
methods: {
getColspan({ item, col, idx }) {
// 如果存在列数小于最大列数的行
if (item.length < this.maxColNum) {
const normalColSpan = Math.floor((this.maxColSpan - item.length) / item.length);
return col.colspan || normalColSpan;
}
return 1;
}
}
}
</script>