Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VueJS dont display input value if its 0

I have an input field that gets pre-populated from my DB, but I won't want it to be pre-filled if the value is 0

Eg:

<input v-model="price" type="text">

If

  data: {
 price: 0
}

I don't want my input field to display 0, I know that I can do stuff like :disabled="price == 0" but this will hide the whole input field, I just don't want to display the value if its 0

like image 824
user2722667 Avatar asked Aug 29 '17 10:08

user2722667


People also ask

How do I get input value from Vue?

To get an input value in Vue, we can use the v-model directive to set up a two-way binding between the value and a variable. Every time the user changes the text in the input field, the text variable will be updated automatically. We can then use this variable to perform an action or display information.

What is $V in Vue?

$v is an object that calls vuelidate (at the time of writing the comment, supported in version Vue.

Why is input value empty?

The input value gets stored inside the variable and it never gets updated after that.

Does Vue need JavaScript?

All you need to start off is basic knowledge of HTML, CSS, and JavaScript, of course. Vue doesn't require in-depth knowledge of libraries and additional technologies like JSX in React and TypeScript in Angular. All you need to start off is basic knowledge of HTML, CSS, and JavaScript, of course.


1 Answers

Simply change price to empty string when it's 0:

created() {
    //since 0 is considered false it will put an empty string in case priceFromDB is 0
    this.price = priceFromDB || ''
  }
like image 168
Tomer Avatar answered Oct 20 '22 19:10

Tomer