tiankong.vue 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <template>
  2. <view class="fill-in-the-blank">
  3. <!-- 显示填空后的文本 -->
  4. <view class="formatted-text" v-html="formattedText" :question="question" :myflag="myflag"
  5. :change:myflag="TK.updateFlag" :change:question="TK.watchQuestionChange"></view>
  6. <!-- 提供与占位符数量相匹配的输入框 -->
  7. <template v-for="(placeholder, index) in placeholders" :key="index">
  8. <FillItem :value="question.reply[index]" :ref="`popup${index}Ref`" :index="index" @blur="onBlur"></FillItem>
  9. </template>
  10. </view>
  11. </template>
  12. <script>
  13. import FillItem from "./FillItem.vue";
  14. export default {
  15. components: {
  16. FillItem
  17. },
  18. props: {
  19. placeholders: { // 占位符
  20. type: Array,
  21. required: true
  22. },
  23. question: {
  24. type: Object,
  25. },
  26. showError: {
  27. type: Boolean,
  28. default: false
  29. }
  30. },
  31. data() {
  32. return {
  33. myflag: 0
  34. }
  35. },
  36. computed: {
  37. // 计算属性,用于生成填空后的文本
  38. formattedText() {
  39. let result = this.question.name;
  40. this.placeholders.forEach((placeholder, index) => {
  41. const regex2 = new RegExp(`\\<img `, 'g');
  42. result = result.replace(regex2, '<image ');
  43. // 使用正则表达式全局替换占位符
  44. const regex = new RegExp(`\\${placeholder}`, 'g');
  45. if (this.showError) {
  46. // 答题状态默认错误
  47. let answer_status = 'showError';
  48. // 更新答题状态正确
  49. this.question.reply[index] && this.question.result[index].some(citem => citem === this
  50. .question.reply[index].trim()) && (answer_status = 'active_right');
  51. result = result.replace(regex,
  52. `<input class="tiankong-input ${answer_status}" style="width: 50px;margin: 0 3px" id="t_${this.question.stId}_${index}" readonly data-index="${index}" value="${this.question.reply[index]}" />`
  53. );
  54. } else {
  55. result = result.replace(regex,
  56. `<input class="tiankong-input" style="width: 50px;margin: 0 3px" id="t_${this.question.stId}_${index}" readonly data-index="${index}" value="${this.question.reply[index]}" />`
  57. );
  58. }
  59. });
  60. this.myflag++;
  61. return result;
  62. },
  63. },
  64. methods: {
  65. setResult(data) {
  66. const {
  67. index,
  68. result
  69. } = data;
  70. this.question.reply[index] = result;
  71. },
  72. onBlur(data) {
  73. const {
  74. index,
  75. result
  76. } = data;
  77. this.setResult(data);
  78. },
  79. showPopup(str) {
  80. this.$refs[str][0].showPopup();
  81. }
  82. },
  83. }
  84. </script>
  85. <script module="TK" lang="renderjs">
  86. export default {
  87. data() {
  88. return {
  89. myQ: null
  90. }
  91. },
  92. methods: {
  93. updateFlag() {
  94. this.initListener(this.myQ)
  95. },
  96. updateFn(e) {
  97. const str = `popup${e.target.dataset.index}Ref`;
  98. this.$ownerInstance.callMethod('showPopup', str);
  99. },
  100. watchQuestionChange(newValue, oldValue, ownerInstance, instance) {
  101. if (newValue) {
  102. this.myQ = newValue;
  103. this.initListener(newValue)
  104. }
  105. },
  106. initListener(question) {
  107. if (!question) {
  108. return;
  109. }
  110. question.result.forEach((item, index) => {
  111. const dom = document.getElementById(`t_${question.stId}_${index}`)
  112. dom && dom.addEventListener('focus', (e) => this.updateFn(e))
  113. })
  114. },
  115. },
  116. }
  117. </script>
  118. <style scoped>
  119. .fill-in-the-blank {
  120. width: 100%;
  121. }
  122. .formatted-text {
  123. margin-bottom: 10px;
  124. font-size: 16px;
  125. line-height: 1.5;
  126. }
  127. .inputs-container {
  128. display: flex;
  129. flex-wrap: wrap;
  130. gap: 10px;
  131. }
  132. .input-box {
  133. padding: 5px;
  134. width: 150px;
  135. /* 可根据需要调整宽度 */
  136. border: 1px solid #ccc;
  137. border-radius: 4px;
  138. }
  139. .tiankong-input {
  140. width: 50px;
  141. }
  142. </style>