Swiftorial Logo
Home
Swift Lessons
Tutorials
Learn More
Career
Resources

VueJS - Filters

Using Filters to Format Data in VueJS

Filters in VueJS allow you to format data directly in your templates. This guide covers the basics of using filters in VueJS, including defining, using, and understanding the benefits of filters.

Key Points:

  • Filters are used to format data in VueJS templates.
  • Filters can be defined globally or locally.
  • Filters can be chained to apply multiple transformations to the same data.

Defining and Using Filters

Global Filters

Define a global filter and use it in a component:


// JavaScript
Vue.filter('uppercase', function (value) {
  if (!value) return '';
  return value.toString().toUpperCase();
});

new Vue({
  el: '#app',
  data: {
    message: 'hello world'
  }
});


{{ message | uppercase }}

Local Filters

Define a local filter within a component:


// JavaScript
const app = new Vue({
  el: '#app',
  data: {
    message: 'hello world'
  },
  filters: {
    lowercase(value) {
      if (!value) return '';
      return value.toString().toLowerCase();
    }
  }
});


{{ message | lowercase }}

Chaining Filters

Filters can be chained to apply multiple transformations to the same data:


// JavaScript
Vue.filter('uppercase', function (value) {
  if (!value) return '';
  return value.toString().toUpperCase();
});

Vue.filter('reverse', function (value) {
  if (!value) return '';
  return value.split('').reverse().join('');
});

new Vue({
  el: '#app',
  data: {
    message: 'hello world'
  }
});


{{ message | uppercase | reverse }}

Common Use Cases

Date Formatting

Use a filter to format dates:


// JavaScript
Vue.filter('formatDate', function (value) {
  if (!value) return '';
  const date = new Date(value);
  return date.toLocaleDateString();
});

new Vue({
  el: '#app',
  data: {
    currentDate: new Date()
  }
});


{{ currentDate | formatDate }}

Number Formatting

Use a filter to format numbers:


// JavaScript
Vue.filter('currency', function (value) {
  if (!value) return '';
  return '$' + value.toFixed(2);
});

new Vue({
  el: '#app',
  data: {
    price: 19.99
  }
});


{{ price | currency }}

Best Practices

Follow these best practices when using filters in VueJS:

  • Encapsulate Reusable Logic: Use filters to encapsulate reusable formatting logic that can be applied across multiple components.
  • Avoid Complex Logic: Keep filters simple and avoid adding complex logic that could make them hard to maintain.
  • Document Filters: Document the purpose and usage of filters to make your code easier to understand and maintain.
  • Use Filters Sparingly: Avoid overusing filters to prevent your templates from becoming cluttered and hard to read. Consider using computed properties for more complex transformations.

Example Application

Here is an example application that demonstrates using filters in VueJS:



Filters Example

{{ message | uppercase }}

{{ message | lowercase }}

{{ message | uppercase | reverse }}

{{ currentDate | formatDate }}

{{ price | currency }}

Summary

This guide provided an overview of using filters in VueJS, including defining and using filters, chaining filters, common use cases, and following best practices. By understanding and utilizing these features, you can create reusable and maintainable VueJS components with enhanced data formatting capabilities.