Tags: Vue.js »»»» JavaScript
Most applications with item lists you want to add or remove individual items. With Vue.js it's easy and obvious how to render an item list. What's not so obvious is how to manipulate the list afterwards. The Vue.js documentation doesn't say how to remove items or change items.
Let's start with a basic first step - rendering a list of items.
With a list of items in managed data like:
data() {
return {
...
items: [
{
id: "videos",
label: "Videos"
},
{
id: "show-books",
label: "Show Books"
},
{
id: "music",
label: "Music"
},
{
id: "pictures",
label: "Pictures"
}
]
}
}
The items
list can be rendered in a template as so:
<ul id="item-list">
<li v-for="(item, index) of items" v-bind:key="index" :id="item.id">
{{ item.label }}
</li>
</ul>
The v-for
attribute is like a for
loop in that it will render a sequence of the named HTML element. The construction of the HTML element will depend on other attributes, and the child components. It's quite flexible and powerful. See documentation:
https://vuejs.org/v2/guide/list.html
What we want to focus on is adding/removing items from the list. The Vue.js Guide page says that manipulating the array causes Array Change Detection to cause a re-rendering.
For this tutorial we will create a simple Vue.js application using the Bulma/Buefy UI toolkit, and explore adding and removing items from a list. The use of Bulma/Buefy does not change anything about list add/remove behaviors. We're using the toolkit simply to make the UI a little nicer.
In a companion tutorial we will go over simple form validation: Rudimentary form validation in Vue.js with Buefy/Bulma
An example application
We need the Vue.js CLI tool installed. See Getting started with Vue.js application development
$ vue init webpack-simple list-add-delete
Let's get right into the surgery. If you need a tour of the skeleton see Getting started with Vue.js application development
In index.html
there is one line to add:
<link rel="stylesheet" href="//cdn.materialdesignicons.com/2.0.46/css/materialdesignicons.min.css">
This sets up the Material Design icons so they can be easily used in a Buefy/Bulma UI.
In App.vue
insert these <template>
and <style>
tags:
<template>
<div id="list-add-delete" class="columns">
<section id="items-area" class="column">
<ul id="item-list">
<li v-for="(item, index) of items" v-bind:key="index" :id="item.id">
{{ item.item }}
<button class="button is-medium is-primary"
@click="removeItem(index)"><b-icon icon="close-box-outline"></b-icon></button>
</li>
</ul>
</section>
<section id="new-item-form" class="column">
<div class="field">
<b-switch v-model="deleteWithSplice">
{{ deleteWithSplice ? ".splice" : "$delete" }}
</b-switch>
</div>
<b-field horizontal label="id">
<b-input v-model="newId"></b-input>
</b-field>
<b-field horizontal label="label">
<b-input v-model="newItem"></b-input>
</b-field>
<button class="button is-medium is-primary"
@click="addNewItem">Add</button>
</section>
</div>
</template>
...
<style scoped>
#list-add-delete {
font-family: 'Avenir', Helvetica, Arial, sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
text-align: center;
color: #2c3e50;
margin-top: 12px;
}
#item-list,
#item-list li {
list-style: disc inside;
}
#new-item-form {
margin: 10px;
}
</style>
We have a simple list rendered from an array in items
. Each item has a couple fields, where id
is used for the id
attribute, and item
is used as the body text of the list item. Additionally each list item has a button that will be used for removing that item from the array.
The second section is a simple form with which we will enter new items.
And we've added a little bit of styling. By default it seems Bulma/Buefy makes lists where the disc
is not showing, so we've ensured it does show, and we've set up a little bit of other styling.
The list and the form for new lists will show up side-by-side. This is due to using a Bulma columns
construct.
For the code to drive this, change the <script>
tag in App.vue
to this:
<script>
export default {
name: 'list-add-delete',
data () {
return {
items: [
{ id: "foo", item: "Foo" },
{ id: "goo", item: "Goo" }
],
newId: "",
newItem: "",
deleteWithSplice: true,
}
},
methods: {
addNewItem() {
if (this.newId === "" || this.newItem === "") return;
this.items.push({
id: this.newId,
item: this.newItem
});
this.newId = "";
this.newItem = "";
},
removeItem(index) {
if (this.deleteWithSplice) {
this.items.splice(index, 1);
} else {
this.$delete(this.items, index);
}
},
}
}
</script>
In the data
, the items
array is what's used for rendering the list. The newId
and newItem
values are used while adding new items, as the name implies. The deleteWithSplice
value determines whether to delete array values using the splice
function or the $delete
function of Vue.js.
Vue.js will recognize removing list items by manipulating the array, such as by using this.items.splice
as shown here. Vue.js also provides a $delete
function, which corresponds to Vue.delete
, which is used for deleting a property from an object. See
https://vuejs.org/v2/api/#Vue-delete
We want to be able to try either.
In this application we've demonstrated adding a list item using push
and removing one using either splice
or $delete
. According to the Vue.js documentation other array manipulations are possible. For example, sorting the array would be interesting. Bottom line is that Vue.js promises that manipulating the array automatically bubbles changes out to the user interface.
Without further ado, here is the resulting application. It shows a couple additional that we haven't discussed in this page.