EditFillQuestion.vue 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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 v-if="modalForm.isBoolean" label="选项排列方向:" required>
  69. <el-radio-group v-model="modalForm.optionDirection" size="small">
  70. <el-radio-button
  71. v-for="(val, key) in DIRECTION_TYPE"
  72. :key="key"
  73. :label="key"
  74. >{{ val }}</el-radio-button
  75. >
  76. </el-radio-group>
  77. </el-form-item>
  78. </el-form>
  79. </div>
  80. </template>
  81. <script>
  82. import { getFillQuestionName } from "../../elementModel";
  83. import { BOOLEAN_TYPE, DIRECTION_TYPE } from "../../enumerate";
  84. const initModalForm = {
  85. id: "",
  86. topicNo: null,
  87. topicName: "",
  88. startNumber: 1,
  89. endNumber: 5,
  90. questionsCount: 10,
  91. optionCount: 5,
  92. optionDirection: "horizontal",
  93. questionDirection: "vertical",
  94. isBoolean: false,
  95. booleanType: BOOLEAN_TYPE[0],
  96. isMultiply: false,
  97. };
  98. export default {
  99. name: "edit-fill-question",
  100. props: {
  101. instance: {
  102. type: Object,
  103. default() {
  104. return {};
  105. },
  106. },
  107. },
  108. data() {
  109. const topicTypeValidater = (rule, value, callback) => {
  110. if (!this.topicType) {
  111. callback(new Error("请选择题型"));
  112. } else {
  113. callback();
  114. }
  115. };
  116. const numberRangeValidater = (rule, value, callback) => {
  117. if (!this.modalForm.startNumber || !this.modalForm.endNumber) {
  118. return callback(new Error("请输入起止题号"));
  119. }
  120. if (this.modalForm.startNumber > this.modalForm.endNumber) {
  121. callback(new Error("开始题号不能大于结束题号"));
  122. } else {
  123. callback();
  124. }
  125. };
  126. return {
  127. modalForm: { ...initModalForm },
  128. BOOLEAN_TYPE,
  129. DIRECTION_TYPE,
  130. topicType: null,
  131. rules: {
  132. topicType: [
  133. {
  134. required: true,
  135. validator: topicTypeValidater,
  136. trigger: "change",
  137. },
  138. ],
  139. topicNo: [
  140. {
  141. required: true,
  142. message: "请输入大题序号",
  143. trigger: "change",
  144. },
  145. ],
  146. topicName: [
  147. {
  148. required: true,
  149. message: "请输入题目名称",
  150. trigger: "change",
  151. },
  152. ],
  153. endNumber: [
  154. {
  155. required: true,
  156. message: "请输入起止题号",
  157. trigger: "change",
  158. },
  159. {
  160. type: "number",
  161. validator: numberRangeValidater,
  162. trigger: "change",
  163. },
  164. ],
  165. optionCount: [
  166. {
  167. required: true,
  168. type: "number",
  169. message: "请输入选项个数",
  170. trigger: "change",
  171. },
  172. ],
  173. },
  174. };
  175. },
  176. computed: {
  177. questionName() {
  178. return getFillQuestionName(this.modalForm);
  179. },
  180. },
  181. mounted() {
  182. this.initData(this.instance);
  183. },
  184. methods: {
  185. initData(val) {
  186. const valInfo = val.parent || val;
  187. if (val["_edit"]) this.topicType = this.getTopicType(valInfo);
  188. this.modalForm = Object.assign({}, initModalForm, valInfo);
  189. this.modalForm.endNumber =
  190. this.modalForm.startNumber + this.modalForm.questionsCount - 1;
  191. },
  192. topicTypeChange() {
  193. if (this.topicType === "single") {
  194. this.modalForm.isMultiply = false;
  195. this.modalForm.isBoolean = false;
  196. this.modalForm.optionCount = 4;
  197. } else if (this.topicType === "multiply") {
  198. this.modalForm.isMultiply = true;
  199. this.modalForm.isBoolean = false;
  200. this.modalForm.optionCount = 4;
  201. } else {
  202. this.modalForm.isMultiply = false;
  203. this.modalForm.isBoolean = true;
  204. this.modalForm.optionCount = 2;
  205. }
  206. },
  207. getTopicType(info) {
  208. if (info.isMultiply) return "multiply";
  209. if (info.isBoolean) return "boolean";
  210. return "single";
  211. },
  212. async submit() {
  213. const valid = await this.$refs.modalFormComp.validate().catch(() => {});
  214. if (!valid) return;
  215. this.modalForm.questionsCount =
  216. this.modalForm.endNumber - this.modalForm.startNumber + 1;
  217. this.modalForm.topicName = this.modalForm.topicName.trim();
  218. this.$emit("modified", this.modalForm);
  219. },
  220. },
  221. };
  222. </script>