Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the v-for index in Vue.js?

Tags:

vue.js

I have a Vue component like bellow:

<div v-for="item in items" :key="there I want get the for-loop index"  >

</div>

... 

data(){
  items: [{name:'a'}, {name:'b'}...]
}

How can I get the index when I execute the for-loop in my vue.js?

like image 439
sof-03 Avatar asked Apr 05 '18 15:04

sof-03


People also ask

What is V-for in Vue JS?

v-for directive is a Vue. js directive used to loop over a data usually an array or object. First, we will create a div element with id as app and let's apply the v-for directive to an element with data.

What is key in V-for?

The purpose of this key attribute is to give "a hint for Vue's virtual DOM algorithm to identify VNodes when diffing the new list of nodes against the old list" (from Vue. js Docs). Essentially, it helps Vue identify what's changed and what hasn't.

How do I get element height in Vue?

To get an element's height with Vue. js, we can assign a ref to the element we want to get the height for. Then we can get the height with the clientHeight property of the element that's been assigned the ref. We assigned the ref with the ref prop set to a name.

What is the shorthand for V-bind in Vue?

In the shorthand, everything before the argument (i.e. v-bind: ) is condensed into a single character, : . Here the argument is the event name to listen to: click . v-on has a corresponding shorthand, namely the @ character.

How to get index of each item from Index in Vue?

We get the index of each item from index. To get the v-for index in Vue.js, we can get it from the 2nd parameter.

How do I render a list of items in Vue JS?

Vue.js supports rendering lists of items onto the browser using the built-in v-for< core directive. In this post, we will explore how v-for can be used in Vue applications.

What is V-model in Vue JS?

V-model in vue.js is defined to make a two-way binding process to speed up the development of web application by bundled with Vue.js. This v-model directive helps to bind a value to the component data also triggers the event whenever a user presses a key or paste option.

How to track each element in vuejs?

We can set Vue to track each element using a key. This would cause it to move elements rather than replacing values. This value should be unique to each element that’s being iterated over.


3 Answers

Declare an index variable:

<div v-for="(item, index) in items" :key="item.name">

</div>

Demo:

new Vue({
  el: '#app',
  data: {
    items: [{name: 'a'}, {name: 'b'}]
  }
})
<script src="https://unpkg.com/vue"></script>

<div id="app">
  <div v-for="(item, index) in items" :key="item.name">
    {{ index }}: {{ item.name }}
  </div>
</div>

Official docs section - Mapping an Array to Elements with v-for (emphasis mine):

Inside v-for blocks we have full access to parent scope properties. v-for also supports an optional second argument for the index of the current item.

like image 53
acdcjunior Avatar answered Oct 20 '22 09:10

acdcjunior


Create a new method:

methods: {
    incrementIndex(key) {
        return key + 1;
    },
},

If the array keys are numbered, starting with zero like items[0], items[1], etc.., you can use the array's keys:

<div v-for="(item, key) in items" :key="key">
    {{ incrementIndex(key) }}
</div>

But if the array's keys are typeof String then you can do:

<div v-for="(item, key, index) in items" :key="key">
    {{ incrementIndex(index) }}
</div>

The second version uses the counter from the v-for loop.

like image 7
Yidir Avatar answered Oct 20 '22 07:10

Yidir


You can use `$index` to get the index of v-for.

<div v-for="item in items" :key="`$index`"  >

</div>

and the other method:

<div v-for="(item, index) in items" :key="index"  >

</div>
like image 6
aircraft Avatar answered Oct 20 '22 09:10

aircraft