EditFillQuestion.vue 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. <template>
  2. <el-dialog
  3. class="edit-fill-question edit-dialog"
  4. :visible.sync="dialogIsShow"
  5. title="选择题"
  6. top="10vh"
  7. width="640px"
  8. :modal="false"
  9. :close-on-click-modal="false"
  10. :close-on-press-escape="false"
  11. append-to-body
  12. @open="opened"
  13. @close="closed"
  14. >
  15. <el-form
  16. ref="modalFormComp"
  17. :model="modalForm"
  18. :rules="rules"
  19. :key="modalForm.id"
  20. label-width="100px"
  21. >
  22. <el-form-item prop="topicNo" label="大题题号:">
  23. <el-input-number
  24. style="width:125px;"
  25. v-model.number="modalForm.topicNo"
  26. :min="1"
  27. :max="20"
  28. :step="1"
  29. step-strictly
  30. :controls="false"
  31. ></el-input-number>
  32. </el-form-item>
  33. <el-form-item prop="topicName" label="题目名称:">
  34. <el-input
  35. v-model.trim="modalForm.topicName"
  36. size=""
  37. placeholder="请输入题目名称"
  38. clearable
  39. ></el-input>
  40. </el-form-item>
  41. <el-form-item prop="endNumber" label="起止题号:">
  42. <el-input-number
  43. style="width:40px;"
  44. v-model="modalForm.startNumber"
  45. :min="0"
  46. :max="100"
  47. :step="1"
  48. step-strictly
  49. :controls="false"
  50. ></el-input-number>
  51. <span class="el-input-split"></span>
  52. <el-input-number
  53. style="width:40px;"
  54. v-model="modalForm.endNumber"
  55. :min="modalForm.startNumber"
  56. :max="100"
  57. :step="1"
  58. step-strictly
  59. :controls="false"
  60. ></el-input-number>
  61. </el-form-item>
  62. <el-form-item prop="optionCount" label="选项个数:">
  63. <el-input-number
  64. style="width:125px;"
  65. v-model="modalForm.optionCount"
  66. :min="2"
  67. :max="22"
  68. :step="1"
  69. step-strictly
  70. :controls="false"
  71. :disabled="modalForm.isFill"
  72. ></el-input-number>
  73. </el-form-item>
  74. <el-form-item>
  75. <el-checkbox
  76. v-model="modalForm.isFill"
  77. @change="selectTypeChange"
  78. :disabled="modalForm.isMultiply"
  79. >判断题</el-checkbox
  80. >
  81. </el-form-item>
  82. <el-form-item>
  83. <el-checkbox v-model="modalForm.isMultiply" :disabled="modalForm.isFill"
  84. >多选</el-checkbox
  85. >
  86. </el-form-item>
  87. </el-form>
  88. <div slot="footer">
  89. <el-button type="primary" @click="submit">确认</el-button>
  90. <el-button @click="cancel" plain>取消</el-button>
  91. </div>
  92. </el-dialog>
  93. </template>
  94. <script>
  95. const initModalForm = {
  96. id: "",
  97. topicNo: null,
  98. topicName: "",
  99. startNumber: 1,
  100. endNumber: 5,
  101. questionsCount: 10,
  102. optionCount: 5,
  103. isFill: false,
  104. isMultiply: false
  105. };
  106. export default {
  107. name: "edit-fill-question",
  108. props: {
  109. instance: {
  110. type: Object,
  111. default() {
  112. return {};
  113. }
  114. },
  115. topicNos: {
  116. type: Array,
  117. default() {
  118. return [];
  119. }
  120. }
  121. },
  122. data() {
  123. const numberRangeValidater = (rule, value, callback) => {
  124. if (!this.modalForm.startNumber || !this.modalForm.endNumber) {
  125. return callback(new Error("请输入起止题号"));
  126. }
  127. if (this.modalForm.startNumber > this.modalForm.endNumber) {
  128. callback(new Error("开始题号不能大于结束题号"));
  129. } else {
  130. callback();
  131. }
  132. };
  133. const topicNoValidater = (rule, value, callback) => {
  134. if (!this.instance.topicNo) {
  135. // 新增题目
  136. if (this.topicNos.includes(value)) {
  137. callback(new Error("当前大题题号已经存在,请重新输入"));
  138. } else {
  139. callback();
  140. }
  141. } else {
  142. // 修改题目
  143. if (value !== this.instance.topicNo && this.topicNos.includes(value)) {
  144. callback(new Error("当前大题题号已经存在,请重新输入"));
  145. } else {
  146. callback();
  147. }
  148. }
  149. };
  150. return {
  151. dialogIsShow: false,
  152. modalForm: { ...initModalForm },
  153. rules: {
  154. topicName: [
  155. {
  156. required: true,
  157. message: "请输入题目名称",
  158. trigger: "change"
  159. }
  160. ],
  161. topicNo: [
  162. {
  163. required: true,
  164. message: "请输入大题题号",
  165. trigger: "change"
  166. },
  167. {
  168. type: "number",
  169. validator: topicNoValidater,
  170. trigger: "change"
  171. }
  172. ],
  173. endNumber: [
  174. {
  175. required: true,
  176. message: "请输入起止题号",
  177. trigger: "change"
  178. },
  179. {
  180. type: "number",
  181. validator: numberRangeValidater,
  182. trigger: "change"
  183. }
  184. ],
  185. optionCount: [
  186. {
  187. required: true,
  188. type: "number",
  189. message: "请输入选项个数",
  190. trigger: "change"
  191. }
  192. ]
  193. }
  194. };
  195. },
  196. methods: {
  197. initData(val) {
  198. const valInfo = val.parent || val;
  199. this.modalForm = { ...valInfo };
  200. this.modalForm.endNumber =
  201. this.modalForm.startNumber + this.modalForm.questionsCount - 1;
  202. },
  203. selectTypeChange(val) {
  204. if (val) {
  205. this.modalForm.optionCount = 2;
  206. this.modalForm.isMultiply = false;
  207. }
  208. },
  209. opened() {
  210. this.initData(this.instance);
  211. },
  212. closed() {
  213. this.$emit("closed");
  214. },
  215. cancel() {
  216. this.dialogIsShow = false;
  217. },
  218. open() {
  219. this.dialogIsShow = true;
  220. },
  221. async submit() {
  222. const valid = await this.$refs.modalFormComp.validate().catch(() => {});
  223. if (!valid) return;
  224. this.modalForm.questionsCount =
  225. this.modalForm.endNumber - this.modalForm.startNumber + 1;
  226. this.$emit("modified", this.modalForm);
  227. this.cancel();
  228. }
  229. }
  230. };
  231. </script>