Vue.js

Introduction to Vue.js 2 Filtering with Lodash

Filtering in Vue.js is simple thanks to the filter function built into Vue. Let’s take a look at an introduction filtering with Vue.js and Lodash.

Setup

Each Vue app requires a HTML element to bind with. Generally the HTML element is a div with a unique id attribute. In this example the Vue instance is bound to simple-filter.

<div id="simple-filter">
</div>
var app = new Vue({
    el: "#simple-filter"
});

Basic list unfiltered

Next let’s add default data and display the list.

You’ll notice v-for on the li element. This essentially means for each element in the array do something. In our case print the element between the li element.

<div id="simple-filter">
    <ul>
        <li v-for="animal in animals">{{ animal }}</li>
    </ul>
</div>

var app = new Vue({
    el: "#simple-filter",
    data: {
        animals: ['Zebra', 'Lion', 'Shark', 'Dog', 'Bear', 'Monkey']
    }
});

Produces the following list:

  • Zebra
  • Lion
  • Shark
  • Dog
  • Bear
  • Monkey

Filtering

Vue.js has built in functionality to filter arrays. The basic idea is to iterate over each element with a condition. If the condition is true the element stays in the array. If the condition is false the element is removed from the array.

Within the v-for instead of using the regular animals array in the previous example it is referencing a computed property filteredAnimals. Using the computed property ensures the list is always up to date when searchText is updated.

<div id="simple-filter">
    <input type="text" v-model="searchText" />
    
    <ul>
        <li v-for="animal in filteredAnimals">{{ animal }}</li>
    </ul>
</div>
var app = new Vue({
	el: "#simple-filter",
    data: {
    	searchText: "",
        animals: ['Zebra', 'Lion', 'Shark', 'Dog', 'Bear', 'Monkey']
    },
    computed: {
    	filteredAnimals: function() {
        	var self = this;
        	return this.animals.filter(function (animal) {
            	    return _.includes(animal.toLowerCase(), self.searchText.toLowerCase());
            });
        }
    }
});

filteredAnimals updates itself when searchText changes due to the nature of Vue. The filter function on this.animals is provided by Vue. Each iteration of this.animals passes an animal element to the function. Within the function a true or false value should be returned. Returning true indicates keeping the element and false instructs Vue to remove the element.

In the filter function notice _. this is Lodash. In short Lodash is a library that contains commonly used utility functions such as the includes function. The includes function searches an array, object, or string for a given value. The value in this example is the user provided text searchText. In addition I’ve added a toLowerCase() because users may not always include capital letters. Forcing the compare to be case insensitive is useful for this use case but may not in every case.

Filtering the array does not actually update the animals array. Instead a new array is returned which is then returned from the filteredAnimals computed function.

Working example below:

Build Email Message Dynamically From Submitted Form

I wrote some code to dynamically generate an email message based on the names of form elements being sent through post.
In this post I’ll describe how it’s done.

The foreach loop grabs the keys from the post array. An example key for this  $_POST[‘Name’] would be “Name”. It’s important to put in the name of the field how you want it to show in the email. So if you want uppercase letters or spaces put them in.

The $keywos (key without spaces) gets the current key in the foreach loop and replaces the underscores that are automatically put in if the name from the HTML form elements had spaces when posting and replaces them with spaces.

Copy the post array to values array

If the current value for a particular key doesn’t have information in it don’t add it to the email message. This means the user hasn’t typed in any information in the input or selected an option.

If the key isn’t equal to submit then add it to the email messages. The submit button doesn’t need to be added to the email message.

 

Have questions? Email me at [email protected].