基于element UI input组件自行封装“数字区间”输入框组件的问题及解决

2022-07-14,,,,

问题描述

在开发时遇到一个数字区间输入框的需求,如下图:

项目使用的是vue,组件库用的是element ui,但是element ui并没有提供数字区间组件,只提供了inputnumber 计数器输入框,如果用两个计数器输入框进行拼接也能满足需求,但是样式调试起来太过于复杂且不够灵活,不能令人满意,并且该数字区间输入框在其它界面也有这种需求,于是就在element input输入框的基础上自行封装了一个数字区间组件使用。

实现效果

实现效果如下:

使用方式如下:

<input-number-range :disabled="isdisabled" :precision="num" v-model="value"></input-number-range>

其中disabled属性控制是否禁用,precision属性控制精度默认为0即只能输入整数,v-model双向绑定要传递的值,该值是一个数组类型 [最小值,最大值]

另外该组件只能输入数字,输入其他非数字,或错误数字(多个小数)都会默认为空;在先输入最小值时,如果后输入的最大值小于最小值,则最大值默认为最小值,同理先输入最大值时,如果后输入的最小值大于最大值,则最小值默认为最大值

实现代码

实现代码可以分为两块一块为组件的封装代码,一块为上述实现效果的演示代码

数字区间组件代码

<template>
  <div>
    <div class="input-number-range" :class="{ 'is-disabled': disabled }">
      <div class="flex">
        <div class="from">
          <!--
          blur:最小值失焦事件
          focus:最小值聚焦事件
          input:最小值输入事件
          change:最小值change事件
          -->
          <el-input
            ref="input_from"
            v-model="userinputform"
            :disabled="disabled"
            placeholder="最小值"
            @blur="handleblurfrom"
            @focus="handlefocusfrom"
            @input="handleinputfrom"
            @change="handleinputchangefrom"
          ></el-input>
        </div>
        <div class="center">
          <span>至</span>
        </div>
        <div class="to">
           <!--
          blur:最大值失焦事件
          focus:最大值聚焦事件
          input:最大值输入事件
          change:最大值change事件
          -->
          <el-input
            ref="input_to"
            v-model="userinputto"
            :disabled="disabled"
            placeholder="最大值"
            @blur="handleblurto"
            @focus="handlefocusto"
            @input="handleinputto"
            @change="handleinputchangeto"
          ></el-input>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: "inputnumberrange",

  /** 组件接收参数 */
  props: {
    value: { required: true },

    // 是否禁用
    disabled: {
      type: boolean,
      default: false,
    },

    // 精度参数
    precision: {
      type: number,
      default: 0,
      validator(val) {
        return val >= 0 && val === parseint(val, 10);
      },
    },
  },

  data() {
    return {
      userinputform: null, // 最小值
      userinputto: null, // 最大值
    };
  },

  watch: {
  /** 监听value实现双向绑定 */
    value: {
      immediate: true,
      handler(value) {
         // 判断是否为数字number类型
        if (value instanceof array && this.precision !== undefined) {
          let fromval =
            value[0] && typeof value[0] === "number" ? value[0] : null;
          let toval =
            value[1] && typeof value[1] === "number" ? value[1] : null;
          this.userinputform = fromval ? fromval : null;
          this.userinputto = toval ? toval : null;
        }
      },
    },
  },

  methods: {
    // 根据精度保留数字
    toprecision(num, precision) {
      if (precision === undefined) precision = 0;
      return parsefloat(
        math.round(num * math.pow(10, precision)) / math.pow(10, precision)
      );
    },
    
    /** 触发以下事件时自动向上冒泡执行(通过emit将事件抛给element input组件) */
    handleblurfrom(event) {
      this.$emit("blurfrom", event);
    },

    handlefocusfrom(event) {
      this.$emit("focusfrom", event);
    },

    handleblurto(event) {
      this.$emit("blurto", event);
    },

    handlefocusto(event) {
      this.$emit("focusto", event);
    },

    handleinputfrom(value) {
      this.$emit("inputfrom", value);
      this.userinputfrom = value;
    },

    handleinputto(value) {
      this.$emit("inputto", value);
      this.userinputto = value;
    },

    // from输入框change事件
    handleinputchangefrom(value) {
      const newval = this.setprecisionvalue(value);
      this.userinputform = newval;
      // 如果初始化数字的精度不符合代码设置时重置数字
      this.userinputto = this.setprecisionvalue(this.userinputto);
      if (!this.userinputform && !this.userinputto) {
        this.$emit("input", []);
        this.$emit("changefrom", newval);
        return;
      }

      if (!this.userinputto) {
        this.userinputform = newval;
      } else {
        // 最小值大于最大值时逻辑判断
        this.userinputform =
          !newval || parsefloat(newval) <= parsefloat(this.userinputto)
            ? newval
            : this.userinputto;
      }
      this.$emit("input", [this.userinputform, this.userinputto]);
      this.$emit("changefrom", newval);
    },

    // to输入框change事件
    handleinputchangeto(value) {
      const newval = this.setprecisionvalue(value);
      this.userinputto = newval;
      this.userinputform = this.setprecisionvalue(this.userinputform);
      if (!this.userinputto && !this.userinputform) {
        this.$emit("input", []);
        this.$emit("changefrom", newval);
        return;
      }

      if (!this.userinputform) {
        this.userinputto = newval;
      } else {
        // 最大值小于最小值时逻辑判断
        this.userinputto =
          !newval || parsefloat(newval) >= parsefloat(this.userinputform)
            ? newval
            : this.userinputform;
      }
      this.$emit("input", [this.userinputform, this.userinputto]);
      this.$emit("changeto", newval);
    },

    // 设置成精度数字
    setprecisionvalue(value) {
      if (!value) return null;
      const newval = number(value);
      // 如果是非数字空返回null
      if (isnan(value)) return null;
      if (typeof newval === "number" && this.precision !== undefined) {
        const val = this.toprecision(value, this.precision);
        return val;
      }
      return null;
    },
  },
};
</script>

<style lang="scss" scoped>
// 取消element原有的input框样式
::v-deep .el-input .el-input__inner {
  border: 0px;
  margin: 0;
  padding: 0 15px;
  background-color: transparent;
  text-align: center;
}

.input-number-range {
  background-color: #fff;
  border: 1px solid #dcdfe6;
  border-radius: 4px;
}

.flex {
  display: flex;
  flex-direction: row;
  width: 100%;
  justify-content: center;
  align-items: center;
  .center {
    margin-top: 1px;
  }
}

.is-disabled {
  background-color: #f5f7fa;
  border-color: #e4e7ed;
  color: #c0c4cc;
  cursor: not-allowed;
}
</style>

上述就是完整的组件代码,写好组件代码后,就是在项目中使用,有两种方式,一种是使用时在通过引用进行使用如下:

<template>
  <div>
   <inputnumberrange></inputnumberrange>
  </div>
</template>

<script>
import inputnumberrange from './components/inputnumberrange.vue'
export default { 
    name: "xxxx"
    components: {
    inputnumberrange,
  },
  data() {}
}
</script>

另一种方式是在main.js中进行全局组测,这样就可以自由使用<input-number-range>标签,如下:

import inputnumberrange from './components/inputnumberrange.vue' 
vue.component(inputnumberrange.name, inputnumberrange)

示例演示代码

<template>
  <div class="main">
    <!-- 演示操作按钮模块 -->
    <div class="caseheader">
      <div>
        <el-switch
          v-model="isdisabled"
          size="small"
          active-text="禁用"
          @change="switchchange"
        >
        </el-switch>
      </div>
      <div style="display: flex">
        <span>精度:</span>
        <el-input-number
          size="small"
          v-model="num"
          @change="precisionchange"
          :min="0"
          :max="10"
          label="描述文字"
        ></el-input-number>
      </div>
      <div>
        <el-button type="link" size="small" @click="reset">重置</el-button>
      </div>
    </div>
    <!-- 数字区间使用模块 -->
    <div class="numberrange">
      <el-form ref="form" :model="formdata" label-width="80px">
        <el-form-item label="数字区间">
          <input-number-range
            :disabled="isdisabled"
            :precision="num"
            v-model="formdata.numberrange"
          ></input-number-range>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  name: "testcase",
  data() {
    return {
      isdisabled: false, // 是否禁用
      num: 0, // 精度
      formdata: {
        numberrange: [],
      },
    };
  },
  methods: {
    /** 重置方法 */
    reset() {
      this.formdata.numberrange = [];
    }
  },
};
</script>

<style lang="scss" scoped>
.main {
  width: 100%;
  margin: 16px;
  position: relative;
}

.numberrange {
  width: 400px;
}

.caseheader {
  width: 400px;
  display: flex;
  justify-content: space-between;
  margin: 24px;
}
</style>

上述就是数字区间输入组件的实现即演示内容

到此这篇关于基于element ui input组件自行封装“数字区间”输入框组件的文章就介绍到这了,更多相关element ui数字区间输入组件内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

《基于element UI input组件自行封装“数字区间”输入框组件的问题及解决.doc》

下载本文的Word格式文档,以方便收藏与打印。