wangrong
2021-11-24 130b72d6c5734f8acaac0214b6dfe5c8bfa319c3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
41
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
 
<template>
  <el-form :inline="true" size="mini">
    <el-form-item
      v-for="(filterInfo, key) in filter"
      :key="key"
      :label="filterInfo.text"
    >
      <!--单选框-->
      <el-radio-group
        v-model="returnValue[key]"
        v-if="filterInfo.type == 'radioGroup'"
        :style="`width:${filterInfo.width};`"
      >
        <el-radio-button
          :label="option.id"
          v-for="(option, index) in filterInfo.options"
          :key="index"
          >{{ option.name }}</el-radio-button
        >
      </el-radio-group>
 
      <!--搜索类型为输入框-->
      <el-input
        v-else-if="filterInfo.type == 'input'"
        v-model="returnValue[key]"
        :placeholder="filterInfo.placeholder"
        :style="`width:${filterInfo.width};`"
      >
      </el-input>
 
      <!--搜索类型输入框远程搜索服务端数据-->
      <el-autocomplete
        v-else-if="filterInfo.type == 'autocomplete'"
        v-model="returnValue[key]"
        :placeholder="filterInfo.placeholder"
        :fetch-suggestions="querySearchAsync"
        :trigger-on-focus="true"
        :style="`width:${filterInfo.width};`"
      >
      </el-autocomplete>
      <!--搜索类型为日期或日期时间-->
      <el-date-picker
        v-else-if="
          filterInfo.type == 'date' ||
          filterInfo.type == 'datetime' ||
          filterInfo.type == 'month' ||
          filterInfo.type == 'year'
        "
        :format="
          filterInfo.type == 'date'
            ? 'yyyy-MM-dd'
            : filterInfo.type == 'datetime'
            ? 'yyyy-MM-dd HH:mm:ss'
            : filterInfo.type == 'month'
            ? 'yyyy-MM'
            : 'yyyy'
        "
        v-model="returnValue[key]"
        :type="filterInfo.type"
        :style="`width:${filterInfo.width};`"
        @change="pickerChange(filterInfo, key, returnValue[key])"
        :placeholder="filterInfo.placeholder"
      >
      </el-date-picker>
 
      <!--搜索类型为日期范围或日期时间范围-->
      <el-date-picker
        v-else-if="
          filterInfo.type == 'daterange' || filterInfo.type == 'datetimerange'
        "
        v-model="returnValue[key]"
        :type="filterInfo.type"
        range-separator="-"
        start-placeholder="开始日期"
        end-placeholder="结束日期"
        @change="pickerRangeChange(filterInfo, key, returnValue[key])"
        :style="`width:${filterInfo.width};`"
        :placeholder="filterInfo.placeholder"
      >
      </el-date-picker>
 
      <!--搜索类型为选择器-->
      <el-select
        v-else-if="filterInfo.type == 'select'"
        v-model="returnValue[key]"
        clearable
        :multiple="filterInfo.multiple"
        :collapse-tags="filterInfo.multiple"
        :placeholder="filterInfo.placeholder"
        :style="`width:${filterInfo.width};`"
      >
        <el-option
          v-for="(option, index) in filterInfo.options"
          :key="index"
          :label="option.name"
          :value="option.id"
        >
        </el-option>
      </el-select>
    </el-form-item>
 
    <el-form-item>
      <el-button type="primary" icon="el-icon-search" @click="handleSearch"
        >查询</el-button
      >
      <el-button type="info" icon="el-icon-delete" @click="handleClear"
        >清空查询</el-button
      >
    </el-form-item>
  </el-form>
</template>
<script>
export default {
  data() {
    return {
      returnValue: {},
    };
  },
  watch: {},
  props: ["filter"],
  created() {
    this.handleSearch();
  },
  methods: {
    pickerChange(info, key, value) {
      switch (info.type) {
        case "date":
          this.returnValue[key] = this.format(value, "yyyy-MM-dd");
          break;
        case "month":
          this.returnValue[key] = this.format(value, "yyyy-MM");
          break;
        case "year":
          this.returnValue[key] = this.format(value, "yyyy");
          break;
        case "datetime":
          this.returnValue[key] = this.format(value, "yyyy-MM-dd hh:mm:ss");
          break;
      }
    },
    pickerRangeChange(info, key, value) {
      switch (info.type) {
        case "daterange":
          for (let i in value) {
            this.returnValue[key][i] = this.format(value[i], "yyyy-MM-dd");
          }
          break;
        case "datetimerange":
          for (let i in value) {
            this.returnValue[key][i] = this.format(
              value[i],
              "yyyy-MM-dd hh:mm:ss"
            );
          }
          break;
      }
    },
    handleSearch() {
      let options = [];
      for (let key in this.returnValue) {
        if (typeof this.filter[key] != "undefined") {
          let value = this.returnValue[key];
          if (value != "" && value != []) {
            options.push({
              type: this.filter[key].sql_type,
              name: key,
              value: this.returnValue[key],
            });
          }
        }
      }
      this.$emit("handleSearch", options);
    },
    handleClear() {
      let options = [];
      for (let key in this.returnValue) {
        if (typeof this.filter[key] != "undefined") {
          if (typeof this.returnValue[key] == "object") {
            this.returnValue[key] = [];
          } else if (typeof this.filter[key].default != "undefined") {
            this.returnValue[key] = this.filter[key].default;
            options.push({
              type: this.filter[key].sql_type,
              name: key,
              value: this.returnValue[key],
            });
          } else {
            this.returnValue[key] = "";
          }
        } else {
          continue;
        }
      }
      this.$emit("handleSearch", options);
    },
  },
};
</script>