当前位置: 首页 > news >正文

el-input,金额千分符自动转换

1. 说明

在平时项目中,对于金额处理显示一般需要按千分符显示,通常实现会申明一个formater函数来进行转换,但是涉及的地方比较多试,使用起来比较繁琐,封装一个单独的组件比较合理

2. 实现组件代码

  1. ElMoneyInput.vue
<template><div :style="{'background-color': disabled ? 'transparent' : '#fff'}"><span class="money-input" v-if="!isInput" @click="focusHanle" :disabled="disabled" :value="viewValue">{{ viewValue }}</span><el-input ref="moneyInput" v-else v-bind="$attrs" :value="value" @input="handlerChange" @blur="handlerBlur" autofocus onkeypress="if(event.keyCode == 13) return false;"/></div>
</template>
<script>
export default {name: 'ElMoneyInput',inheritAttrs: false,model: {prop: "value",event: "input"},props: {value: {type: String,default: ""},disabled:{type:Boolean,default:false},rules: {type: Object,default: () => {}}},watch: {value(val, old) {if (val !== old) {this.init()}}},mounted() {this.init()},data() {return {isEdit: true,isInput: false,viewValue: ''};},methods: {init() {if (!this.isInput) {this.viewValue = this.formatMoney(this.value || 0)this.$emit('input', this.blurformat(this.value || 0))}},focusHanle() {if (this.disabled) returnthis.isInput = !this.isInputthis.$emit('input', this.blurformat(this.value || 0))this.$nextTick(() => {this.$refs.moneyInput.focus()})},formatMoney(cellValue, num = 2) {if (isNaN(cellValue)) {return ""}if (cellValue === 0) {return '0.00';}return this.$Utils.formatMoney(cellValue, num);},format(v) {return (`${v}`.match(/([\d\.]+)/) || "")[0];},blurformat(v) {return v ? Number.parseFloat(v).toFixed(2) : "";},handlerChange(v) {this.$emit('input', v)},handlerBlur() {this.isInput = false;this.$emit('input', this.blurformat(this.value))this.viewValue = this.formatMoney(this.value)},// handleFocus() {//   this.isInput = true;//   this.$emit('input', this.blurformat(this.value))// }}
};
</script>
<style lang="less">
.span-input{display: inline-block;width: 100%;height:28px;
}
.money-input {position: relative;font-size: 14px;display: inline-block;height: 32px;line-height: 32px;background: transparent !important;cursor: text !important;background-color: #FFF;background-image: none;border-radius: 4px;border: 1px solid #DCDFE6;-webkit-box-sizing: border-box;box-sizing: border-box;color: #606266;display: inline-block;font-size: inherit;outline: 0;padding: 0 15px;-webkit-transition: border-color .2s cubic-bezier(.645,.045,.355,1);transition: border-color .2s cubic-bezier(.645,.045,.355,1);width: 100%;.el-input__inner {background: transparent !important;cursor: text !important;background-color: #FFF;background-image: none;border-radius: 4px;border: 1px solid #DCDFE6;-webkit-box-sizing: border-box;box-sizing: border-box;color: #606266;display: inline-block;font-size: inherit;height: 40px;line-height: 40px;outline: 0;padding: 0 15px;-webkit-transition: border-color .2s cubic-bezier(.645,.045,.355,1);transition: border-color .2s cubic-bezier(.645,.045,.355,1);width: 100%;}
}
</style>

3.实现效果

在这里插入图片描述

相关文章:

  • window下配置ssh免密登录服务器
  • RushDB开源程序 是现代应用程序和 AI 的即时数据库。建立在 Neo4j 之上
  • 【网站建设】不同类型网站如何选择服务器?建站项目实战总结
  • 【MySQL系列】MySQL 执行 SQL 文件
  • GeoBoundaries下载行政区划边界数据(提供中国资源shapefile)
  • Linux:守护进程(进程组、会话和守护进程)
  • Ubuntu系统多网卡多相机IP设置方法
  • Prompt工程学习之思维树(TOT)
  • Prompt Tuning(提示调优)到底训练优化的什么部位
  • 在React 中安装和配置 shadcn/ui
  • Windmill:开源开发者基础设施的革命者
  • Prompt工程学习之自我一致性
  • 双指针详解
  • 《第五人格》暑期活动前瞻爆料:39赛季精华、限定时装返场、新玩法攻略
  • JavaScript 数组学习总结
  • 获取wordpress某个栏目的内容数量
  • 服务网格技术深度解析:Istio vs Linkerd的选型对比
  • 当前市场环境下,软件行业的突围之道:技术演进与商业模式重构
  • 异或和之差-字典树
  • Web后端开发(请求、响应)
  • 东营网站建设制作/竞价排名点击器
  • 做旅游网站需要引进哪些技术人才/免费的网站软件下载
  • 扬中网站建设如何/广告招商
  • 苏州园区做网站/什么是电商
  • 怎么在网站做系统/百度竞价最低点击一次多少钱
  • 免费自助建站工具/福州关键词搜索排名