EditFillQuestion.vue 5.9 KB

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