EditExplain.vue 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <template>
  2. <div class="edit-explain">
  3. <el-form
  4. ref="modalFormComp"
  5. :model="modalForm"
  6. :rules="rules"
  7. :key="modalForm.id"
  8. label-width="100px"
  9. >
  10. <!-- <el-form-item prop="topicNo" label="大题序号:">
  11. <el-input-number
  12. v-model="modalForm.topicNo"
  13. :min="1"
  14. :max="99"
  15. :step="1"
  16. step-strictly
  17. :controls="false"
  18. ></el-input-number>
  19. </el-form-item> -->
  20. <el-form-item prop="topicName" label="题目名称:">
  21. <el-input
  22. v-model="modalForm.topicName"
  23. type="textarea"
  24. :autosize="{ minRows: 2, maxRows: 5 }"
  25. resize="none"
  26. placeholder="请输入题目名称"
  27. clearable
  28. maxlength="100"
  29. show-word-limit
  30. ></el-input>
  31. </el-form-item>
  32. <el-form-item prop="startEnd" label="起止题号:">
  33. <el-input-number
  34. style="width: 40px"
  35. v-model="modalForm.startNumber"
  36. :min="0"
  37. :max="999"
  38. :step="1"
  39. step-strictly
  40. :controls="false"
  41. ></el-input-number>
  42. <span class="el-input-split"></span>
  43. <el-input-number
  44. style="width: 40px"
  45. v-model="modalForm.endNumber"
  46. :min="0"
  47. :max="999"
  48. :step="1"
  49. step-strictly
  50. :controls="false"
  51. ></el-input-number>
  52. </el-form-item>
  53. </el-form>
  54. </div>
  55. </template>
  56. <script>
  57. const initModalForm = {
  58. id: "",
  59. topicNo: null,
  60. topicName: "",
  61. startNumber: 1,
  62. endNumber: 4,
  63. questionsCount: 4,
  64. };
  65. export default {
  66. name: "edit-explain",
  67. props: {
  68. instance: {
  69. type: Object,
  70. default() {
  71. return {};
  72. },
  73. },
  74. },
  75. data() {
  76. const numberRangeValidater = (rule, value, callback) => {
  77. if (!this.modalForm.startNumber || !this.modalForm.endNumber) {
  78. return callback(new Error("请输入起止题号"));
  79. }
  80. if (this.modalForm.startNumber > this.modalForm.endNumber) {
  81. callback(new Error("开始题号不能大于结束题号"));
  82. } else {
  83. callback();
  84. }
  85. };
  86. return {
  87. modalForm: { ...initModalForm },
  88. rules: {
  89. topicNo: [
  90. {
  91. required: true,
  92. message: "请输入大题序号",
  93. trigger: "change",
  94. },
  95. ],
  96. topicName: [
  97. {
  98. required: true,
  99. message: "请输入题目名称",
  100. trigger: "change",
  101. },
  102. ],
  103. endNumber: [
  104. {
  105. required: true,
  106. message: "请输入起止题号",
  107. trigger: "change",
  108. },
  109. {
  110. type: "number",
  111. validator: numberRangeValidater,
  112. trigger: "change",
  113. },
  114. ],
  115. },
  116. };
  117. },
  118. mounted() {
  119. this.initData(this.instance);
  120. },
  121. methods: {
  122. initData(val) {
  123. const valInfo = val.parent || val;
  124. this.modalForm = { ...valInfo };
  125. this.modalForm.endNumber =
  126. this.modalForm.startNumber + this.modalForm.questionsCount - 1;
  127. },
  128. async submit() {
  129. const valid = await this.$refs.modalFormComp.validate().catch(() => {});
  130. if (!valid) return;
  131. this.modalForm.questionsCount =
  132. this.modalForm.endNumber - this.modalForm.startNumber + 1;
  133. this.modalForm.topicName = this.modalForm.topicName.trim();
  134. this.$emit("modified", this.modalForm);
  135. },
  136. },
  137. };
  138. </script>