Created
October 16, 2020 16:29
-
-
Save Aslam97/15521389e41b00bba440b517fdd2e8f4 to your computer and use it in GitHub Desktop.
Change format price to rupiah using vue.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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