vue ant autocomplete控件再封装

控件代码:

<template>
  <a-auto-complete v-if="type=='autocomplete'"
                   :data-source="dataSource"
                   :placeholder="placeholder"
                   :filter-option="filterOption"
                   @change="change"
                   @select="select"
                   allowClear
                   v-model="selId"
                   ref="auto"
                   option-label-prop="title"
  >
    <template slot="dataSource">
      <a-select-option v-for="opt in dataSource" :key="opt.id" :title="opt.residentName + opt.phone">
        <a-row>
          <a-col :span="6">{{opt.residentName}}</a-col>
          <a-col :span="6">{{opt.phone}}</a-col>
          <a-col :span="6">{{opt.idCard}}</a-col>
          <a-col :span="6">{{opt.communityName}}{{opt.buildingNo}}</a-col>
        </a-row>

      </a-select-option>
    </template>
  </a-auto-complete>
  <a-input v-else-if="type=='input'" :placeholder="placeholder" @change="changeInput" v-model="selId" allowClear></a-input>
</template>

<script>
  import {getAction} from '@/api/manage'

  //支持autocomplete和input两种格式
  //type : autocomplete , input
  export default {
    name: "AutoResident",
    model: {
      prop: 'resident',
      event: 'selResident'
    },
    props: {
      placeholder: {type: String, default: '请用姓名、拼音首字母、身份证号、电话号码快速查找'},
      resident: {
        type: Object, default: () => {
        }
      },
      type: {type: String, default: 'autocomplete'}
    },
    data() {
      return {
        dataSource: [],
        selId: this.resident ? this.resident.id : '',
      }
    },
    created() {
      if (this.resident) {
        this.dataSource.push(this.resident);
      }
    },
    watch: {
      resident(newVal) {
        this.selId = newVal.id;
        if (this.dataSource.length == 0) {
          if (JSON.stringify(this.resident) != "{}") {
            this.dataSource.push(this.resident);
          }
        }
      }
    },
    methods: {
      filterOption(input, option) {
        let row = this.dataSource[this.dataSource.findIndex(f => f.id == option.key)];
        return row.residentName.indexOf(input) >= 0
          || row.py.toUpperCase().indexOf(input.toUpperCase()) >= 0
          || row.phone.indexOf(input) >= 0
          || row.idCard.toUpperCase().indexOf(input.toUpperCase()) >= 0

        // return this.pinyin.getCamelChars(option.componentOptions.children[0].text).toUpperCase().indexOf(input.toUpperCase()) >= 0;
      },
      change(value) {
        if (value) {
          this.list();
        } else {
          //输入框清空了,设置选择对象为空,清除数据源
          this.$emit('selResident', {});
          this.dataSource = [];
          this.$emit('onDataChange', this.dataSource);
        }
      },
      select(value) {
        let selObj = {};
        if (this.dataSource.length > 0) {
          for (let i = 0; i < this.dataSource.length; i++) {
            if (this.dataSource[i].id == value) {
              selObj = this.dataSource[i];
              break;
            }
          }
        }
        if (JSON.stringify(selObj) != "{}") {
          this.$emit('selResident', selObj);
          this.$emit('onSelect', selObj);
          this.dataSource=[];
          this.dataSource.push(selObj);
          this.$emit('onDataChange',this.dataSource);//选择后的数据源只留下选择的这个数据
        }
      },
      changeInput() {
        this.list();
      },
      list() {
        if (!this.selId) {
          this.dataSource = [];
          this.$emit('onDataChange', this.dataSource);
          return;
        }
        if (this.dataSource.findIndex(f => f.id == this.selId) >= 0) return;//此时已是选择后,不用再查找了

        let params = {residentName: this.selId, pageSize: 30};
        getAction('/hoscard/resident/list', params).then(res => {
          if (res.success) {
            this.dataSource = [];
            for (let i = 0; i < res.result.records.length; i++) {
              let item = res.result.records[i];
              //赋值这几个可以不需要
              item.value = item.id;
              item.text = item.residentName;
              item.label = item.residentName;
              this.dataSource.push(item);
            }
            this.$emit('onDataChange', this.dataSource);
          }
        })
      }
    }

  }
</script>

<style scoped>

</style>

重点是filterOption方法中的过滤,用option中key,在数据源中查找,符合条件的就返回true,change事件中用输入值向后台请求查询,结果返回后抛出 onDataChange事件,调用方可以在这个事件中使用数据源做其他事情。
option-label-prop=“title” 指定填充到select中的属性值,在选择后用于回填,下拉项是自定义的格式,显示多列数据。值 “title” 是指option的属性, ,如果不设置,默认是value

使用代码:

<auto-resident v-model="queryParam.resident"  @onDataChange="residentDataChang"></auto-resident>


queryParam.resident 是一个对象。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值