tiankong.vue 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. name: 'FillInTheBlank',
  15. props: {
  16. placeholders: { // 占位符
  17. type: Array,
  18. required: true
  19. },
  20. question: {
  21. type: Object,
  22. },
  23. showError: {
  24. type: Boolean,
  25. default: false
  26. }
  27. },
  28. data() {
  29. return {
  30. answers: this.placeholders.map(() => '') // 初始化答案数组,与占位符数量一致
  31. };
  32. },
  33. computed: {
  34. // 计算属性,用于生成填空后的文本
  35. formattedText() {
  36. let result = this.question.name;
  37. this.placeholders.forEach((placeholder, index) => {
  38. // 使用正则表达式全局替换占位符
  39. const regex = new RegExp(`\\${placeholder}`, 'g');
  40. result = result.replace(regex,
  41. `<input id="t_${this.question.stId}_${index}" readonly data-index="${index}" value="${this.question.reply[index]}" />`);
  42. });
  43. // 追加监听事件
  44. this.$nextTick(() => {
  45. this.initListener();
  46. })
  47. return result;
  48. },
  49. },
  50. methods: {
  51. onBlur(data) {
  52. const {index,result} = data;
  53. this.question.reply[index] = result;
  54. },
  55. updateFn(e) {
  56. // console.log('e',e.target.dataset.index);
  57. const str = `popup${e.target.dataset.index}Ref`;
  58. console.log(this.$refs[str],str)
  59. this.$refs[str][0].showPopup();
  60. },
  61. initListener() {
  62. question.result.forEach((item,index) => {
  63. document.getElementById(`t_${item.stId}_${index}`).addEventListener('focus', (e) => this.updateFn(e))
  64. })
  65. }
  66. },
  67. mounted() {
  68. }
  69. };
  70. </script>
  71. <style scoped>
  72. .fill-in-the-blank {
  73. width: 100%;
  74. }
  75. .formatted-text {
  76. margin-bottom: 10px;
  77. font-size: 16px;
  78. line-height: 1.5;
  79. }
  80. .inputs-container {
  81. display: flex;
  82. flex-wrap: wrap;
  83. gap: 10px;
  84. }
  85. .input-box {
  86. padding: 5px;
  87. width: 150px;
  88. /* 可根据需要调整宽度 */
  89. border: 1px solid #ccc;
  90. border-radius: 4px;
  91. }
  92. </style>