Vue.js List Rendering

List rendering is a fundamental aspect of building dynamic web applications. Vue.js provides powerful and flexible tools for rendering lists of items and managing dynamic data. Understanding how to effectively render lists is essential for any Vue.js developer. This chapter will cover the basics to advanced concepts of list rendering in Vue.js, with detailed examples and explanations.

Vue.js List Rendering

List rendering involves displaying multiple elements or components based on the items in a data collection, such as an array or an object. Vue.js uses the v-for directive to loop through data and render elements for each item.

Basic List Rendering with v-for

Rendering Arrays

The v-for directive is used to iterate over an array and render a list of items.

				
					<div id="app">
  <ul>
    <li v-for="item in items" :key="item.id">{{ item.text }}</li>
  </ul>
</div> <script type="litespeed/javascript">new Vue({el:'#app',data(){return{items:[{id:1,text:'Item 1'},{id:2,text:'Item 2'},{id:3,text:'Item 3'}]}}})</script> 
				
			

Explanation:

  • The v-for directive iterates over the items array.
  • Each item in the array is rendered as an <li> element.
  • The :key attribute ensures each item has a unique identifier for efficient DOM updates

Rendering Objects

You can also use v-for to iterate over the properties of an object.

				
					<div id="app">
  <ul>
    <li v-for="(value, key) in object" :key="key">{{ key }}: {{ value }}</li>
  </ul>
</div> <script type="litespeed/javascript">new Vue({el:'#app',data(){return{object:{name:'Vue.js',version:'3.0',type:'Framework'}}}})</script> 
				
			

Explanation:

  • The v-for directive iterates over the properties of the object.
  • Each property is rendered as an <li> element displaying the key and value.

Key Attributes in List Rendering

Using the :key attribute in v-for loops is crucial for performance and correctness. It helps Vue identify which items have changed, been added, or removed.

				
					<div id="app">
  <ul>
    <li v-for="item in items" :key="item.id">{{ item.text }}</li>
  </ul>
</div> <script type="litespeed/javascript">new Vue({el:'#app',data(){return{items:[{id:1,text:'Item 1'},{id:2,text:'Item 2'},{id:3,text:'Item 3'}]}}})</script> 
				
			

Explanation:

  • The :key attribute is set to item.id, ensuring each <li> element has a unique key.
  • This key helps Vue optimize rendering by tracking each item individually.

Using v-for with Components

You can use v-for to render a list of components, passing props to each instance.

				
					<div id="app">
  <item-component v-for="item in items" :key="item.id" :item="item"></item-component>
</div> <script type="litespeed/javascript">Vue.component('item-component',{props:['item'],template:'<div>{{ item.text }}</div>'});new Vue({el:'#app',data(){return{items:[{id:1,text:'Item 1'},{id:2,text:'Item 2'},{id:3,text:'Item 3'}]}}})</script> 
				
			

Explanation:

  • The v-for directive is used to render multiple item-component instances.
  • Each component receives the item object as a prop and renders its text.

Handling Array Mutations

Adding Items

You can add items to an array using methods like push.

				
					<div id="app">
  <ul>
    <li v-for="item in items" :key="item.id">{{ item.text }}</li>
  </ul>
  <button @click="addItem">Add Item</button>
</div> <script type="litespeed/javascript">new Vue({el:'#app',data(){return{items:[{id:1,text:'Item 1'},{id:2,text:'Item 2'},{id:3,text:'Item 3'}]}},methods:{addItem(){const newItem={id:this.items.length+1,text:`Item ${this.items.length + 1}`};this.items.push(newItem)}}})</script> 
				
			

Explanation:

  • The addItem method adds a new item to the items array using push.
  • The list updates automatically to include the new item.

Removing Items

You can remove items from an array using methods like splice.

				
					<div id="app">
  <ul>
    <li v-for="(item, index) in items" :key="item.id">
      {{ item.text }} <button @click="removeItem(index)">Remove</button>
    </li>
  </ul>
</div> <script type="litespeed/javascript">new Vue({el:'#app',data(){return{items:[{id:1,text:'Item 1'},{id:2,text:'Item 2'},{id:3,text:'Item 3'}]}},methods:{removeItem(index){this.items.splice(index,1)}}})</script> 
				
			

Explanation:

  • The removeItem method removes an item from the items array using splice.
  • The list updates automatically to exclude the removed item.

Modifying Items

You can modify items in an array by directly changing their properties.

				
					<div id="app">
  <ul>
    <li v-for="item in items" :key="item.id">
      {{ item.text }} <button @click="editItem(item)">Edit</button>
    </li>
  </ul>
</div> <script type="litespeed/javascript">new Vue({el:'#app',data(){return{items:[{id:1,text:'Item 1'},{id:2,text:'Item 2'},{id:3,text:'Item 3'}]}},methods:{editItem(item){item.text=`${item.text} (edited)`}}})</script> 
				
			

Explanation:

  • The editItem method modifies the text property of an item.
  • The list updates automatically to reflect the change.

Dynamic Lists with Filtering and Sorting

You can create dynamic lists by filtering or sorting items based on certain criteria.

Filtering

				
					<div id="app">
  <input v-model="searchQuery" placeholder="Search items">
  <ul>
    <li v-for="item in filteredItems" :key="item.id">{{ item.text }}</li>
  </ul>
</div> <script type="litespeed/javascript">new Vue({el:'#app',data(){return{searchQuery:'',items:[{id:1,text:'Item 1'},{id:2,text:'Item 2'},{id:3,text:'Item 3'}]}},computed:{filteredItems(){return this.items.filter(item=>item.text.toLowerCase().includes(this.searchQuery.toLowerCase()))}}})</script> 
				
			

Explanation:

  • The searchQuery data property is bound to the input field.
  • The filteredItems computed property filters the items array based on the searchQuery.
  • The list updates automatically to display only the items that match the search query.

Sorting

				
					<div id="app">
  <button @click="sortItems">Sort Items</button>
  <ul>
    <li v-for="item in items" :key="item.id">{{ item.text }}</li>
  </ul>
</div> <script type="litespeed/javascript">new Vue({el:'#app',data(){return{items:[{id:1,text:'Item 3'},{id:2,text:'Item 1'},{id:3,text:'Item 2'}]}},methods:{sortItems(){this.items.sort((a,b)=>a.text.localeCompare(b.text))}}})</script> 
				
			

Explanation:

  • The sortItems method sorts the items array alphabetically based on the text property.
  • The list updates automatically to display items in sorted order after clicking the “Sort Items” button.

Nested List Rendering

You can render nested lists by using v-for within another v-for.

				
					<div id="app">
  <ul v-for="category in categories" :key="category.id">
    <li>{{ category.name }}</li>
    <ul>
      <li v-for="item in category.items" :key="item.id">{{ item.name }}</li>
    </ul>
  </ul>
</div> <script type="litespeed/javascript">new Vue({el:'#app',data(){return{categories:[{id:1,name:'Category 1',items:[{id:1,name:'Item 1.1'},{id:2,name:'Item 1.2'}]},{id:2,name:'Category 2',items:[{id:3,name:'Item 2.1'},{id:4,name:'Item 2.2'}]}]}}})</script> 
				
			

Explanation:

  • The outer v-for iterates over the categories array to render each category.
  • The inner v-for iterates over the items array within each category to render nested items.

Best Practices for List Rendering

  • Use :key Attribute: Always provide a unique key attribute when using v-for to help Vue efficiently update the DOM.
  • Computed Properties: Use computed properties for complex filtering, sorting, or transformations to keep the template clean.
  • Component-based Approach: Consider creating components for list items when they are reusable or have complex logic.
  • Avoid Direct Mutation: When modifying arrays or objects in Vue.js, avoid direct mutation and use Vue’s reactive methods (push, splice, etc.) to ensure reactivity.

List rendering in Vue.js is a powerful feature that allows developers to dynamically render lists of items based on data. By leveraging directives like v-for and understanding key concepts such as :key attributes, developers can create responsive and efficient user interfaces. Whether you are rendering simple lists, handling array mutations, or dealing with nested structures, Vue.js provides flexible tools to manage and display data dynamically. This chapter has covered the complete range of techniques and best practices for list rendering, equipping you with the knowledge to build robust Vue.js applications with dynamic list functionalities.Happy coding !❤️

Table of Contents

Contact here

Copyright © 2025 Diginode

Made with ❤️ in India