# Filters

Filters are use to transform output in the template. It does NOT transform the data itself, but transforms what the user sees.

# Local filter

To use filter locally in a component, You define filters in a component’s options. To use that filter you append it to the mustache interpolation or to the v-bind expression.

# Input:


 









 
 
 
 
 



<template>
  <p>{{ employee | capitalize }}</p>
</template>

<script>
export default {
  data() {
    return {
      employee: 'Creed Bratton'
    }
  },
  filters: {
    capitalize(value) {
      return value.toUpperCase()
    }
  }
}
</script>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

# Output:

CREED BRATTON

# Global Filter

You might want to use a filter in several components. To do so, you can set up the filter in the main.js file.




 
 
 







import Vue from 'vue'
import App from './App.vue'

Vue.filter('capitalize', function(value) {
  return value.toUpperCase()
})

Vue.config.productionTip = false

new Vue({
  render: h => h(App)
}).$mount('#app')
1
2
3
4
5
6
7
8
9
10
11
12

Then, you can use it in any component.


 

















<template>
  <p>{{ employee | capitalize }}</p>
</template>

<script>
export default {
  data() {
    return {
      employee: 'Creed Bratton'
    }
  },
  filters: {
    capitalize(value) {
      return value.toUpperCase()
    }
  }
}
</script>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

# Output:

Creed Bratton

# Chaining Filters

You can chain filters. When you have several filters, the following ones will be applied to the output of the previous filter. Here I am changing the data to uppercase, then reversing it, then changing it to lowercase.


 









 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 



<template>
  <p>{{ employee | capitalize | reverseInPlace | lowercase }}</p>
</template>

<script>
export default {
  data() {
    return {
      employee: 'Creed Bratton'
    }
  },
  filters: {
    capitalize(value) {
      return value.toUpperCase()
    },
    reverseInPlace(str) {
      var words = []
      words = str.match(/\S+/g)
      var result = ''
      for (var i = 0; i < words.length; i++) {
        result +=
          words[i]
            .split('')
            .reverse()
            .join('') + ' '
      }
      return result
    },
    lowercase(value) {
      return value.toLowerCase()
    }
  }
}
</script>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34

# Output:

deerc nottarb