EditFillQuestion.vue 6.0 KB

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