addLevel.vue
11.4 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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
<template>
<!-- ... existing code ... -->
<div class="grade-editor">
<el-button :disabled="status == 2 || status == 1"
icon="el-icon-plus"
plain
@click="addLevel">
添加等级
</el-button>
<el-form ref="levelForm"
class="grade-form"
:model="formModel">
<el-table border
:data="levels"
size="mini"
style="width: 100%; margin-top: 8px">
<el-table-column align="center"
label="序号"
width="60">
<template #default="scope">
{{ scope.$index + 1 }}
</template>
</el-table-column>
<el-table-column align="center"
label="等级分数段"
min-width="373">
<template #default="scope">
<el-input disabled
style="width: 140px"
:value="scope.row.minScore" />
<span class="between">≤ 分数 ≤</span>
<!-- 包裹最大值为表单项,动态校验:正整数、> 最小值、≤ 999 -->
<el-form-item :prop="`levels.${scope.$index}.maxScore`"
:rules="[
{
validator: maxValidatorFactory(scope.$index),
trigger: ['blur'],
},
]">
<el-input v-model="levels[scope.$index].maxScore"
:disabled="status == 2 || status == 1"
placeholder="请输入"
style="width: 140px"
@input="onMaxInput(scope.$index, $event)" />
</el-form-item>
</template>
</el-table-column>
<el-table-column align="center"
label="等级名称"
min-width="373">
<template #default="scope">
<el-form-item :prop="`levels.${scope.$index}.levelName`"
:rules="nameRules">
<el-input v-model="levels[scope.$index].levelName"
:disabled="status == 2 || status == 1"
maxlength="10"
placeholder="请输入"
@input="onNameInput(scope.$index, $event)" />
</el-form-item>
</template>
</el-table-column>
<el-table-column v-if="status == 0"
align="center"
label="操作"
width="68">
<template #default="scope">
<el-link v-if="scope.$index !== 0"
type="primary"
:underline="false"
@click="deleteLevel(scope.$index)">
删除
</el-link>
<span v-else style="color: #999"></span>
</template>
</el-table-column>
</el-table>
</el-form>
</div>
</template>
<script>
export default {
props: {
examLevel: {
type: Array,
default: () => [],
},
status: {
type: Number | String,
default: "",
},
},
data() {
return {
id: this.$route.query.id,
levels: [{ maxScore: "", minScore: 0, levelName: "" }],
// 行内错误集合
errors: [],
formModel: { levels: [] },
nameRules: [
{ required: true, message: "等级名称必填", trigger: "blur" },
{
validator: (rule, value, cb) => {
const name = String(value || "").trim();
if (!name)
return cb(new Error("等级名称必填"));
if (name.length > 10)
return cb(new Error("最多 10 个字符"));
const names = (this.levels || [])
.map(l =>
String(l && l.levelName != null ? l.levelName : "").trim(),
)
.filter(n => !!n);
const count = names.filter(n => n === name).length;
if (count > 1) {
return cb(new Error("等级名称不可重复"));
}
cb();
},
trigger: ["blur", "change"],
},
],
};
},
watch: {
// levels 任何变动时,重算错误并修正连续性
levels: {
deep: true,
handler() {
this.computeErrors();
},
},
examLevel: {
deep: true,
immediate: true,
handler(val) {
if (this.id || this.id === 0) {
this.levels = val;
this.formModel.levels = this.levels;
this.computeErrors();
}
},
},
},
created() {
this.formModel.levels = this.levels;
this.computeErrors();
},
methods: {
// 添加等级:最小值 = 上一行最大值 + 1,且不可编辑
addLevel() {
if (this.levels.length == 6) {
this.$message.error("最多添加6个等级");
return;
}
if (this.status == 2)
return;
const last = this.levels[this.levels.length - 1];
const lastMax = this.toInt(last.maxScore);
if (!this.isValidMax(lastMax, last.minScore)) {
this.$message.warning(
"请先填写上一行的最大值(正整数且 > 最小值,≤ 999)",
);
return;
}
const nextMin = lastMax + 1;
if (nextMin > 999) {
this.$message.warning("已达到最大分数 999,不能继续添加");
return;
}
this.levels.push({ minScore: nextMin, maxScore: "", levelName: "" });
},
// 删除等级(第一行不可删)
deleteLevel(index) {
if (index === 0)
return;
this.levels.splice(index, 1);
this.recomputeMins();
},
// 最大值输入:仅正整数、自动纠偏(≥ 最小值 且 ≤ 999)
onMaxInput(index, val) {
const row = this.levels[index];
const minScore = Number(row.minScore) || 0;
// 规范化:只保留数字字符
let s = String(val == null ? "" : val).replace(/\D/g, "");
// 长度限制:最多 3 位(上限 999)
if (s.length > 3)
s = s.slice(0, 3);
// 更新可见值
row.maxScore = s;
// 空输入:不联动
if (!s)
return;
const n = Number(s);
if (!Number.isFinite(n))
return;
// 上限处理:> 999 时直接卡到 999 并联动
if (n > 999) {
row.maxScore = "999";
this.recomputeMins();
return;
}
// 仅当有效且 n > minScore 时,联动后续最小值
if (n > minScore) {
this.recomputeMins();
}
// n <= minScore 的中间态(如 1、12 相对 minScore=71):允许存在,不清空、不联动
},
// 等级名称:最多 10 个字符
onNameInput(index, val) {
const row = this.levels[index];
const levelName = String(val || "").slice(0, 10);
this.$set(this.levels, index, { ...row, levelName });
},
// 删除或编辑后,后续行最小值按“上一行最大值 + 1”重算
recomputeMins() {
for (let i = 1; i < this.levels.length; i++) {
const prev = this.levels[i - 1];
const prevMax = this.toInt(prev.maxScore);
if (Number.isFinite(prevMax)) {
this.levels[i].minScore = prevMax + 1;
if (
this.levels[i].maxScore !== ""
&& Number(this.levels[i].maxScore) <= this.levels[i].minScore
) {
// 当前行最大值不合法:清空,让用户重新输入(避免强制等于最小值)
this.levels[i].maxScore = "";
}
}
}
},
// 工具:提取正整数
toInt(val) {
if (val === null || val === undefined)
return Number.NaN;
const s = String(val).trim();
if (!s)
return Number.NaN;
const n = Number(s);
if (!Number.isFinite(n))
return Number.NaN;
return Math.floor(Math.abs(n));
},
// 重算每行错误,并确保后续行最小值连续
computeErrors() {
const errs = [];
for (let i = 0; i < this.levels.length; i++) {
const row = this.levels[i];
const err = {};
// 校验最大值
const maxScore = this.toInt(row.maxScore);
if (!Number.isFinite(maxScore)) {
err.maxScore = "请输入最大值(正整数)";
} else if (maxScore < row.minScore) {
err.maxScore = `最大值须 ≥ ${row.minScore}`;
} else if (maxScore > 999) {
err.maxScore = "最大值须 ≤ 999";
}
// 校验名称
const levelName = String(row.levelName || "").trim();
if (!levelName) {
err.levelName = "等级名称必填";
} else if (levelName.length > 10) {
err.levelName = "最多 10 个字符";
}
// 连续性:后一行的最小值 = 当前行最大值 + 1(若能计算)
if (Number.isFinite(maxScore) && i + 1 < this.levels.length) {
const expectedMin = maxScore + 1;
if (this.levels[i + 1].minScore !== expectedMin) {
this.$set(this.levels[i + 1], "minScore", expectedMin);
}
}
errs.push(err);
}
this.errors = errs;
},
// 对外校验入口:全部通过返回 true,否则提示并返回 false
validateAll() {
this.computeErrors();
for (let i = 0; i < this.errors.length; i++) {
const e = this.errors[i];
if (e.maxScore || e.levelName) {
this.$message.error(`第 ${i + 1} 行:${e.maxScore || e.levelName}`);
return false;
}
// 强制要求每行最大值已填写
const maxScore = this.toInt(this.levels[i].maxScore);
if (!Number.isFinite(maxScore)) {
this.$message.error(`第 ${i + 1} 行:请输入最大值(正整数)`);
return false;
}
}
return true;
},
// 校验最大值合法性
isValidMax(maxScore, minScore) {
return (
Number.isFinite(maxScore) && maxScore >= minScore && maxScore <= 999
);
},
// 动态生成最大值校验器(严格 > 最小值)
maxValidatorFactory(index) {
return (rule, value, callback) => {
const minScore = this.levels[index].minScore;
const n = this.toInt(value);
if (!Number.isFinite(n))
return callback(new Error("请输入最大值(正整数)"));
if (n <= minScore) {
this.levels[index].maxScore = "";
return callback(new Error(`最大值须 > ${minScore}`));
}
if (n > 999)
return callback(new Error("最大值须 ≤ 999"));
callback();
};
},
// 新增:保存入口,先跑表单校验,再跑连续性校验,成功后提交
onSave() {
return new Promise((resolve, reject) => {
this.$refs.levelForm.validate((valid) => {
resolve(valid);
});
});
},
// 输入触发尝试提交:先跑表单校验,再跑整体验证(连续性)
onTrySubmit() {
this.$refs.levelForm.validate((valid) => {
if (!valid)
return;
if (this.validateAll()) {
this.$emit("submit", this.levels);
}
});
},
},
};
</script>
<style scoped lang="scss">
.grade-editor {
margin-top: 8px;
}
.between {
display: inline-block;
margin: 0 8px;
color: #666;
}
::v-deep .grade-form {
.el-form-item__error {
position: static !important;
}
}
::v-deep .el-button.is-disabled,
.el-button.is-disabled:focus,
.el-button.is-disabled:hover {
color: #c0c4cc !important;
background-color: #fff !important;
border-color: #ebeef5 !important;
}
</style>