Commit 8efa184c authored by linhuandong's avatar linhuandong

type=moneyinput 千分位格式化

parent a4d046a4
...@@ -14,13 +14,13 @@ Input 为受控组件,它**总会显示 Vue 绑定值**。 ...@@ -14,13 +14,13 @@ Input 为受控组件,它**总会显示 Vue 绑定值**。
:::demo :::demo
```html ```html
<el-input v-model="input" placeholder="请输入内容"></el-input> <el-input v-model="input" type="moneyinput" placeholder="请输入内容"></el-input>
<script> <script>
export default { export default {
data() { data() {
return { return {
input: '' input: '123123123'
} }
} }
} }
......
{"1.4.13":"1.4","2.0.11":"2.0","2.1.0":"2.1","2.2.2":"2.2","2.3.9":"2.3","2.4.11":"2.4","2.5.4":"2.5","2.6.3":"2.6","2.7.2":"2.7","2.8.2":"2.8","2.9.2":"2.9","2.10.1":"2.10","2.11.1":"2.11","2.12.0":"2.12","2.13.2":"2.13","2.14.1":"2.14","2.15.7":"2.15"} {"1.4.13":"1.4","2.0.11":"2.0","2.1.0":"2.1","2.2.2":"2.2","2.3.9":"2.3","2.4.11":"2.4","2.5.4":"2.5","2.6.3":"2.6","2.7.2":"2.7","2.8.2":"2.8","2.9.2":"2.9","2.10.1":"2.10","2.11.1":"2.11","2.12.0":"2.12","2.13.2":"2.13","2.14.1":"2.14","2.15.7675":"2.15"}
\ No newline at end of file \ No newline at end of file
{ {
"name": "element-ui", "name": "element-ui",
"version": "2.15.7667", "version": "2.15.7675",
"description": "A Component Library for Vue.js.", "description": "A Component Library for Vue.js.",
"main": "lib/element-ui.common.js", "main": "lib/element-ui.common.js",
"files": [ "files": [
......
<template> <template>
<div :class="[ <div :class="[
type === 'textarea' ? 'el-textarea' : 'el-input', type === 'textarea' ? 'el-textarea' : 'el-input',
inputSize ? 'el-input--' + inputSize : '', inputSize ? 'el-input--' + inputSize : '',
...@@ -21,24 +21,44 @@ ...@@ -21,24 +21,44 @@
<slot name="prepend"></slot> <slot name="prepend"></slot>
</div> </div>
<input <input
:tabindex="tabindex" :tabindex="tabindex"
v-if="type !== 'textarea'" v-if="type === 'moneyinput'"
class="el-input__inner" class="el-input__inner"
v-bind="$attrs" v-bind="$attrs"
:type="showPassword ? (passwordVisible ? 'text': 'password') : type" :type="showPassword ? (passwordVisible ? 'text' : 'password') : type"
:disabled="inputDisabled" :disabled="inputDisabled"
:readonly="readonly" :readonly="readonly"
:autocomplete="autoComplete || autocomplete" :autocomplete="autoComplete || autocomplete"
ref="input" ref="input"
@compositionstart="handleCompositionStart" @compositionstart="handleCompositionStart"
@compositionupdate="handleCompositionUpdate" @compositionupdate="handleCompositionUpdate"
@compositionend="handleCompositionEnd" @compositionend="handleCompositionEnd"
@input="handleInput" @input="handleInput"
@focus="handleFocus" @focus="handleFocus"
@blur="handleBlur" @blur="handleBlur"
@change="handleChange" @change="handleChange"
:aria-label="label" :aria-label="label"
> v-money
>
<input
:tabindex="tabindex"
v-else-if="type !== 'textarea'"
class="el-input__inner"
v-bind="$attrs"
:type="showPassword ? (passwordVisible ? 'text' : 'password') : type"
:disabled="inputDisabled"
:readonly="readonly"
:autocomplete="autoComplete || autocomplete"
ref="input"
@compositionstart="handleCompositionStart"
@compositionupdate="handleCompositionUpdate"
@compositionend="handleCompositionEnd"
@input="handleInput"
@focus="handleFocus"
@blur="handleBlur"
@change="handleChange"
:aria-label="label"
>
<!-- 前置内容 --> <!-- 前置内容 -->
<span class="el-input__prefix" v-if="$slots.prefix || prefixIcon"> <span class="el-input__prefix" v-if="$slots.prefix || prefixIcon">
<slot name="prefix"></slot> <slot name="prefix"></slot>
...@@ -143,7 +163,6 @@ ...@@ -143,7 +163,6 @@
focusOpen: false focusOpen: false
}; };
}, },
props: { props: {
value: [String, Number], value: [String, Number],
size: String, size: String,
...@@ -260,7 +279,30 @@ ...@@ -260,7 +279,30 @@
(this.textLength > this.upperLimit); (this.textLength > this.upperLimit);
} }
}, },
directives: {
money: {
// 指令的定义
inserted: function(el, binding) {
// 指令作用在 element-input 节点,对应原生 div.el-input (真实input节点的父节点)
if (el.tagName.toLocaleUpperCase() !== 'INPUT') {
el = el.getElementsByTagName('input')[0];
}
// 带¥符号、保留2位小数+千分位
// el.value = (Number(el.value)).toLocaleString('zh', {style:'currency', currency: 'CNY', minimumFractionDigits: 2});
// 保留2位小数+千分位
el.value = parseFloat(el.value).toLocaleString(undefined, { 'minimumFractionDigits': 2, 'maximumFractionDigits': 8 });
// console.log(el);
el.onblur = (e) => {
// 去除‘,’
let a = el.value.replace(/,/g, '');
if (a) {
el.value = parseFloat(a).toLocaleString(undefined, { 'minimumFractionDigits': 2, 'maximumFractionDigits': 8 });
// console.log(el.value);
}
};
}
}
},
watch: { watch: {
value(val) { value(val) {
this.$nextTick(this.resizeTextarea); this.$nextTick(this.resizeTextarea);
...@@ -329,11 +371,20 @@ ...@@ -329,11 +371,20 @@
this.textareaCalcStyle = calcTextareaHeight(this.$refs.textarea, minRows, maxRows); this.textareaCalcStyle = calcTextareaHeight(this.$refs.textarea, minRows, maxRows);
}, },
formatMoney(val) {
if (this.type === 'moneyinput') {
try {
val = parseFloat(val || '0').toLocaleString(undefined, { 'minimumFractionDigits': 2, 'maximumFractionDigits': 8 });
} catch (ex) {
}
}
return val;
},
setNativeInputValue() { setNativeInputValue() {
const input = this.getInput(); const input = this.getInput();
if (!input) return; if (!input) return;
if (input.value === this.nativeInputValue) return; if (input.value === this.nativeInputValue) return;
input.value = this.nativeInputValue; input.value = this.formatMoney(this.nativeInputValue);
}, },
handleFocus(event) { handleFocus(event) {
this.focused = true; this.focused = true;
......
...@@ -210,7 +210,7 @@ if (typeof window !== 'undefined' && window.Vue) { ...@@ -210,7 +210,7 @@ if (typeof window !== 'undefined' && window.Vue) {
} }
export default { export default {
version: '2.15.7667', version: '2.15.7675',
locale: locale.use, locale: locale.use,
i18n: locale.i18n, i18n: locale.i18n,
install, install,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment