Element基于el-input數(shù)字范圍輸入框的實(shí)現(xiàn)
數(shù)字范圍組件
在做篩選時(shí)可能會(huì)出現(xiàn)數(shù)字范圍的篩選,例如:價(jià)格、面積,但是elementUI本身沒(méi)有自帶的數(shù)字范圍組件,于是進(jìn)行了簡(jiǎn)單的封裝,不足可自行進(jìn)行優(yōu)化
滿足功能:
- 最小值與最大值的相關(guān)約束,當(dāng)最大值存在,最小值大于最大值且失焦,自動(dòng)將最小值賦值為最大值,反之亦然。
- 擁有el-input組件本身的屬性綁定以及方法
- 可設(shè)置精度,默認(rèn)精度為0
- 可使用
el-input
插槽,但需要加前綴start-
,end-
進(jìn)行區(qū)分
<numberRange :startValue.sync="startValue" :endValue.sync="endValue" />
相關(guān)代碼:
<template> <div class="input-number-range" :class="{ 'is-disabled': disabled }"> <div class="flex"> <el-input ref="inputFromRef" clearable v-model="startValue" :disabled="disabled" :placeholder="startPlaceholder" @blur="handleBlurFrom" @focus="handleFocusFrom" @input="handleInputFrom" @change="handleInputChangeFrom" v-bind="$attrs" v-on="$listeners" > <template v-for="(value, name) in startSlots" #[name]="slotData"> <slot :name="name" v-bind="slotData || {}"></slot> </template> </el-input> <div class="center"> <span>至</span> </div> <el-input ref="inputToRef" clearable v-model="endValue" :disabled="disabled" :placeholder="endPlaceholder" @blur="handleBlurTo" @focus="handleFocusTo" @input="handleInputTo" @change="handleInputChangeTo" v-bind="$attrs" v-on="$listeners" > <template v-for="(value, name) in endSlots" #[name]="slotData"> <slot :name="name" v-bind="slotData || {}"></slot> </template> </el-input> </div> </div> </template> <script> export default { name: "InputNumberRange", props: { // inputs: { // type: Array, // required: true, // default: () => [null, null], // }, startValue: { type: Number || String, default: null, }, endValue: { typeof: Number || String, default: null, }, // 是否禁用 disabled: { type: Boolean, default: false, }, startPlaceholder: { type: String, default: "最小值", }, endPlaceholder: { type: String, default: "最大值", }, // 精度參數(shù) precision: { type: Number, default: 0, validator(val) { return val >= 0 && val === parseInt(val, 10); }, }, }, data() { return {}; }, computed: { startSlots() { const slots = {}; Object.keys(this.$slots).forEach((name) => { if (name.startsWith("start-")) { const newKey = name.replace(/^start-/, ""); slots[newKey] = this.$slots[name]; } }); return slots; }, endSlots() { const slots = {}; Object.keys(this.$slots).forEach((name) => { if (name.startsWith("end-")) { const newKey = name.replace(/^end-/, ""); slots[newKey] = this.$slots[name]; } }); return slots; }, }, watch: {}, methods: { handleInputFrom(value) { this.$emit("update:startValue", value); }, handleInputTo(value) { this.$emit("update:endValue", value); }, // from輸入框change事件 handleInputChangeFrom(value) { // 如果是非數(shù)字空返回null if (value == "" || isNaN(value)) { this.$emit("update:startValue", null); return; } // 初始化數(shù)字精度 const newStartValue = this.setPrecisionValue(value); // 如果from > to 將from值替換成to if ( typeof newStartValue === "number" && parseFloat(newStartValue) > parseFloat(this.endValue) ) { this.startValue = this.endValue; } else { this.startValue = newStartValue; } if (this.startValue !== value) { this.$emit("update:startValue", this.startValue); } }, // to輸入框change事件 handleInputChangeTo(value) { // 如果是非數(shù)字空返回null if (value == "" || isNaN(value)) { this.$emit("update:endValue", null); return; } // 初始化數(shù)字精度 const newEndValue = this.setPrecisionValue(value); // 如果from > to 將from值替換成to if ( typeof newEndValue === "number" && parseFloat(newEndValue) < parseFloat(this.startValue) ) { this.endValue = this.startValue; } else { this.endValue = newEndValue; } if (this.endValue !== value) { this.$emit("update:endValue", this.endValue); } }, handleBlurFrom(event) { this.$emit("blur-from", event); }, handleFocusFrom(event) { this.$emit("focus-from", event); }, handleBlurTo(event) { this.$emit("blur-to", event); }, handleFocusTo(event) { this.$emit("focus-to", event); }, // 根據(jù)精度保留數(shù)字 toPrecision(num, precision) { if (precision === undefined) precision = 0; return parseFloat( Math.round(num * Math.pow(10, precision)) / Math.pow(10, precision) ); }, // 設(shè)置精度 setPrecisionValue(value) { if (this.precision === undefined) return value; return this.toPrecision(parseFloat(value), this.precision); }, }, }; </script> <style lang="scss" scoped> // 取消element原有的input框樣式 ::v-deep .el-input__inner { border: 0px; margin: 0; padding: 0 15px; background-color: transparent; } .input-number-range { background-color: #fff; border: 1px solid #dcdfe6; border-radius: 4px; } .flex { display: flex; flex-direction: row; width: 100%; height: auto; justify-content: center; align-items: center; .center { margin-top: 1px; } } .is-disabled { background-color: #f5f7fa; border-color: #e4e7ed; color: #c0c4cc; cursor: not-allowed; } </style>
到此這篇關(guān)于 Element基于el-input數(shù)字范圍輸入框的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān) Element el-input輸入框內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue2路由動(dòng)畫(huà)效果的實(shí)現(xiàn)代碼
本篇文章主要介紹了Vue2路由動(dòng)畫(huà)效果的實(shí)現(xiàn)代碼,可以根據(jù)不同的路徑去改變動(dòng)畫(huà)的效果,有興趣的可以了解一下2017-07-07在Vue中延遲執(zhí)行某個(gè)函數(shù)的實(shí)現(xiàn)方式
在Vue中延遲執(zhí)行某個(gè)函數(shù),你可以使用setTimeout()函數(shù)或者Vue提供的生命周期鉤子函數(shù),本文通過(guò)一些示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2023-12-12vue-router 路由傳參問(wèn)題(路由傳參方式)
路由傳參主要有兩種方式一種是路徑傳參一種是參數(shù)傳遞,本文結(jié)合示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2023-11-11vue-cli創(chuàng)建項(xiàng)目ERROR?in?Conflict:?Multiple?assets?emit?dif
最近vue/cli創(chuàng)建項(xiàng)目后出現(xiàn)了錯(cuò)誤,下面這篇文章主要給大家介紹了關(guān)于vue-cli創(chuàng)建項(xiàng)目ERROR?in?Conflict:?Multiple?assets?emit?different?content?to?the?same?filename?index.html問(wèn)題的解決辦法,需要的朋友可以參考下2023-02-02vue.js路由mode配置之去掉url上默認(rèn)的#方法
今天小編就為大家分享一篇vue.js路由mode配置之去掉url上默認(rèn)的#方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-11-11關(guān)于element-ui?select?下拉框位置錯(cuò)亂問(wèn)題解決
這篇文章主要介紹了關(guān)于element-ui?select?下拉框位置錯(cuò)亂問(wèn)題解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-09-09elementplus實(shí)現(xiàn)多級(jí)表格(最后一級(jí)展示圖片)
本文主要介紹了elementplus實(shí)現(xiàn)多級(jí)表格(最后一級(jí)展示圖片),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-05-05vue控制滾動(dòng)條滑到某個(gè)位置的方法實(shí)例
當(dāng)容器有滾動(dòng)條時(shí),有時(shí)需要將滾動(dòng)條滑到某個(gè)位置,下面這篇文章主要給大家介紹了關(guān)于vue控制滾動(dòng)條滑到某個(gè)位置的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-12-12