Skip to content

Instantly share code, notes, and snippets.

@Aslam97
Created October 16, 2020 16:29
Show Gist options
  • Save Aslam97/15521389e41b00bba440b517fdd2e8f4 to your computer and use it in GitHub Desktop.
Save Aslam97/15521389e41b00bba440b517fdd2e8f4 to your computer and use it in GitHub Desktop.
Change format price to rupiah using vue.js
Vue.component('idr', {
template: '<input type="text" v-model="currentValue" @input="handleInput" />',
props: {
value: {
type: [String, Number],
default: ""
},
},
data: () => ({
currentValue: ''
}),
watch: {
value: {
handler(after) {
this.currentValue = this.format(after)
},
immediate: true
}
},
methods: {
format: value => (value + '').replace(/\D/g, "").replace(/\B(?=(\d{3})+(?!\d))/g, "."),
handleInput() {
this.currentValue = this.format(this.currentValue)
this.$emit('input', (this.currentValue + '').replace(/[^0-9]/g, ""))
}
}
})
new Vue({
el: '#app',
data: {
total: 5000,
}
})
// try example in this link https://stackoverflow.com/questions/52462915/change-format-price-to-rupiah-using-vue-js
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment