EditFillQuestion.vue 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. <template>
  2. <div class="edit-fill-question">
  3. <el-form
  4. ref="modalFormComp"
  5. :model="modalForm"
  6. :rules="rules"
  7. :key="modalForm.id"
  8. label-width="120px"
  9. >
  10. <el-form-item label="题型:">
  11. {{ questionName }}
  12. </el-form-item>
  13. <el-form-item prop="topicNo" label="大题序号:">
  14. <el-input-number
  15. v-model="modalForm.topicNo"
  16. :min="1"
  17. :max="99"
  18. :step="1"
  19. step-strictly
  20. :controls="false"
  21. ></el-input-number>
  22. </el-form-item>
  23. <el-form-item prop="topicName" label="题目名称:">
  24. <el-input
  25. v-model="modalForm.topicName"
  26. type="textarea"
  27. :autosize="{ minRows: 2, maxRows: 5 }"
  28. resize="none"
  29. placeholder="请输入题目名称"
  30. clearable
  31. maxlength="100"
  32. show-word-limit
  33. ></el-input>
  34. </el-form-item>
  35. <el-form-item prop="endNumber" label="起止题号:">
  36. <el-input-number
  37. style="width: 40px"
  38. v-model="modalForm.startNumber"
  39. :min="0"
  40. :max="999"
  41. :step="1"
  42. step-strictly
  43. :controls="false"
  44. ></el-input-number>
  45. <span class="el-input-split"></span>
  46. <el-input-number
  47. style="width: 40px"
  48. v-model="modalForm.endNumber"
  49. :min="modalForm.startNumber"
  50. :max="999"
  51. :step="1"
  52. step-strictly
  53. :controls="false"
  54. ></el-input-number>
  55. </el-form-item>
  56. <el-form-item prop="optionCount" label="选项个数:">
  57. <el-input-number
  58. style="width: 125px"
  59. v-model="modalForm.optionCount"
  60. :min="2"
  61. :max="22"
  62. :step="1"
  63. step-strictly
  64. :controls="false"
  65. :disabled="modalForm.isBoolean"
  66. ></el-input-number>
  67. </el-form-item>
  68. <el-form-item
  69. v-if="topicType === 'boolean'"
  70. label="选项排列方向:"
  71. required
  72. >
  73. <el-radio-group v-model="modalForm.optionDirection" size="small">
  74. <el-radio-button
  75. v-for="(val, key) in DIRECTION_TYPE"
  76. :key="key"
  77. :label="key"
  78. >{{ val }}</el-radio-button
  79. >
  80. </el-radio-group>
  81. </el-form-item>
  82. </el-form>
  83. </div>
  84. </template>
  85. <script>
  86. import { getFillQuestionName } from "../../elementModel";
  87. import { BOOLEAN_TYPE, DIRECTION_TYPE } from "../../enumerate";
  88. const initModalForm = {
  89. id: "",
  90. topicNo: null,
  91. topicName: "",
  92. startNumber: 1,
  93. endNumber: 5,
  94. questionsCount: 10,
  95. optionCount: 5,
  96. optionDirection: "horizontal",
  97. questionDirection: "vertical",
  98. isBoolean: false,
  99. booleanType: BOOLEAN_TYPE[0],
  100. isMultiply: false,
  101. };
  102. export default {
  103. name: "edit-fill-question",
  104. props: {
  105. instance: {
  106. type: Object,
  107. default() {
  108. return {};
  109. },
  110. },
  111. },
  112. data() {
  113. const topicTypeValidater = (rule, value, callback) => {
  114. if (!this.topicType) {
  115. callback(new Error("请选择题型"));
  116. } else {
  117. callback();
  118. }
  119. };
  120. const numberRangeValidater = (rule, value, callback) => {
  121. if (!this.modalForm.startNumber || !this.modalForm.endNumber) {
  122. return callback(new Error("请输入起止题号"));
  123. }
  124. if (this.modalForm.startNumber > this.modalForm.endNumber) {
  125. callback(new Error("开始题号不能大于结束题号"));
  126. } else {
  127. callback();
  128. }
  129. };
  130. return {
  131. modalForm: { ...initModalForm },
  132. BOOLEAN_TYPE,
  133. DIRECTION_TYPE,
  134. topicType: null,
  135. rules: {
  136. topicType: [
  137. {
  138. required: true,
  139. validator: topicTypeValidater,
  140. trigger: "change",
  141. },
  142. ],
  143. topicNo: [
  144. {
  145. required: true,
  146. message: "请输入大题序号",
  147. trigger: "change",
  148. },
  149. ],
  150. topicName: [
  151. {
  152. required: true,
  153. message: "请输入题目名称",
  154. trigger: "change",
  155. },
  156. ],
  157. endNumber: [
  158. {
  159. required: true,
  160. message: "请输入起止题号",
  161. trigger: "change",
  162. },
  163. {
  164. type: "number",
  165. validator: numberRangeValidater,
  166. trigger: "change",
  167. },
  168. ],
  169. optionCount: [
  170. {
  171. required: true,
  172. type: "number",
  173. message: "请输入选项个数",
  174. trigger: "change",
  175. },
  176. ],
  177. },
  178. };
  179. },
  180. computed: {
  181. questionName() {
  182. return getFillQuestionName(this.modalForm);
  183. },
  184. },
  185. mounted() {
  186. this.initData(this.instance);
  187. },
  188. methods: {
  189. initData(val) {
  190. const valInfo = val.parent || val;
  191. if (val["_edit"]) this.topicType = this.getTopicType(valInfo);
  192. this.modalForm = Object.assign({}, initModalForm, valInfo);
  193. this.modalForm.endNumber =
  194. this.modalForm.startNumber + this.modalForm.questionsCount - 1;
  195. },
  196. topicTypeChange() {
  197. if (this.topicType === "single") {
  198. this.modalForm.isMultiply = false;
  199. this.modalForm.isBoolean = false;
  200. this.modalForm.optionCount = 4;
  201. } else if (this.topicType === "multiply") {
  202. this.modalForm.isMultiply = true;
  203. this.modalForm.isBoolean = false;
  204. this.modalForm.optionCount = 4;
  205. } else {
  206. this.modalForm.isMultiply = false;
  207. this.modalForm.isBoolean = true;
  208. this.modalForm.optionCount = 2;
  209. }
  210. },
  211. getTopicType(info) {
  212. if (info.isMultiply) return "multiply";
  213. if (info.isBoolean) return "boolean";
  214. return "single";
  215. },
  216. async submit() {
  217. const valid = await this.$refs.modalFormComp.validate().catch(() => {});
  218. if (!valid) return;
  219. this.modalForm.questionsCount =
  220. this.modalForm.endNumber - this.modalForm.startNumber + 1;
  221. this.modalForm.topicName = this.modalForm.topicName.trim();
  222. this.$emit("modified", this.modalForm);
  223. },
  224. },
  225. };
  226. </script>