Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vue Filters For Input / V-Model

So I am very new to ES6 SPA Javascript and Vue JS. I have mostly been using JQuery.

So I have a global filter,

Vue.filter('formatDate', function (value) {
  if (value) {
    return moment(String(value)).format('Do MMMM YYYY')
  }
})

I am also using Vuetify. I can use that filter with a data table, like so,

{{ props.item.DateAdded | formatDate }}

However, its not working for me on a v-model, I am guesting i doing something wrong?

 <v-flex xs12><v-text-field v-model="profileData.DateAdded | formatDate" label="Date Added"></v-text-field></v-flex>

I have also tried, v-bind:value as its the input value I want to format? No luck.

Please help?

Thanks,

like image 353
C0ol_Cod3r Avatar asked Feb 18 '19 10:02

C0ol_Cod3r


People also ask

Where can Vue filters be applied?

Going off number 2, because VueJS filters are meant for text transformations, they can only be used in two places: mustache interpolations (the curly braces in your template) and in v-bind expressions.

Are filters deprecated in VUE 3?

Filters are removed from Vue 3.0 and no longer supported.

What are the common use of Vue filters?

Filters are a functionality provided by Vue. js that allows us to define filters that can be used to apply common text formatting. Filters can be used in two places: v-bind expressions and mustache interpolation (the first is supported in 2.1. 0+).


1 Answers

According to documentaiton:

Filters are usable in two places: mustache interpolations and v-bind expressions (the latter supported in 2.1.0+)

So you can use v-bind. You said you tried but it doesn't work, however it works in this jsfiddle. I've also added @input event handler to have the v-model functionality.

So basically your text field component should be like this:

<v-text-field
  :value="profileData.DateAdded | formatDate"
  label="Date Added"
  @input="value => profileData.DateAdded = value"
></v-text-field>
like image 129
Özgür Uysal Avatar answered Oct 19 '22 07:10

Özgür Uysal