tiankong.vue 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <template>
  2. <div class="fill-in-the-blank">
  3. <!-- 显示填空后的文本 -->
  4. <div class="formatted-text" v-html="formattedText"></div>
  5. <!-- 提供与占位符数量相匹配的输入框 -->
  6. <template v-for="(placeholder, index) in placeholders" :key="index">
  7. <FillItem :value="question.reply[index]" :ref="`popup${index}Ref`" :index="index" @blur="onBlur"></FillItem>
  8. </template>
  9. </div>
  10. </template>
  11. <script>
  12. import FillItem from "./FillItem.vue";
  13. export default {
  14. components: {
  15. FillItem
  16. },
  17. props: {
  18. placeholders: { // 占位符
  19. type: Array,
  20. required: true
  21. },
  22. question: {
  23. type: Object,
  24. },
  25. showError: {
  26. type: Boolean,
  27. default: false
  28. }
  29. },
  30. methods: {
  31. setResult(data) {
  32. const {index,result} = data;
  33. this.question.reply[index] = result;
  34. }
  35. }
  36. }
  37. </script>
  38. <script module="TK" lang="renderjs">
  39. export default {
  40. props: {
  41. placeholders: { // 占位符
  42. type: Array,
  43. required: true
  44. },
  45. question: {
  46. type: Object,
  47. },
  48. showError: {
  49. type: Boolean,
  50. default: false
  51. }
  52. },
  53. data() {
  54. return {
  55. };
  56. },
  57. computed: {
  58. // 计算属性,用于生成填空后的文本
  59. formattedText() {
  60. let result = this.question.name;
  61. this.placeholders.forEach((placeholder, index) => {
  62. // 使用正则表达式全局替换占位符
  63. const regex = new RegExp(`\\${placeholder}`, 'g');
  64. result = result.replace(regex,
  65. `<input id="t_${this.question.stId}_${index}" readonly data-index="${index}" value="${this.question.reply[index]}" />`);
  66. });
  67. // 追加监听事件
  68. this.$nextTick(() => {
  69. this.initListener();
  70. })
  71. return result;
  72. },
  73. },
  74. methods: {
  75. onBlur(data) {
  76. const {index,result} = data;
  77. this.question.reply[index] = result;
  78. this.$ownerInstance.callMethod('setResult', data)
  79. },
  80. updateFn(e) {
  81. const str = `popup${e.target.dataset.index}Ref`;
  82. console.log(this.$refs[str],str)
  83. this.$refs[str][0].showPopup();
  84. },
  85. initListener() {
  86. this.question.result.forEach((item,index) => {
  87. document.getElementById(`t_${this.question.stId}_${index}`).addEventListener('focus', (e) => this.updateFn(e))
  88. })
  89. },
  90. },
  91. }
  92. </script>
  93. <style scoped>
  94. .fill-in-the-blank {
  95. width: 100%;
  96. }
  97. .formatted-text {
  98. margin-bottom: 10px;
  99. font-size: 16px;
  100. line-height: 1.5;
  101. }
  102. .inputs-container {
  103. display: flex;
  104. flex-wrap: wrap;
  105. gap: 10px;
  106. }
  107. .input-box {
  108. padding: 5px;
  109. width: 150px;
  110. /* 可根据需要调整宽度 */
  111. border: 1px solid #ccc;
  112. border-radius: 4px;
  113. }
  114. </style>