el-select 自定义检索下拉框

vue 组件实现,附事件实现方式与逻辑

html

<template>
  <div>
    <el-select :popper-append-to-body="false" style="width: 400px !important;" class="select" multiple collapse-tags
      v-model="comSampleCode" placeholder="请选择">
      <div slot="empty" class="select-head empty-head">
        <el-input clearable v-model="searchKey" @change="searchChange(searchKey)"
          @clear="clearChange()"></el-input>
        <p class="noMess">未检索到匹配数据!</p>
      </div>
      <div class="select-head">
        <el-input class="head-ipt" v-model="searchKey" @change="searchChange" placeholder="范围检索时请输入最大值及最小值,中间以-区分,如1-2'">
          <i class="el-icon-search el-input__icon" slot="suffix" @click="searchChange(searchKey)">
          </i></el-input>
        <el-checkbox class="head-check" v-model="checkState"
          :indeterminate="comSampleCode.length == currentOption.length" @change="checkSelectAll">
          全选
        </el-checkbox>
      </div>
      <el-option style="position: relative;" :label="item" :value="item" v-for="(item, index) in currentOption"
        :key="index">
      </el-option>
    </el-select>
  </div>
</template>

js

// 获取数字是否在俩个值之间
export function calRange ([one, two],num){
  let min = Math.min(one, two)
  let max = Math.max(one, two)
  return min < Number(num) && Number(num) < max
}


<script>
import { calRange } from '@/utils/mathPoint'    //引入函数在上面

export default {
  props: {
    sampleCode: {
      type: Array,
      default: () => []
    },
    ctaskBizCode: {
      type: String,
    }
  },
  data() {
    return {
      sampleAll: [],
      checkState: false,
      searchKey: '',
      currentOption: []
    }
  },
  computed: {
    comSampleCode: {
      get() {
        return this.sampleCode
      },
      set(val) {
        this.$emit('update:sampleCode', val)
      }
    },
  },
  watch: {
    ctaskBizCode: {
      immediate: true,
      handler() {
        this.initData()
      }

    }
  },
  methods: {
    initData() {
      this.$request({
        url: this.$baseApi.SAMPLEMANAGE.queryAllSample,
        method: "GET",
        params: { ctaskCode: this.ctaskBizCode,sort:'code,desc' },
      }).then(
        (res) => {
          this.sampleAll = res;
          this.currentOption = this.sampleAll.map(o => o.code)
        },
        (err) => {
          this.sampleAll = [];
        }
      );
    },
    //全选事件
    checkSelectAll(val) {
      if (val) {
        this.comSampleCode = this.currentOption
      } else {
        this.comSampleCode = []
      }
    },
    //清空事件
    clearChange() {
      this.currentOption = this.sampleAll.map(o => o.code)
    },
    //检索事件
    searchChange(val) {
      if (!val) {
        return this.clearChange()
      }
      const [one, two] = val.split('-')
      let arr = []
      this.sampleAll.forEach(o => {
        let str = o.code.split('-').at(-1)
        if (two && calRange([one, two], str)) {
          arr.push(o.code)
        } else if (str == val || o.code == val) {
          arr.push(o.code)
        }
      })
      this.currentOption = arr

    }
  }
}

style

<style lang="scss" scoped>
.noMess {
  width: 100%;
  line-height: 50px;
  height: 50px;
  text-align: center;
}

.select {
  position: relative;

  ::v-deep {
    .el-select-dropdown__list{
      padding: 0;
    }
    .el-checkbox__label{
      padding-left: 2px;
    }
    .el-select-dropdown {
      width: 400px !important;
    }

    .select-head {
      padding: 10px 10px 0 15px;
      margin-top: 0;
      position: sticky;
      display: flex;
      top: 0;
      left: 0;
      width: 100%;
      z-index: 9999;
      background-color: #fff;

      .head-ipt {
        width: 80%;
      }
      
      .head-check {
        text-align: center;
        width: 20%;
      }
    }
    .empty-head{
      flex-direction: column;
    }
  }
}
</style>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值