EditFillQuestion.vue 6.1 KB

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