index.vue
5.12 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
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
<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;
}
.eagle-form-table__value {
overflow: hidden;
}
.eagle-form-table__value.noborder .el-input__inner,
.eagle-form-table__value.noborder .el-textarea__inner {
border-left: 0;
border-right: 0;
border-top: 0;
border-bottom: 0;
}
.eagle-form-table .el-form-item__error {
margin-left: 0;
}
.eagle-form-table .el-form-item__content {
display: flex;
flex-direction: column;
}
</style>
<template>
<el-form :model="model" inline-message 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`" :rowspan="col.rowspan">
<el-form-item :label="col.label" :prop="col.key" :rules="bindItemRules(col)" style="margin-bottom: 0;"></el-form-item>
</td>
<td class="eagle-form-table__value" :class="{ noborder: !border }" :key="`${index}-${idx}-value`" :colspan="getColspan({ item, col, idx })" :rowspan="col.rowspan">
<el-form-item :prop="col.key" :rules="bindItemRules(col)" style="margin-bottom: 0;">
<component :is="col.type" v-model="model[col.key]" v-bind="col.props" v-on="bindItemEvent(col)" size="mini" />
</el-form-item>
</td>
</template>
</template>
<template v-else>
<td class="eagle-form-table__label" :style="labelStyle" :rowspan="item.rowspan">{{ item.label }}</td>
<td class="eagle-form-table__value" :class="{ noborder: !border }" :colspan="maxColSpan - 1" :rowspan="item.rowspan">
<component :is="item.type" v-model="model[item.key]" v-bind="item.props" v-on="bindItemEvent(item)" size="mini" />
</td>
</template>
</tr>
</tbody>
</table>
</div>
<div v-if="$scopedSlots['footer'] || $slots['footer']" class="el-table__footer-wrapper">
<table class="el-table__header w-full" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td class="eagle-form-table__value" :class="{ noborder: !border }" :colspan="maxColSpan" style="text-align: center">
<slot name="footer"></slot>
</td>
</tr>
</tbody>
</table>
</div>
</el-form>
</template>
<script>
export default {
name: 'form-table',
props: {
value: Object,
list: Array,
title: String,
border: {
type: Boolean,
default: false
}
},
data() {
return {
model: this.value || {},
fullProps: { ...this.$attrs, ...this.$props },
labelStyle: { width: '100px' },
}
},
watch: {
value(val = {}) {
this.model = val;
},
model: {
handler(val) {
this.$emit("input", val);
},
deep: true,
}
},
computed: {
// 配置列表中的最大列数
maxColNum() {
let number = 1;
this.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;
},
/**
* @description 绑定表单项事件
* @param {Object} item 表单项配置
* @returns {Function} 事件函数
*/
bindItemEvent(item) {
if (item.on) {
if (typeof item.on === 'function') {
return item.on(this.model);
} else {
return item.on
}
} else {
return undefined
}
},
// 绑定组件校验规则
bindItemRules(item) {
if (typeof item.rules === 'function') {
return item.rules(this.model);
} else {
return item.rules;
}
},
}
}
</script>