当前位置: 首页 > 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.实现效果

在这里插入图片描述

http://www.xdnf.cn/news/929971.html

相关文章:

  • Unity中的transform.up
  • 【碎碎念】宝可梦 Mesh GO : 基于MESH网络的口袋妖怪 宝可梦GO游戏自组网系统
  • Java求职者面试:微服务技术与源码原理深度解析
  • SpringSecurity+vue通用权限系统2
  • SOC-ESP32S3部分:36-适配自己的板卡
  • HTML前端开发:JavaScript的条分支语句if,Switch
  • HTML前端开发:JavaScript 常用事件详解
  • 4. TypeScript 类型推断与类型组合
  • 分析 java 的 Map<String,Map<String, List<Map<String,Integer>>>>
  • Go语言并发模型与模式:Worker Pool 模式
  • 详解鸿蒙Next仓颉开发语言中的动画
  • 勒让德多项式
  • 投屏技术深度解析:从原理到成功率优化实战·优雅草卓伊凡
  • 高级数据结构与算法期末考试速成记录2
  • exec进程替换函数族
  • AOSP CachedAppOptimizer中的冻结和内存压缩功能
  • 11.无重复字符的最长子串
  • LUFFY(路飞): 使用DeepSeek指导Qwen强化学习
  • 34 C 语言字符串转数值函数详解:strtol、strtoll、strtoul、strtoull(含 errno 处理、ERANGE 错误)
  • 创建一个纯直线组成的字体库
  • 【强连通分量 缩点 最长路 拓扑排序】P2656 采蘑菇|普及+
  • Linux 文本三剑客(grep, awk, sed)
  • 运维_集运维核心学习
  • xctf-weak_auth(弱口令)
  • 【C++ 真题】P1747 好奇怪的游戏
  • 23、字节对齐
  • 22、模板特例化
  • WPF 播放器(AudioPlayer 2025)
  • triton学习笔记6: Fused Attention
  • CAN转PROFINET网关设备基本功能介绍