MatchQuestion.vue 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <template>
  2. <div class="match-question">
  3. <el-form
  4. ref="modalFormComp"
  5. :model="modalForm"
  6. :rules="rules"
  7. label-width="100px"
  8. >
  9. <el-form-item v-if="IS_PARAGRAPH_MATCHING" prop="quesBody" label="题干">
  10. <v-editor
  11. v-model="modalForm.quesBody"
  12. @change="quesBodyChange"
  13. ></v-editor>
  14. </el-form-item>
  15. <el-form-item prop="quesAnswer" label="答案">
  16. <el-radio-group v-model="quesAnswer" @change="answerChange">
  17. <el-radio-button
  18. v-for="option in parentQuestion.quesOptions"
  19. :key="option.number"
  20. :label="option.number"
  21. >
  22. {{ (option.number - 1) | optionOrderWordFilter }}
  23. </el-radio-button>
  24. </el-radio-group>
  25. </el-form-item>
  26. </el-form>
  27. <question-info-edit
  28. ref="QuestionInfoEdit"
  29. :question="modalForm"
  30. @change="questionInfoChange"
  31. ></question-info-edit>
  32. </div>
  33. </template>
  34. <script>
  35. import { isAnEmptyRichText } from "@/utils/utils";
  36. import QuestionInfoEdit from "../QuestionInfoEdit.vue";
  37. import { getMatchQuestionModel } from "../model/questionModel";
  38. export default {
  39. name: "MatchQuestion",
  40. components: { QuestionInfoEdit },
  41. props: {
  42. question: {
  43. type: Object,
  44. default() {
  45. return {};
  46. },
  47. },
  48. parentQuestion: {
  49. type: Object,
  50. default() {
  51. return {
  52. quesOptions: [],
  53. questionType: "",
  54. };
  55. },
  56. },
  57. },
  58. data() {
  59. return {
  60. modalForm: {},
  61. quesAnswer: null,
  62. rules: {
  63. quesBody: [
  64. {
  65. validator: (rule, value, callback) => {
  66. if (!value || isAnEmptyRichText(value)) {
  67. return callback(new Error(`请输入题干`));
  68. }
  69. callback();
  70. },
  71. trigger: "change",
  72. },
  73. ],
  74. quesAnswer: [
  75. {
  76. validator: (rule, value, callback) => {
  77. if (!value || !value.length) {
  78. return callback(new Error(`请设置答案`));
  79. }
  80. callback();
  81. },
  82. trigger: "change",
  83. },
  84. ],
  85. },
  86. };
  87. },
  88. computed: {
  89. IS_PARAGRAPH_MATCHING() {
  90. return this.parentQuestion.questionType === "PARAGRAPH_MATCHING";
  91. },
  92. },
  93. created() {
  94. this.initData();
  95. },
  96. methods: {
  97. initData() {
  98. this.modalForm = this.$objAssign(getMatchQuestionModel(), this.question);
  99. this.quesAnswer = this.question.quesAnswer
  100. ? this.question.quesAnswer[0]
  101. : null;
  102. },
  103. quesBodyChange() {
  104. this.$refs.modalFormComp.validateField(`quesBody`, () => {});
  105. },
  106. answerChange() {
  107. this.modalForm.quesAnswer =
  108. this.quesAnswer || this.quesAnswer === 0 ? [this.quesAnswer] : [];
  109. this.$refs.modalFormComp.validateField(`quesAnswer`, () => {});
  110. },
  111. optionChange() {
  112. const optionCount = this.parentQuestion.quesOptions.length;
  113. if (!this.quesAnswer || this.quesAnswer <= optionCount) return;
  114. this.quesAnswer = null;
  115. this.answerChange();
  116. },
  117. questionInfoChange(questionInfo) {
  118. this.modalForm = Object.assign({}, this.modalForm, questionInfo);
  119. },
  120. validate() {
  121. return this.$refs.modalFormComp.validate();
  122. },
  123. getData() {
  124. return this.modalForm;
  125. },
  126. },
  127. };
  128. </script>