TextAnswerQuestion.vue 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <template>
  2. <div class="text-answer-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. @change="quesBodyChange"
  13. ></v-editor>
  14. </el-form-item>
  15. <el-form-item prop="quesAnswer" label="答案">
  16. <v-editor v-model="quesAnswer" @change="answerChange"></v-editor>
  17. </el-form-item>
  18. <el-form-item label="答案解析">
  19. <v-editor v-model="modalForm.comment"></v-editor>
  20. </el-form-item>
  21. </el-form>
  22. <question-info-edit
  23. ref="QuestionInfoEdit"
  24. :question="modalForm"
  25. @change="questionInfoChange"
  26. ></question-info-edit>
  27. </div>
  28. </template>
  29. <script>
  30. import { isAnEmptyRichText } from "@/utils/utils";
  31. import { getInitQuestionModel } from "../model/questionModel";
  32. import QuestionInfoEdit from "../QuestionInfoEdit.vue";
  33. export default {
  34. name: "TextAnswerQuestion",
  35. components: { QuestionInfoEdit },
  36. props: {
  37. question: {
  38. type: Object,
  39. default() {
  40. return {};
  41. },
  42. },
  43. },
  44. data() {
  45. return {
  46. modalForm: {},
  47. rules: {
  48. quesBody: [
  49. {
  50. validator: (rule, value, callback) => {
  51. if (!value || isAnEmptyRichText(value)) {
  52. return callback(new Error(`请输入题干`));
  53. }
  54. callback();
  55. },
  56. trigger: "change",
  57. },
  58. ],
  59. quesAnswer: [
  60. {
  61. validator: (rule, value, callback) => {
  62. if (!this.quesAnswer || isAnEmptyRichText(this.quesAnswer)) {
  63. return callback(new Error(`请输入答案`));
  64. }
  65. callback();
  66. },
  67. trigger: "change",
  68. },
  69. ],
  70. },
  71. quesAnswer: null,
  72. };
  73. },
  74. created() {
  75. this.initData();
  76. },
  77. methods: {
  78. initData() {
  79. this.modalForm = this.$objAssign(
  80. getInitQuestionModel("TEXT_ANSWER_QUESTION"),
  81. this.question
  82. );
  83. this.modalForm.quesAnswer = this.question.quesAnswer
  84. ? JSON.parse(this.question.quesAnswer)
  85. : [];
  86. this.quesAnswer = this.modalForm.quesAnswer[0] || null;
  87. },
  88. quesBodyChange() {
  89. this.$refs.modalFormComp.validateField(`quesBody`, () => {});
  90. },
  91. answerChange() {
  92. this.modalForm.quesAnswer = [
  93. {
  94. index: 1,
  95. ...this.quesAnswer,
  96. },
  97. ];
  98. this.$refs.modalFormComp.validateField(`quesAnswer`, () => {});
  99. },
  100. questionInfoChange(questionInfo) {
  101. this.modalForm = Object.assign({}, this.modalForm, questionInfo);
  102. },
  103. validate() {
  104. return this.$refs.modalFormComp.validate();
  105. },
  106. getData() {
  107. let data = { ...this.modalForm };
  108. data.quesAnswer = JSON.stringify(data.quesAnswer);
  109. return data;
  110. },
  111. },
  112. };
  113. </script>