javascript - 使用 vue.js 将格式价格更改为印尼盾
问题描述
我想使用 JS 和 VueIDR 50,000.00
将这个价格的格式更改为这样。IDR 50.000
我从这个链接得到了这个脚本,但我不明白它是如何工作的。我不知道这是什么
replace(/(\d)(?=(\d{3})+(?:\.\d+)?$)/g, "$1,")
所以我不能改变格式。
Vue.js
<template lang="html">
<div>
<input type="text" class="form-control" v-model="displayValue" @blur="isInputActive = false" @focus="isInputActive = true"/>
</div>
</template>
<script>
export default {
props: ["value"],
data: function() {
return {
isInputActive: false
}
},
computed: {
displayValue: {
get: function() {
if (this.isInputActive) {
// Cursor is inside the input field. unformat display value for user
return this.value.toString()
} else {
// User is not modifying now. Format display value for user interface
return "IDR " + this.value.toFixed(2).replace(/(\d)(?=(\d{3})+(?:\.\d+)?$)/g, "$1,")
}
},
set: function(modifiedValue) {
// Recalculate value after ignoring "$" and "," in user input
let newValue = parseFloat(modifiedValue.replace(/[^\d\.]/g, ""))
// Ensure that it is not NaN
if (isNaN(newValue)) {
newValue = 0
}
// Note: we cannot set this.value as it is a "prop". It needs to be passed to parent component
// $emit the event so that parent component gets it
this.$emit('input', newValue)
}
}
}
}
</script>
<style lang="css">
</style>
解决方案
我改变了这条线
return "$ " + this.value.toFixed(2).replace(/(\d)(?=(\d{3})+(?:\.\d+)?$)/g, "$1,")
对此
return "IDR " + this.value.toString().replace(/(\d)(?=(\d{3})+(?:\.\d+)?$)/g, "$1\.")
片段:
Vue.component('my-currency-input', {
props: ["value"],
template: `
<div>
<input type="text" v-model="displayValue" @blur="isInputActive = false" @focus="isInputActive = true"/>
</div>`,
data: function() {
return {
isInputActive: false
}
},
computed: {
displayValue: {
get: function() {
if (this.isInputActive) {
// Cursor is inside the input field. unformat display value for user
return this.value.toString()
} else {
// User is not modifying now. Format display value for user interface
return "IDR " + this.value.toString().replace(/(\d)(?=(\d{3})+(?:\.\d+)?$)/g, "$1\.")
}
},
set: function(modifiedValue) {
// Recalculate value after ignoring "$" and "," in user input
let newValue = parseFloat(modifiedValue.replace(/[^\d\.]/g, ""))
// Ensure that it is not NaN
if (isNaN(newValue)) {
newValue = 0
}
// Note: we cannot set this.value as it is a "prop". It needs to be passed to parent component
// $emit the event so that parent component gets it
this.$emit('input', newValue)
}
}
}
});
new Vue({
el: '#app',
data: function() {
return {
price: 50000
}
}
});
body {
margin: 20px;
font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
}
input {
border: 1px solid #888;
font-size: 1.2rem;
padding: 0.5rem;
}
<script src="https://unpkg.com/vue@2.1.5/dist/vue.js"></script>
<div id="app">
Price:
<my-currency-input v-model="price"></my-currency-input>
<p>
Price (in parent component): {{price}}
</p>
</div>
推荐阅读
- button - 当我按下 LED 时,Arduino MIDIUSB 按钮停止工作
- flutter - 如何在列表上方添加按钮?
- ios - 如何重新安装从 iPhone 中删除的测试应用程序?
- javascript - 使用 material-ui-picker 在 React Hooks 中未更新日期(handleDateChange)
- c++ - 在 C++ 的链表中使用 * 的区别
- javascript - 我的 laravel 应用程序中的分页显示两次
- reactjs - 为什么 Redux 的 id 未定义?
- sql - 访问查询 - 分组依据和最大值
- javascript - 如何将多个导入 js 文件包含到另一个 js 文件中
- c# - SqlBulkCopy 与 SSIS