8

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

2
  • What about programatically changing the value from 0 to "" in the model? Would that work? Commented Aug 29, 2017 at 10:59
  • you could try something like this: price !== 0 Commented Aug 29, 2017 at 11:07

2 Answers 2

6

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 || ''
  }
Sign up to request clarification or add additional context in comments.

Comments

1

As I understood you want to always have empty input when value of price is 0, even if user type It manually.If that's true, something like this should work for you:

<div id="app">
    <input type="text" v-model="price" @keyup="trackChange">
</div>

JS:

const app = new Vue({

  el: '#app',

  data: {
    price: 0
  },

  methods: {
    trackChange() {
      this.price = parseInt(this.price) === 0  ? null : this.price
    }
  },

  created() {
    this.trackChange()
  }


})

Demo: http://jsbin.com/sihuyotuto/edit?html,js,output

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.