Home  >  Q&A  >  body text

The re-expressed title is: v-if based on DOM updates

I have some elements rendered using v-for Each element contains text and buttons I need to show the button only when the text overflows the height of the div

<div v-for="el in elements" :key="el.id">
    <span>{{ el.text }}</span>
    <button>Click me</button>
</div>

The obvious solution is to use v-if, but on what basis should I base my judgment? I need to calculate the height of the text and decide whether to show the button or not Therefore, I need to use refs to reference the divs and a function to determine whether to display:

<template>
    <button @click="addDiv"> 点击添加div </button>
    <div v-for="(el, index) in elements" :key="el.id">
        <span ref="items">{{ el.text }}</span>
        <button v-if="showButton(index)">Click me</button>
    </div>
</template>

<script setup lang="ts">
//imports
const elements = ref([]);
const addDiv = function() { 
    elements.value.push({ text: "测试", id: Date.now() })
}
const items = ref();
const showButton = function (index) {
    const item = items.value[index] as HTMLElement;
    return item.scrollHeight > item.offsetHeight
}
</script>

But I found that the problem is that items is out of sync with the DOM. So obviously, the DOM is updated asynchronously and that's why my data is a bit late So I decided to add nextTick() in my showButton function, but it started returning Promise which caused the v-if to always be true

<template>
    <button @click="addDiv"> 点击添加div </button>
    <div v-for="(el, index) in elements" :key="el.id">
        <span ref="items">{{ el.text }}</span>
        <button v-if="showButton(index)">Click me</button>
    </div>
</template>

<script setup lang="ts">
//imports
const elements = ref([]);
const addDiv = function() { 
    elements.value.push({ text: "测试", id: Date.now() })
}
const items = ref();
const showButton = function (index) {
    nextTick(() => {
        const item = items.value[index] as HTMLElement;
        return item.scrollHeight > item.offsetHeight
    })
}
</script>

So is there a way to show or hide my buttons specifically for each element?

P粉163951336P粉163951336405 days ago492

reply all(1)I'll reply

  • P粉187677012

    P粉1876770122023-09-11 00:40:21

    I used watchers in Vue to complete this operation. I hope it can be helpful to you!

    <template>
      <button @click="addDiv">点击添加div</button>
      <div
        v-for="el in elements"
        :key="el.id"
      >
        <span ref="items">{{ el.text }}</span>
        <button v-if="el.showButton">点击我</button>
      </div>
    </template>

    And the script part, I have updated the input part:

    <script setup lang="ts">
      //imports
      import { ref, watch } from 'vue';
    
      const elements = ref<Array<any>>([]);
      const items = ref<Array<HTMLElement>>([]);
    
      const addDiv = function () {
        elements.value.push({ text: '测试', id: Date.now(), showButton: false });
      };
    
      watch(items.value, (newItems) => {
        console.log('items changed');
        let cpt = 0;
    
        // you can do it with a for loop too
        newItems.forEach((item) => {
          if (item.scrollHeight > item.offsetHeight) {
            console.log('overflow -> show button');
            elements.value[cpt].showButton = true;
          }
          cpt++;
        });
      });
    </script>

    reply
    0
  • Cancelreply