Home » vue » How to add text in input form with v-model data bindings

How to add text in input form with v-model data bindings

Posted by: admin November 26, 2021 Leave a comment

Questions:

I can get salary data from my database using vue js like this

salary without curency

but I want to add curency code like this but I don’t know how to do it

salary with curency

here is my code for my retrieve, the field key salary is a double
how to add " Rp. " in a input with vue js ??

  <div class="form-row">

      <div class="form-group col-md-6">

         <label class="col-form-label">Salary</label>
         <input type="text" class="form-control" v-model="nasabah.form.salary">

      </div>

  </div>
Answers:

You have to use the value attribute instead of v-model. Then, I would advice you to make the input field readonly to prevent any unexpected behaviour that will arise when someone changes the value.

data() {
  return {
    nasabah: {
      form: {
        salary: ''
      }
    }
  }
}

<div class="form-row">
<div class="form-group col-md-6">
   <input
     :value="formattedSalaryWithCurrency">  //use the value attribute
     readonly // make it readonly to prevent unexpected behaviour
   >
</div>
</div>

computed: { // a computed property will only re-compute when a dependency changes (in this case, this.nasabah.form.salary;)
  formattedSalaryWithCurrency() {
    return this.formatSalaryWithCurrency(this.nasabah.form.salary);
  }
}

methods: {
  formatSalaryWithCurrency(amount) {
    `Rp.${amount}`
  }
}

###

You should use Computed properties of Vue for that.