EditFillQuestion.vue 6.1 KB

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