需求:一个加分扣分的需求,当输入时可以输入小数点,不进行四舍五入并只能保留小数点一位
前提:个人是在uni-number-box 组件中进行二次修改的,下面进行完善的组件代码展示!
<template>
<view class="uni-numbox">
<view @click="_calcValue('minus')" class="uni-numbox__minus" :style="{'background-color': (inputValue <= min || disabled ? '#F8F8F8' : 'rgb(255, 109, 109)')}">
<text class="uni-numbox--text" :class="{ 'uni-numbox--disabled': inputValue <= min || disabled }">-</text>
</view>
<input :disabled="disabled" @input="onKeyInput" @blur="_onBlur" class="uni-numbox__value" type="number" v-model="inputValue" />
<view @click="_calcValue('plus')" class="uni-numbox__plus" :style="{'background-color': (inputValue >= max || disabled ? '#F8F8F8' : 'rgb(0,179,15)')}">
<text class="uni-numbox--text" :class="{ 'uni-numbox--disabled': inputValue >= max || disabled }">+</text>
</view>
</view>
</template>
<script>
/**
* NumberBox 数字输入框
* @description 带加减按钮的数字输入框
* @tutorial https://ext.dcloud.net.cn/plugin?id=31
* @property {Number} value 输入框当前值
* @property {Number} min 最小值
* @property {Number} max 最大值
* @property {Number} step 每次点击改变的间隔大小
* @property {Boolean} disabled = [true|false] 是否为禁用状态
* @event {Function} change 输入框值改变时触发的事件,参数为输入框当前的 value
*/
export default {
name: "UniNumberBox",
props: {
value: {
type: [Number, String],
default: 1
},
min: {
type: Number,
default: 0
},
max: {
type: Number,
default: 100
},
step: {
type: Number,
default: 1
},
disabled: {
type: Boolean,
default: false
}
},
data() {
return {
inputValue: 0,
vlues:'',
};
},
watch: {
value(val) {
/*
此处为第三处新增代码
通过监听我们可以知道当用户 默认第一次上来时我们会获得值 给的默认值
然后我们进行判断 给默认值添加.0操作 例如:1.0 ; -1.0;
*/
let y = String(val).indexOf(".");//获取小数点的位置
if(y == -1){
//重新赋值
this.$nextTick(()=>{
this.inputValue = val + '.0';
});
if(this.inputValue == 0){
this.inputValue = 1+'.0';
}
if(val == -1){
this.inputValue = -1+'.0';
}
}else{
this.inputValue = +val;
}
},
inputValue(newVal, oldVal) {
if (+newVal !== +oldVal) {
this.$emit("change", newVal);
}
}
},
created() {
this.inputValue = +this.value;
},
methods: {
//输入框实时获取输入内容
onKeyInput: function(event) {
this.vlues = event.target.value;
},
//加分 减分操作(minus:减分;plus:加分)
_calcValue(type) {
if (this.disabled) {
return;
}
const scale = this._getDecimalScale();
let value = this.inputValue * scale;
let step = this.step * scale;
if (type === "minus") {
value -= step;
if (value < (this.min * scale)) {
return;
}
if (value > (this.max * scale)) {
value = this.max * scale
}
} else if (type === "plus") {
value += step;
if (value > (this.max * scale)) {
return;
}
if (value < (this.min * scale)) {
value = this.min * scale
}
}
/*
此处为个人新增代码 首先拿到当前值的长度 因需求 加减分数不超过99
当我们将numer转成字符串后就可以知道了字符的长度
举例:因为有负数 -11.1 正数:11.1 所以判断 当大于5时就出现了偶然性的11.111
这时我们通过toFixed()方法告诉它我们只保留小数点一位
*/
if((value+'').length > 5){
value = value.toFixed(1); //toFixed保留小数点位数方法
}
this.inputValue = String(value / scale);
},
_getDecimalScale() {
let scale = 1;
// 浮点型
if (~~this.step !== this.step) {
scale = Math.pow(10, (this.step + "").split(".")[1].length);
}
return scale;
},
_onBlur(event) {
let value = event.detail.value;
if (!value) {
return;
}
value = +value;
if (value > this.max) {
value = this.max;
} else if (value < this.min) {
value = this.min;
}
this.inputValue = value;
/*
此处为新增代码第二处
这里判断主要是在输入的时候进行处理,告诉它我们只要小数点一位就可以这个判断有点没
啥意思了可以去掉判断 直接放上重复的代码(这个判断有点脱裤子放屁多此一举了)
*/
if(value > 0 ){
event.target.value = event.target.value.match(/\d+(\.\d{0,1})?/)[0];
}else{
event.target.value = -event.target.value.match(/\d+(\.\d{0,1})?/)[0];
}
//重新赋值给input
this.$nextTick(() => {
this.inputValue = event.target.value;
})
}
}
};
</script>
<style scoped>
/* #ifdef APP-NVUE */
/* #endif */
.uni-numbox {
/* #ifndef APP-NVUE */
display: flex;
/* #endif */
flex-direction: row;
height: 35px;
line-height: 35px;
width: 120px;
}
.uni-numbox__value {
background-color: #ffffff;
width: 40px;
height: 35px;
text-align: center;
font-size: 32rpx;
border-width: 1rpx;
border-style: solid;
border-color: #e5e5e5;
border-left-width: 0;
border-right-width: 0;
}
.uni-numbox__minus {
/* #ifndef APP-NVUE */
display: flex;
/* #endif */
flex-direction: row;
align-items: center;
justify-content: center;
width: 35px;
height: 35px;
/* line-height: $box-line-height;
*/
/* text-align: center;
*/
font-size: 20px;
color: #333;
border-width: 1rpx;
border-style: solid;
border-color: #e5e5e5;
border-top-left-radius: 6rpx;
border-bottom-left-radius: 6rpx;
border-right-width: 0;
}
.uni-numbox__plus {
/* #ifndef APP-NVUE */
display: flex;
/* #endif */
flex-direction: row;
align-items: center;
justify-content: center;
width: 35px;
height: 35px;
border-width: 1rpx;
border-style: solid;
border-color: #e5e5e5;
border-top-right-radius: 6rpx;
border-bottom-right-radius: 6rpx;
border-left-width: 0;
}
.uni-numbox--text {
font-size: 40rpx;
color: #FFF;
}
.uni-numbox--disabled {
color: #c0c0c0;
}
</style>
<!--
四舍五入
// var f = parseFloat(event.target.value);
// if (isNaN(f)) {
// return false;
// }
// var f = event.target.value;
// var s = f.toString();
// var rs = s.indexOf('.');
// if (rs < 0) {
// rs = s.length;
// s += '.';
// }
// //拼接小数位数 在此处修改 小数位数
// while (s.length <= rs + 1) {
// s += '0';
// }
// event.target.value = s;
-->
父界面调用自组件代码展示
<template>
<view>
<uni-number-box :disabled="scoringType == 3 ? true : false" :key="keyup" :value="defaultNum" :min="minNum" :max="maxNum" @change="changeScore"></uni-number- box>
</view>
</template>
<script>
let _self;
import {
mapState,
mapMutations
} from 'vuex';
import uniNumberBox from "@/components/public/uni-number-box.vue";
export default {
data() {
return {
defaultNum:""
}
},
computed: {...mapState(['StudentsList'])},
onLoad(e) {
_self = this;
},
onReady() {},
methods: {
//分数改变回调
changeScore(e) {
this.score = e;
this.defaultNum = e;
},
//获取星期
dayfz(obj) {
var arys1 = obj
if (arys1) {
var moth = arys1.split(" ");
var s = moth[0];
var Y = s.replace("年", "-");
var M = Y.replace("月", "-");
var D = M.replace("日", "")
var day = "星期" + "日一二三四五六".charAt(new Date(D).getDay());
return day;
}
},
},
components: {uniNumberBox}
}
</script>
<style lang="scss" scoped></style>