Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases now! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Vue.js 2.x by Example
Vue.js 2.x by Example

Vue.js 2.x by Example: Example-driven guide to build web apps with Vue.js for beginners

eBook
€22.99 €32.99
Paperback
€41.99
Subscription
Free Trial
Renews at €18.99p/m

What do you get with a Packt Subscription?

Free for first 7 days. $19.99 p/m after that. Cancel any time!
Product feature icon Unlimited ad-free access to the largest independent learning library in tech. Access this title and thousands more!
Product feature icon 50+ new titles added per month, including many first-to-market concepts and exclusive early access to books as they are being written.
Product feature icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Product feature icon Thousands of reference materials covering every tech concept you need to stay up to date.
Subscribe now
View plans & pricing
Table of content icon View table of contents Preview book icon Preview Book

Vue.js 2.x by Example

Displaying, Looping, Searching, and Filtering Data

In Chapter 1, Getting Started with Vue.js, we covered the data, computed, and method objects within Vue and how to display static data values. In this chapter, were are going to cover:

  • Displaying lists and more complex data with Vue using v-if, v-else, and v-for
  • Filtering the lists using form elements
  • Applying conditional CSS classes based on the data

The data we are going to be using is going to be randomly generated by the JSON generator service (http://www.json-generator.com/). This website allows us to get dummy data to practice with. The following template was used to generate the data we will be using. Copy the following into the left-hand side to generate data of the same format so the attributes match with the code examples, as follows:

      [
'{{repeat(5)}}',
{
index: '{{index...

HTML declarations 

Vue allows you to use HTML tags and attributes to control and alter the view of your application. This involves setting attributes dynamically, such as alt and href. It also allows you to render tags and components based on data in the application. These attributes begin with a v- and, as mentioned at the beginning of this book, get removed from the HTML on render. Before we start outputting and filtering our data, we'll run through a few of the common declarations.

v-html

The v-html directive allows you to output content without using the mustache-style curly bracket syntax. It can also be used if your output contains HTML tags – it will render the output as HTML instead of plain text....

Conditional rendering

Using custom HTML declarations, Vue allows you to render elements and contents conditionally based on data attributes or JavaScript declarations. These include v-if, for showing a container whether a declaration equates to true, and v-else, to show an alternative.

v-if

The most basic example of this would be the v-if directive – determining a value or function if the block should be displayed.

Create a Vue instance with a single div inside the view and a data key, isVisible, set to false.

View:

Start off with the view code as the following:

      <div id="app">
<div>Now you see me</div>
</div>

JavaScript:

In the JavaScript, initialize Vue and create...

v-for and displaying our data

The next HTML declaration means we can start displaying our data and putting some of these attributes into practice. As our data is an array, we will need to loop through it to display each element. To do this, we will use the v-for directive.

Generate your JSON and assign it to a variable called people. During these examples, the generated JSON loop will be displayed in the code blocks as [...]. Your Vue app should look like the following:

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

data: {
people: [...]
}
});

We now need to start displaying each person's name in our View as a bulleted list. This is where the v-for directive comes in:

      <div id="app">
<ul>
<li v-for="person in people">
{{ person }}
</li>
</ul...

Filtering our data

With our data being listed out, we are now going to build filtering ability. This will allow a user to select a field to filter by and a text field to enter their query. The Vue application will then filter the rows as the user types. To do this, we are going to bind some form inputs to various values in the data object, create a new method, and use a new directive on the table rows; v-show.

Building the form

Start off by creating the HTML in your view. Create a <select> box with an <option> for each field you want to filter, an <input> for the query, and a pair of radio buttons – we'll use these to filter active and non-active users. Make sure the value attribute of each &lt...

Showing and hiding Vue content

Along with v-if for showing and hiding content, you can also use the v-show="" directive. v-show is very similar to v-if; they both get added to the HTML wrapper and can both accept the same parameters, including a function.

The difference between the two is v-if alters the markup, removing and adding HTML elements as required, whereas v-show renders the element regardless, hiding and showing the element with inline CSS styles. v-if is much more suited to runtime renders or infrequent user interactivities as it could potentially be restructuring the whole page. v-show is favorable when lots of elements are quickly coming in and out of view, for example, when filtering!

When using v-show with a method, the function needs to return just a true or false. The function has no concept of where it is being used, so we need to pass in the current...

Changing CSS classes

As with any HTML attribute, Vue is able to manipulate CSS classes. As with everything in Vue, this can be done in a myriad of ways ranging from attributes on the object itself to utilizing methods. We'll start off adding a class if the user is active.

Binding a CSS class is similar to other attributes. The value takes an object that can calculate logic from within the view or be abstracted out into our Vue instance. This all depends on the complexity of the operation.

First, let's add a class to the cell containing the isActive variable if the user is active:

      <td v-bind:class="{ active: person.isActive }">
{{ activeStatus(person) }}
</td>

The class HTML attribute is first prepended by v-bind: to let Vue know it needs to process the attribute. The value is then an object, with the CSS class as the key and the...

Filtering and custom classes

We now have a fully fledged user list/register that has filtering on selected fields and custom CSS classes depending on the criteria. To recap, this is what our view looks like now we have the filter in place:

      <div id="app">
<form>
<label for="fiterField">
Field:
<select id="filterField" v-model="filterField">
<option value="">Disable filters</option>
<option value="isActive">Active user</option>
<option value="name">Name</option>
<option value="email">Email</option>
<option value="balance">Balance</option>
<option value="registered">Date registered...

Summary

In this chapter, we looked at Vue HTML declarations, conditionally rendering our HTML and showing an alternative if required. We also put into practice what we learned about methods. Lastly, we built a filtering component for our table, allowing us to show active and inactive users, find users with specific names and emails, and filter out rows based on the balance.

Now we've got to a good point in our app, it's a good opportunity to take a look at our code to see if it can be optimized in any way. By optimizations, I mean reducing repetition, making the code simpler if possible, and abstracting logic out into smaller, readable, and reusable chunks.

In Chapter 3, Optimizing Our App and Using Components to Display Data, we will optimize our code and look at Vue components as a way of separating out logic into separate segments and sections.

...
Left arrow icon Right arrow icon
Download code icon Download Code

Key benefits

  • We bridge the gap between "learning" and "doing" by providing real-world examples that will improve your web development skills with Vue.js
  • Explore the exciting features of Vue.js 2 through practical and interesting examples
  • Explore modern development tools and learn how to utilize them by building applications with Vue.js

Description

Vue.js is a frontend web framework which makes it easy to do just about anything, from displaying data up to creating full-blown web apps, and has become a leading tool for web developers. This book puts Vue.js into a real-world context, guiding you through example projects that helps you build Vue.js applications from scratch. With this book, you will learn how to use Vue.js by creating three Single Page web applications. Throughout this book, we will cover the usage of Vue, for building web interfaces, Vuex, an official Vue plugin which makes caching and storing data easier, and Vue-router, a plugin for creating routes and URLs for your application. Starting with a JSON dataset, the first part of the book covers Vue objects and how to utilize each one. This will be covered by exploring different ways of displaying data from a JSON dataset. We will then move on to manipulating the data with filters and search and creating dynamic values. Next, you will see how easy it is to integrate remote data into an application by learning how to use the Dropbox API to display your Dropbox contents in an application In the final section, you will see how to build a product catalog and dynamic shopping cart using the Vue-router, giving you the building blocks of an e-commerce store.

Who is this book for?

This book is for developers who know the basics of JavaScript and are looking to learn Vue.js with real examples. You should understand the basics of JavaScript functions and variables and be comfortable with using CSS or a CSS framework for styling your projects.

What you will learn

  • Looping through data with Vue.js
  • Searching and filtering data
  • Using components to display data
  • Getting a list of files using the dropbox API
  • Navigating through a file tree and loading folders from a URL
  • Caching with Vuex
  • Pre-caching for faster navigation
  • Introducing vue-router and loading components
  • Using vue-router dynamic routes to load data
  • Using vue-router and Vuex to create an ecommerce store

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : Dec 27, 2017
Length: 412 pages
Edition : 1st
Language : English
ISBN-13 : 9781788293464
Languages :
Tools :

What do you get with a Packt Subscription?

Free for first 7 days. $19.99 p/m after that. Cancel any time!
Product feature icon Unlimited ad-free access to the largest independent learning library in tech. Access this title and thousands more!
Product feature icon 50+ new titles added per month, including many first-to-market concepts and exclusive early access to books as they are being written.
Product feature icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Product feature icon Thousands of reference materials covering every tech concept you need to stay up to date.
Subscribe now
View plans & pricing

Product Details

Publication date : Dec 27, 2017
Length: 412 pages
Edition : 1st
Language : English
ISBN-13 : 9781788293464
Languages :
Tools :

Packt Subscriptions

See our plans and pricing
Modal Close icon
€18.99 billed monthly
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Simple pricing, no contract
€189.99 billed annually
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just €5 each
Feature tick icon Exclusive print discounts
€264.99 billed in 18 months
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just €5 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total 115.97
Vue.js 2.x by Example
€41.99
Full-Stack Vue.js 2 and Laravel 5
€36.99
Vue.js 2 Web Development Projects
€36.99
Total 115.97 Stars icon

Table of Contents

12 Chapters
Getting Started with Vue.js Chevron down icon Chevron up icon
Displaying, Looping, Searching, and Filtering Data Chevron down icon Chevron up icon
Optimizing your App and Using Components to Display Data Chevron down icon Chevron up icon
Getting a List of Files Using the Dropbox API Chevron down icon Chevron up icon
Navigating through the File Tree and Loading Folders from the URL Chevron down icon Chevron up icon
Caching the Current Folder Structure Using Vuex Chevron down icon Chevron up icon
Pre-Caching Other Folders and Files for Faster Navigation Chevron down icon Chevron up icon
Introducing Vue-Router and Loading URL-Based Components Chevron down icon Chevron up icon
Using Vue-Router Dynamic Routes to Load Data Chevron down icon Chevron up icon
Building an E-Commerce Store – Browsing Products Chevron down icon Chevron up icon
Building an E-Commerce Store – Adding a Checkout Chevron down icon Chevron up icon
Using Vue Dev Tools and Testing Your SPA Chevron down icon Chevron up icon

Customer reviews

Rating distribution
Full star icon Full star icon Full star icon Full star icon Full star icon 5
(2 Ratings)
5 star 100%
4 star 0%
3 star 0%
2 star 0%
1 star 0%
Bogdan Balc Jan 19, 2018
Full star icon Full star icon Full star icon Full star icon Full star icon 5
I was a technical reviewer for this book and I would like to mention a couple of things that to me seem very important for any potential reader.This book will give you the very basics of Vue.js, the things you must know in order to develop the most impressive web apps. In my opinion the author managed to do something that is very rarely seen in a book like this, to capture the inner workings of the framework with the example projects which are explained quite well. This way you don't get only some basic understanding of major functionalities in Vue.js but also an in depth view of what makes Vue.js tick!
Amazon Verified review Amazon
Amazon Customer Jan 13, 2018
Full star icon Full star icon Full star icon Full star icon Full star icon 5
Mike Street wrote an awesome book. Each chapter you read will teach something useful and in the right way. Vue.js is a great javascript framework to work with and has a lot of feature. Most of those features can be found in this book while developing an app.It covers from the basic to some advanced concepts.
Amazon Verified review Amazon
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

What is included in a Packt subscription? Chevron down icon Chevron up icon

A subscription provides you with full access to view all Packt and licnesed content online, this includes exclusive access to Early Access titles. Depending on the tier chosen you can also earn credits and discounts to use for owning content

How can I cancel my subscription? Chevron down icon Chevron up icon

To cancel your subscription with us simply go to the account page - found in the top right of the page or at https://subscription.packtpub.com/my-account/subscription - From here you will see the ‘cancel subscription’ button in the grey box with your subscription information in.

What are credits? Chevron down icon Chevron up icon

Credits can be earned from reading 40 section of any title within the payment cycle - a month starting from the day of subscription payment. You also earn a Credit every month if you subscribe to our annual or 18 month plans. Credits can be used to buy books DRM free, the same way that you would pay for a book. Your credits can be found in the subscription homepage - subscription.packtpub.com - clicking on ‘the my’ library dropdown and selecting ‘credits’.

What happens if an Early Access Course is cancelled? Chevron down icon Chevron up icon

Projects are rarely cancelled, but sometimes it's unavoidable. If an Early Access course is cancelled or excessively delayed, you can exchange your purchase for another course. For further details, please contact us here.

Where can I send feedback about an Early Access title? Chevron down icon Chevron up icon

If you have any feedback about the product you're reading, or Early Access in general, then please fill out a contact form here and we'll make sure the feedback gets to the right team. 

Can I download the code files for Early Access titles? Chevron down icon Chevron up icon

We try to ensure that all books in Early Access have code available to use, download, and fork on GitHub. This helps us be more agile in the development of the book, and helps keep the often changing code base of new versions and new technologies as up to date as possible. Unfortunately, however, there will be rare cases when it is not possible for us to have downloadable code samples available until publication.

When we publish the book, the code files will also be available to download from the Packt website.

How accurate is the publication date? Chevron down icon Chevron up icon

The publication date is as accurate as we can be at any point in the project. Unfortunately, delays can happen. Often those delays are out of our control, such as changes to the technology code base or delays in the tech release. We do our best to give you an accurate estimate of the publication date at any given time, and as more chapters are delivered, the more accurate the delivery date will become.

How will I know when new chapters are ready? Chevron down icon Chevron up icon

We'll let you know every time there has been an update to a course that you've bought in Early Access. You'll get an email to let you know there has been a new chapter, or a change to a previous chapter. The new chapters are automatically added to your account, so you can also check back there any time you're ready and download or read them online.

I am a Packt subscriber, do I get Early Access? Chevron down icon Chevron up icon

Yes, all Early Access content is fully available through your subscription. You will need to have a paid for or active trial subscription in order to access all titles.

How is Early Access delivered? Chevron down icon Chevron up icon

Early Access is currently only available as a PDF or through our online reader. As we make changes or add new chapters, the files in your Packt account will be updated so you can download them again or view them online immediately.

How do I buy Early Access content? Chevron down icon Chevron up icon

Early Access is a way of us getting our content to you quicker, but the method of buying the Early Access course is still the same. Just find the course you want to buy, go through the check-out steps, and you’ll get a confirmation email from us with information and a link to the relevant Early Access courses.

What is Early Access? Chevron down icon Chevron up icon

Keeping up to date with the latest technology is difficult; new versions, new frameworks, new techniques. This feature gives you a head-start to our content, as it's being created. With Early Access you'll receive each chapter as it's written, and get regular updates throughout the product's development, as well as the final course as soon as it's ready.We created Early Access as a means of giving you the information you need, as soon as it's available. As we go through the process of developing a course, 99% of it can be ready but we can't publish until that last 1% falls in to place. Early Access helps to unlock the potential of our content early, to help you start your learning when you need it most. You not only get access to every chapter as it's delivered, edited, and updated, but you'll also get the finalized, DRM-free product to download in any format you want when it's published. As a member of Packt, you'll also be eligible for our exclusive offers, including a free course every day, and discounts on new and popular titles.