EditFillQuestion.vue 5.0 KB

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