0bf7e6a5
wuxw
加入问卷功能代码
|
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
|
<template>
<el-dialog :title="$t('questionTitle.edit.title')" :visible.sync="visible" width="70%" @close="handleClose">
<el-form ref="form" :model="formData" label-width="120px">
<el-form-item :label="$t('questionTitle.edit.type')" prop="titleType" :rules="[
{ required: true, message: $t('questionTitle.edit.typeRequired'), trigger: 'change' }
]">
<el-select v-model="formData.titleType" :placeholder="$t('questionTitle.edit.typePlaceholder')" style="width:100%"
@change="handleTypeChange">
<el-option :label="$t('questionTitle.edit.pleaseSelect')" value="" disabled />
<el-option :label="$t('questionTitle.types.singleChoice')" value="1001" />
<el-option :label="$t('questionTitle.types.multipleChoice')" value="2002" />
<el-option :label="$t('questionTitle.types.shortAnswer')" value="3003" />
</el-select>
</el-form-item>
<el-form-item :label="$t('questionTitle.edit.name')" prop="qaTitle" :rules="[
{ required: true, message: $t('questionTitle.edit.nameRequired'), trigger: 'blur' },
{ max: 256, message: $t('questionTitle.edit.nameTooLong'), trigger: 'blur' }
]">
<el-input v-model="formData.qaTitle" :placeholder="$t('questionTitle.edit.namePlaceholder')" />
</el-form-item>
<template v-if="formData.titleType && formData.titleType !== '3003'">
<el-form-item v-for="(item, index) in formData.titleValues" :key="index"
:label="`${$t('questionTitle.edit.option')}${item.seq}`" :prop="`titleValues.${index}.qaValue`" :rules="[
{ required: true, message: $t('questionTitle.edit.optionRequired'), trigger: 'blur' }
]">
<el-row :gutter="20">
<el-col :span="18">
<el-input v-model="item.qaValue" :placeholder="$t('questionTitle.edit.optionPlaceholder')" />
</el-col>
<el-col :span="5">
<el-button v-if="index === formData.titleValues.length - 1" type="text" @click="addOption">
{{ $t('questionTitle.edit.addOption') }}
</el-button>
<el-button v-else type="text" @click="removeOption(index)">
{{ $t('questionTitle.edit.removeOption') }}
</el-button>
</el-col>
</el-row>
</el-form-item>
</template>
</el-form>
<div slot="footer" class="dialog-footer">
<el-button @click="visible = false">
{{ $t('common.cancel') }}
</el-button>
<el-button type="primary" @click="handleSubmit">
{{ $t('common.confirm') }}
</el-button>
</div>
</el-dialog>
</template>
<script>
import { updateQuestionTitle } from '@/api/oa/questionTitleApi'
import { getCommunityId } from '@/api/community/communityApi'
export default {
name: 'EditQuestionTitle',
data() {
return {
visible: false,
formData: {
titleId: '',
titleType: '',
qaTitle: '',
titleValues: [],
communityId: ''
}
}
},
methods: {
open(data) {
this.resetForm()
this.formData = {
...data,
communityId: getCommunityId()
}
this.visible = true
this.$nextTick(() => {
this.$refs.form.clearValidate()
})
},
handleClose() {
this.$refs.form.resetFields()
},
resetForm() {
this.formData = {
titleId: '',
titleType: '',
qaTitle: '',
titleValues: [],
communityId: getCommunityId()
}
},
handleTypeChange(value) {
if (value === '3003') {
this.formData.titleValues = []
} else if (value === '1001' && this.formData.titleValues.length === 0) {
this.formData.titleValues = [{ qaValue: '', seq: 1 }]
} else if (value === '2002' && this.formData.titleValues.length < 2) {
this.formData.titleValues = [
{ qaValue: '', seq: 1 },
{ qaValue: '', seq: 2 }
]
}
},
addOption() {
const newSeq = this.formData.titleValues.length + 1
this.formData.titleValues.push({ qaValue: '', seq: newSeq })
},
removeOption(index) {
this.formData.titleValues.splice(index, 1)
// 重新排序
this.formData.titleValues.forEach((item, i) => {
item.seq = i + 1
})
},
async handleSubmit() {
try {
const valid = await this.$refs.form.validate()
if (!valid) return
// 如果是简答题,清空选项
if (this.formData.titleType === '3003') {
this.formData.titleValues = []
}
await updateQuestionTitle(this.formData)
this.$message.success(this.$t('questionTitle.edit.success'))
this.$emit('success')
this.visible = false
} catch (error) {
console.error(error)
}
}
}
}
</script>
|