FillBlankQuestion.vue 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <template>
  2. <div class="fill-blank-question">
  3. <el-form
  4. ref="modalFormComp"
  5. :model="modalForm"
  6. :rules="rules"
  7. label-width="100px"
  8. >
  9. <el-form-item prop="quesBody" label="题干">
  10. <v-editor
  11. v-model="modalForm.quesBody"
  12. enable-answer-point
  13. @answer-point-changed="answerPointsChange"
  14. @change="quesBodyChange"
  15. ></v-editor>
  16. </el-form-item>
  17. <el-form-item label="答案"></el-form-item>
  18. <el-form-item
  19. v-for="(answer, oindex) in modalForm.quesAnswer"
  20. :key="oindex"
  21. :prop="`quesAnswer.${oindex}.body`"
  22. :rules="answerRule"
  23. >
  24. <div class="question-edit-option">
  25. <div class="option-check">({{ oindex + 1 }})</div>
  26. <div class="option-body">
  27. <v-editor v-model="answer.body"></v-editor>
  28. </div>
  29. </div>
  30. </el-form-item>
  31. <el-form-item label="答案解析">
  32. <v-editor v-model="modalForm.comment"></v-editor>
  33. </el-form-item>
  34. </el-form>
  35. <question-info-edit
  36. ref="QuestionInfoEdit"
  37. :question="modalForm"
  38. @change="questionInfoChange"
  39. ></question-info-edit>
  40. </div>
  41. </template>
  42. <script>
  43. import { isAnEmptyRichText } from "@/utils/utils";
  44. import { getInitQuestionModel } from "../model/questionModel";
  45. import QuestionInfoEdit from "../QuestionInfoEdit.vue";
  46. export default {
  47. name: "FillBlankQuestion",
  48. components: { QuestionInfoEdit },
  49. props: {
  50. question: {
  51. type: Object,
  52. default() {
  53. return {};
  54. },
  55. },
  56. },
  57. data() {
  58. return {
  59. modalForm: {},
  60. rules: {
  61. quesBody: [
  62. {
  63. validator: (rule, value, callback) => {
  64. if (!value || isAnEmptyRichText(value)) {
  65. return callback(new Error(`请输入题干`));
  66. }
  67. callback();
  68. },
  69. trigger: "change",
  70. },
  71. ],
  72. },
  73. answerRule: {
  74. validator: (rule, value, callback) => {
  75. if (!value || !value.length) {
  76. return callback(new Error(`请输入答案内容`));
  77. }
  78. callback();
  79. },
  80. trigger: "change",
  81. },
  82. };
  83. },
  84. created() {
  85. this.initData();
  86. },
  87. methods: {
  88. initData() {
  89. this.modalForm = this.$objAssign(
  90. getInitQuestionModel("FILL_BLANK_QUESTION"),
  91. this.question
  92. );
  93. this.modalForm.quesAnswer = this.modalForm.quesAnswer.map((item) => {
  94. return {
  95. body: item,
  96. };
  97. });
  98. },
  99. answerPointsChange(answerPointsCount) {
  100. console.log(answerPointsCount);
  101. let curAnswerPointsCount = this.modalForm.quesAnswer.length;
  102. if (curAnswerPointsCount === answerPointsCount) return;
  103. if (curAnswerPointsCount > answerPointsCount) {
  104. this.modalForm.quesAnswer = this.modalForm.quesAnswer.slice(
  105. 0,
  106. answerPointsCount
  107. );
  108. } else {
  109. for (let i = curAnswerPointsCount; i < answerPointsCount; i++) {
  110. this.modalForm.quesAnswer.push({
  111. body: null,
  112. });
  113. }
  114. }
  115. },
  116. quesBodyChange() {
  117. this.$refs.modalFormComp.validateField(`quesBody`, () => {});
  118. },
  119. questionInfoChange(questionInfo) {
  120. this.modalForm = Object.assign({}, this.modalForm, questionInfo);
  121. },
  122. validate() {
  123. return this.$refs.modalFormComp.validate();
  124. },
  125. getData() {
  126. let data = { ...this.modalForm };
  127. data.quesAnswer = this.modalForm.quesAnswer.map((item, index) => {
  128. return {
  129. ...item.body,
  130. index,
  131. };
  132. });
  133. return data;
  134. },
  135. },
  136. };
  137. </script>