• 基于elementui 2.x版本input写的input模糊查询组件


    最近做项目中,用到了input框中的模糊查询,由于项目中用到了element-ui,于是就去查看了当中的selecct组件,其中通过添加 filterable  remote  reserve-keyword  remote-method 等相关属性即可实现模糊查询效果,自己也根据效果写了基于 el-input 的模糊查询效果,相关组件代码如下:
    <template>
      <!-- 基于element-ui2.x版本input写的input模糊查询组件 -->
      <div>
        <div ref="selectOption">
          <el-input
            v-model="selectInputVal"
            :placeholder="placeholder"
            @input="inputHandle"
          ></el-input>
          <ul
            class="options"
            v-if="optionsList.length && selectInputVal.length > 0"
            :style="'' + selectOptionWidth + 'px;top:' + selectOptionTop + 'px'"
          >
            <li
              v-for="item in optionsList"
              :key="item.id"
              @click="clickOption(item)"
            >
              {{ item.label }}
            </li>
          </ul>
        </div>
      </div>
    </template>
     
    <script>
    export default {
      data() {
        return {
          selectInputVal: this.value,
          selectOptionWidth: 0,
          selectOptionTop: 0,
        };
      },
      methods: {
        getOptionsOffset() {
          this.selectOptionWidth = this.$refs.selectOption.children[0].offsetWidth;
          this.selectOptionTop = this.$refs.selectOption.children[0].offsetHeight;
        },
        inputHandle(value) {
          this.$emit("selectInputHandle", value);
        },
        clickOption(info) {
          this.$emit("selectOptionItem", info);
        },
        inputBlurHandle() {
          this.$emit("selectBlurHandle");
        },
      },
      created() {},
      mounted() {
        this.getOptionsOffset();
        document.addEventListener("click", this.inputBlurHandle);
      },
      props: {
        value: {
          default: "",
        },
        placeholder: {
          type: String,
          default: "",
        },
        optionsList: {
          type: Array,
          default: () => [],
        },
      },
      watch: {
        value(val) {
          this.selectInputVal = val;
        },
      },
      beforeDestroy() {
        document.removeEventListener("click", this.inputBlurHandle);
      },
    };
    </script>
    
    <style scoped>
    * {
      margin: 0;
      padding: 0;
      list-style: none;
    }
    ul.options {
      position: absolute;
      left: 0;
      border: 1px solid #dcdfe6;
      border-radius: 4px;
      z-index: 99999;
      background-color: #fff;
      box-sizing: border-box;
      max-height: 300px;
      overflow-y: auto;
    }
    li {
      cursor: pointer;
      border-bottom: 1px solid #dcdfe6;
      user-select: none;
    }
    li:hover {
      background-color: #dcdfe6;
    }
    li:last-child {
      border-bottom: none;
    }
    </style>
  • 相关阅读:
    odoo 自定义视图
    Odoo 模型之间的关系 笔记
    C#中计算两点之间连线的角度
    Jquery中1.6.x中新的方法prop()方法
    VS2010快捷键说明
    将DATAtable转换成 json格式
    在IIS中执行EXE文件时的问题
    WebDev.WebServer40.exe已停止工作
    sqllite developer过期解决方案
    c#的DateTime.Now函数详解
  • 原文地址:https://www.cnblogs.com/xiaoyaoxingchen/p/16390742.html
Copyright © 2020-2023  润新知