tiankong.vue 3.4 KB

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