Blame view

src/components/CompanySelect/index.vue 3.78 KB
218a9837   monkeyhouyi   运营主体优化
1
2
3
4
5
6
7
8
9
10
11
12
13
14
  <template>
    <!-- 运营主体选择 -->
    <el-select
      v-model="inputValue"
      :placeholder="placeholder"
      clearable
      style="width: 100%"
      @change="inputValueChange"
      filterable
      remote
      :filter-method="filterFunction"
      @visible-change="inputVisibleChange"
      :loading="listLoading"
      v-selectLoadMore="initMoreList"
f1a44e73   monkeyhouyi   1
15
      :disabled="disabled"
218a9837   monkeyhouyi   运营主体优化
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
    >
      <el-option
        v-for="item in options"
        :key="item.id"
        :label="item.label"
        :value="item.id"
      >
      </el-option>
    </el-select>
  </template>
    
    <script>
    import request from "@/utils/request";
    import { getCompanyInfoList } from "@/api/baseData/company";
    export default {
      name: "el-company-select",
      computed: {},
      props: {
        value: {
          request: true,
          type: String,
          default: '',
        },
        isInput: { type: Boolean, default: false },
        placeholder: { type: String, default: '请选择' },
f1a44e73   monkeyhouyi   1
41
        disabled: { type: Boolean, default: false }
218a9837   monkeyhouyi   运营主体优化
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
      },
     
      data() {
        return {
          inputValue: '',
          options: [],
          currentPage: 1,
          total: 0,
          listLoading: false,
          searchVal: '', // 输入查询内容
          searchVisibla: false, // 是否需要打开下拉时重新请求
          searchBlur: false, // 是否输入
          valid: true, // 防抖使用
          timer: null, // 节流
        }
      },
      created() {
        this.loadList(true);
      },
      watch:{
        value(val) {
          this.initInputValue()
        },
      },
      methods: {
        async initInputValue() {
          this.searchVisibla = true;
          if(!this.searchBlur) {
            this.listLoading = true;
            await this.loadList(true, this.value);
          };
          this.inputValue = this.value;
          this.searchBlur = false;
        },
        loadList(type, id) {
          // type: true-替换 false-添加
          if(!type && this.total < this.options.length) return;
          let obj = {
            currentPage: this.currentPage,
            pageSize: 20,
            sort: "desc",
            sidx: "",
            companyId: id,
            companyName: this.searchVal,
          }
          getCompanyInfoList(obj).then((res) => {
            let list = res.data.list.map(v => {
              return { label: v.companyName, id: v.id}
            })
            let allList = type ? list : [...this.options, ...list];
            this.options = _.uniqBy(allList, 'id');
            this.total = res.data.pagination.total;
            this.listLoading = false;
          });
        },
        inputValueChange(val) {
          this.inputValue = val;
          this.$emit('input', val, 'change');
          this.$emit('change', val, 'change');
        },
        inputValueBlur(val) {
          this.searchBlur = true;
          this.inputValue = val;
          this.$emit('input', val, 'blur');
          this.$emit('change', val, 'blur');
        },
        filterFunction(val) {
          clearTimeout(this.timer);
          this.timer = setTimeout(() => {
            !this.isInput && this.inputValueBlur(val)
            this.currentPage = 1;
            this.searchVal = val;
            this.loadList(true);
          }, 300);
        },
        async inputVisibleChange(val) {
          if(!val) return;
          console.log(this.searchVal);
          if(this.searchVal) {
            this.searchVal = '';
            this.currentPage = 1;
            await this.loadList(true);
          }
          if(this.searchVisibla) {
            await this.loadList(false);
            this.searchVisibla = false;
          }
        },
        initMoreList() {
          if (this.valid) {
            this.valid = false; // 关闭阀门
            // 如果阀门已经打开,就继续往下
            setTimeout(() => {
              ++this.currentPage;
              this.loadList(false);
              this.valid = true; // 执行完成后打开阀门
            }, 300);
          }
        }
      },
    }
    </script>