DynamicAddTable.vue
4.19 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
<template>
<div class="dynamic-table-container">
员工标签:<el-button type="default" v-if="isEdit" @click="addRow">
添加事由
</el-button>
<el-form :model="dynamicForm" ref="dynamicForm">
<el-table :data="tableData" border style="width: 100%; margin-top: 20px">
<el-table-column prop="index" label="序号" width="80" align="center">
<template slot-scope="scope">
{{ scope.$index + 1 }}
</template>
</el-table-column>
<el-table-column prop="adjustAmount" label="调整金额" width="180">
<template slot-scope="scope">
<el-form-item :prop="'tableData.' + scope.$index + '.adjustAmount'" :rules="adjustAmountRules">
<el-input v-model="scope.row.adjustAmount" :disabled="!isEdit" placeholder="请输入金额" type="number" @blur="handleInputBlur"></el-input>
</el-form-item>
</template>
</el-table-column>
<el-table-column prop="adjustReason" label="调整事由">
<template slot-scope="scope">
<el-form-item :prop="'tableData.' + scope.$index + '.adjustReason'" :rules="adjustReasonRules">
<el-input v-model="scope.row.adjustReason" placeholder="请输入调整事由" :disabled="!isEdit"></el-input>
</el-form-item>
</template>
</el-table-column>
<el-table-column label="操作" width="120" align="center" v-if="isEdit">
<template slot-scope="scope">
<el-button size="mini" type="danger" @click="deleteRow(scope.$index)" icon="el-icon-delete"></el-button>
</template>
</el-table-column>
</el-table>
</el-form>
<!-- <div style="margin-top: 20px">
<el-button type="info" @click="showTableData">显示表格数据</el-button>
<el-button type="success" @click="validateForm">提交验证</el-button>
</div> -->
</div>
</template>
<script>
export default {
name: 'DynamicAdjustmentTable',
props: {
adjustments: {
type: Array,
default: () => []
},
isEdit: {
type: Boolean,
default: true
}
},
data() {
return {
dynamicForm: {
tableData: []
},
adjustAmountRules: [
{ required: true, message: '请输入调整金额', trigger: 'blur' },
{
validator: (rule, value, callback) => {
if (value && isNaN(Number(value))) {
callback(new Error('请输入有效的数字'));
} else {
callback();
}
},
trigger: 'blur'
}
],
adjustReasonRules: [
{ required: true, message: '请输入调整事由', trigger: 'blur' },
{ min: 1, max: 50, message: '长度在 1 到 50 个字符', trigger: 'blur' }
]
}
},
computed: {
tableData: {
get() {
return this.dynamicForm.tableData;
},
set(value) {
this.dynamicForm.tableData = value;
}
}
},
created() {
// 初始化一行数据
this.addRow();
},
mounted() {
this.dynamicForm.tableData = this.adjustments
},
methods: {
addRow() {
this.tableData.push({
adjustAmount: '',
adjustReason: ''
});
},
deleteRow(index) {
// if (this.tableData.length <= 1) {
// this.$message.warning('至少保留一行数据');
// return;
// }
this.tableData.splice(index, 1);
},
handleInputBlur() {
this.$emit('handleInputBlur')
},
showTableData() {
const formattedData = this.tableData.map((row, index) => ({
'序号': index + 1,
'调整金额': row.adjustAmount,
'调整事由': row.adjustReason
}));
console.log('当前表格数据:', formattedData);
},
validateForm() {
this.$refs.dynamicForm.validate(valid => {
if (valid) {
this.$message.success('验证通过,表单数据有效');
this.showTableData();
} else {
this.$message.error('请检查表单,确保所有必填项已填写');
return false;
}
});
}
}
}
</script>
<style scoped>
.dynamic-table-container {
padding: 20px;
}
.el-form-item {
margin-bottom: 0;
}
</style>