2

Say I have a group of cars and I want to display each row...3 seconds at a time. How can I do this in Vuejs2?

    <tbody>
      <tr v-for="(car) in cars">
        <td><img v-bind:src="car.photo" width="40px" height="40px" alt=""></td>
        <td><router-link :to="{path:'/car/' + car.id}" >{{ car.name }}</router-link></td>
        <td>{{ car.make }}</td>
        <td></td>
        <td>{{ car.created }}</td>
      </tr>
    </tbody>

2 Answers 2

1

something like this.

  1. stored what to show currently in currentCarIndex.
  2. use setInterval to change currentCarIndex every 3 seconds
  3. btw, v-for and v-if shouldn't be used together, so I add a <template> tag as an empty wrapper to execute v-for

<template>
    <tbody>
        <template v-for="(car,i) in cars">
            <tr :key="i" v-if="i<=currentCarIndex">
                <td><img v-bind:src="car.photo" width="40px" height="40px" alt=""></td>
                <td>
                    <router-link :to="{path:'/car/' + car.id}">{{ car.name }}</router-link>
                </td>
                <td>{{ car.make }}</td>
                <td></td>
                <td>{{ car.created }}</td>
            </tr>
        </template>
    </tbody>
</template>
<script>
export default {
  data() {
    return {
      currentCarIndex: 0,
      cars: "..."
    };
  },
  mounted() {
    const interval = setInterval(() => {
      if (this.currentCarIndex + 1 < this.cars.length) this.currentCarIndex++;
      else clearInterval(interval);
    }, 3000);
  }
};
</script>
Sign up to request clarification or add additional context in comments.

4 Comments

Hmm, well they appear one after the other just sort of on top of each other. Is there a way to have them display one row at a time so they all appear at the end in one table with many rows?
@Jessi is it that you want to add 1 row at a time ? for eg, display 1 row at first, display 2 rows 3 sec later, display 3 rows 6 sec later... ? if that is the case, simply change the v-if to v-if="i<=currentCarIndex" will do
Oh this is VERY VERY close to what I'm after. (See the "Great" image that FrenchMajesty posted below.) Each row displays after the next now but for some reason it just keeps going in a loop.
@Jessi because i misunderstood the question and kept resetting the index to 0 again. I have updated the answer to not reset it to 0.
0

I was having this exact problem a couple of hours ago on an app I'm working on. I have a list of reviews and I wanted the reviews to display at interval so that it looks like the list is 'filled in' top down so that I can create a cascading effect. Something like this:

List cascading

The documentations points out that you can use transition-group but personally I wasn't able to get them working for me so what I did is I created a wrapper component with a delay property on it and I passed in the time the component should wait before rendering. I did this using a simple v-if in the component's template.

What you could do is add a show-in and visible-for prop to a wrapper component like this:

<flashing-row v-for="(car, i) in cars" :show-in="i * 3000" :visible-for="2900">
 // Stuff inside my row here....
</flashing-row>

and then define flashing-row like this:

 Vue.component('flashing-row', {
    props: {
    showIn: {
      type: Number,
      required: true,
    },
    visibleFor: {
      type: Number,
      required: true,
    },
  },
  data() {
    return {
      isVisible: false,
    };
  },
  created() {
    setTimeout(() => {
      // Make component visible
      this.isVisible = true;
      // Create timer to hide component after 'visibleFor' milliseconds
      setTimeout(() => this.isVisible = false, this.visibleFor);
    }, this.showIn);
  },
   template: '<tr v-if="isVisible"><slot></slot></tr>'
   });

You can see an example of the code in JSFiddle. This approach is especially good because:

  • You don't repeat yourself if you're going to be doing this at more than one place.
  • Makes your code more maintainable and easier to browse, read, and thus understand and modify later on.
  • And of course you can play around with the props and expand on it depending on what you need. The possibilities are really endless.

3 Comments

the animation you display is exactly what I'm after. I did try what you created but it just plum exploded in my browser :)
@Jessi Could it be an issue with the styling of the page that breaks or do you get a JavaScript error in the console?
@Jessi Also note that the code does NOT do the same thing as the image animation does but something similar. If you do want rows to appear slowly after one another you can just remove the visible-for prop from the component and the associated setTimeout.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.