DynamicAddTable.vue
4.68 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
<template>
<div class="dynamic-table-container">
<span class="title">{{configObj.title}}调整:</span><el-button type="default" style="padding: 8px 20px;" v-if="isEdit" @click="addRow">
添加事由
</el-button>
<el-form :model="dynamicForm" ref="dynamicForm">
<el-table :data="tableData" border style="width: 100%; margin-top: 20px !important;">
<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" @input="handleInput(scope.$index, 'adjustAmount')" @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="请输入调整事由" maxlength="50" :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="text" @click="deleteRow(scope.$index)">删除</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>
import { deepClone, Util } from '@/common/util'
import { limitPrice } from '@/common/format'
export default {
name: 'DynamicAdjustmentTable',
props: {
adjustments: {
type: Array,
default: () => []
},
isEdit: {
type: Boolean,
default: true
},
configObj: {
type: Object,
default: () => {
return {
title: '应付'
}
}
}
},
data() {
return {
dynamicForm: {
tableData: [],
tableOldData: []
},
adjustAmountRules: [
{ required: true, message: '请输入调整金额', trigger: 'blur' },
{
validator: (rule, value, callback) => {
const decimalPart = String(value).split('.')[1]
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
this.tableOldData = deepClone(this.adjustments)
},
methods: {
addRow() {
this.tableData.push({
adjustAmount: '',
adjustReason: ''
});
this.tableOldData = deepClone(this.tableData)
},
deleteRow(index) {
this.tableData.splice(index, 1);
this.tableOldData = deepClone(this.tableData)
this.$emit('handleInputBlur')
},
handleInput(rowIdx, attr) {
const val = this.tableData[rowIdx][attr]
const oldVal = this.tableOldData[rowIdx][attr]
this.tableData[rowIdx][attr] = limitPrice(val, oldVal, 7, 2, '', '', 'priceReg_negative')
this.tableOldData[rowIdx][attr] = this.tableData[rowIdx][attr]
},
handleInputBlur() {
this.$emit('handleInputBlur')
},
validateForm() {
var flag = true
this.$refs.dynamicForm.validate(valid => {
if (!valid) {
flag = false
}
});
return flag
}
}
}
</script>
<style lang="scss">
.dynamic-table-container {
box-sizing: border-box;
padding: 20px 20px 0 20px;
.title {
font-size: 14px;
}
.el-table--medium td {
padding: 4px 16px !important;
}
.el-form-item {
margin-bottom: 0;
}
}
</style>