2

My vue component like this :

<template>
    <div class="row">
        <div class="col-md-3" v-for="item1 in items1">
            ...
        </div>
        <div class="col-md-3" v-for="item2 in items2">
            ...
        </div>
        <div class="col-md-3" v-for="item3 in items3">
            ...
        </div>
    </div>
</template>
<script>
    export default {
        ...
        computed: {
            items1() {
                const n = ... // this is object
                return n
            },
            items2() {
                const n = ... // this is object
                return n
            },
            items3() {
                const n = ... // this is object
                return n
            }
        },
        ...
    }
</script>

If the three loop complete, I want to call a method

So the method is executed when the three loop completes

How can I do it?

1
  • Why would you ever want something like this? You should never let the presentation control anything, it's the other way around! Commented Jul 20, 2017 at 18:06

1 Answer 1

5

As promised, here is the example.

var counter = 0

const vm = new Vue({
  el: '#app',

  computed: {
    items1() {
      return {item1: 'value1', item2: 'value2'}
    },
    items2() {
      return {item1: 'value3', item2: 'value4'}
    },
    items3() {
      return {item1: 'value5', item2: 'value6'}
    }
  },

  methods: {
    callback() {
      counter++
      console.log('v-for loop finished')
      var numberOfLoops = 3
      if (counter >= numberOfLoops) {
        console.log('All loops have finished executing.')
        counter = 0
      }
    }
  },

  directives: {
    forCallback(el, binding, vnode) {
      let element = binding.value
      var key = element.key
      var len = 0

      if (Array.isArray(element.array)) {
        len = element.array.length
      }

      else if (typeof element.array === 'object') {
        var keys = Object.keys(element.array)
        key = keys.indexOf(key)
        len = keys.length
      }

      if (key == len - 1) {
        if (typeof element.callback === 'function') {
          (element.callback.bind(vnode.context))()
        }
      }
    }
  },

})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.3.4/vue.min.js"></script>

<div id="app">
  <div class="row">
    <div class="col-md-3" v-for="(item, key) in items1" v-for-callback="{key: key, array: items1, callback: callback}">
      ...
    </div>

    <div class="col-md-3" v-for="(item, key) in items2" v-for-callback="{key: key, array: items2, callback: callback}">
      ...
    </div>

    <div class="col-md-3" v-for="(item, key) in items3" v-for-callback="{key: key, array: items3, callback: callback}">
      ...
    </div>
  </div>
</div>

Sign up to request clarification or add additional context in comments.

12 Comments

Perfect... I forgot about the re-rending,,,,my mistake , and thank you for pointing it out...+1 :)
@VamsiKrishna My pleasure :)
@Ikbel, I use export default{...} like my code on the question. Where I put this var counter = 0?
@TrendingNews see my edited answer. Instead of defining counter outside of your vue instance. Use mounted callback to create it within Vue without making it reactive. This should be a better solution.
@Ikbel, Okay. Thanks. I try like this : <script> let counter = 0 export default { ... } </script. I also works.
|

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.