zs
2025-05-08 67ccbefb82b868fa1aa4bada181ed9dd2ddf92b7
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
import {
  defineComponent,
  ref,
  computed,
  onMounted,
  watch,
  onUnmounted,
  Component,
} from 'vue'
import styles from './TableFilter.module.scss'
import { CaretBottom } from '@element-plus/icons-vue'
import IconButton from '@/components/IconButton/IconButton'
 
import ElInput from 'element-plus/es/components/input/index'
import ElSelect from 'element-plus/es/components/select/index'
import Select from '@/components/Select/Select'
import Option from '@/components/Select/Option'
import Icon from '../Icon/Icon'
import { FormPropsType, FormItemPropType, PropsType } from '../DyForm/DyForm.d'
import isNil from 'lodash/isNil'
import { useVModel } from '@vueuse/core'
 
const formItemElementMap: Record<string, any> = {
  input: ElInput,
  select: Select,
}
 
const Type: Record<string, string> = {
  select: 'select',
}
 
interface FieldMapType {
  [key: string]: Array<{
    label: string
    value: string | number | boolean
  }>
}
export default defineComponent({
  name: '表格筛选',
  props: {
    title: {
      type: String,
      default: '',
    },
    columns: {
      type: Array,
      default: () => [],
    },
    tableRef: {
      type: Object,
      default: null,
    },
    modelValue: {
      type: Object,
      default: null,
    },
    text: {
      type: String,
      default: '',
    },
    fieldMap: {
      type: Object,
      default: () => ({}),
    },
    options: {
      type: Array,
      default: () => [],
    },
    defaultOptions: {
      type: Array,
      default: () => [],
    },
  },
  emits: ['update:modelValue', 'data', 'change'],
  setup(props, { attrs, slots, emit }) {
    const visible = ref(false)
    const columnsFilter = ref<FormItemPropType>([])
    const isDisabled = ref<boolean>(false)
    const defaultData = ref({})
    const data = ref({})
    const optionMap = ref<Record<string, any>>({})
    const form = computed({
      get() {
        return props.modelValue
      },
      set(v) {
        emit('update:modelValue', v)
      },
    })
 
    const formData = props.modelValue ? form : data
    let flag = false
    /**
     * 添加筛选条件
     */
    const onAddFilter = (evt?: Event) => {
      if (!flag) {
        evt?.stopPropagation()
 
        let length = columnsFilter.value.length
        for (let index = 0; index < props.columns.length; index++) {
          const element: any = props.columns[index]
          if (element.el && !columnsFilter.value.includes(element)) {
            columnsFilter.value.push(element)
            break
          }
        }
        if (length === columnsFilter.value.length - 1) {
          isDisabled.value = true
        }
      }
    }
 
    /**
     * 重置所有状态,初始化表格
     *  */
    const onReset = () => {
      // columnsFilter.value = []
      // Object.entries(defaultData.value).forEach(([key, value]) => {
      //   formData.value[key] = value
      // })
      // onAddFilter()
      // onSearchTable()
    }
    /**
     * 打开弹窗
     */
    const onListener = () => {
      visible.value = false
    }
    /**
     * 子组件点击
     * @param evt
     */
    const onChildClick = async (evt: Event) => {
      evt.stopPropagation()
      await getOptions()
      visible.value = true
    }
    /**
     * 表格搜索
     */
    const onSearchTable = () => {
      const tableRef =
        props.tableRef && props.tableRef.value
          ? props.tableRef.value
          : props.tableRef
      const data: Record<string, any> = { ...formData.value }
 
      Object.entries(data).forEach(([key, value]) => {
        if (value === null || value === undefined) {
          data[key] = ''
        }
      })
      if (tableRef) {
        tableRef?.getList?.(data)
      } else {
        emit('data', data)
        emit('change', data)
      }
    }
    const hasFormData = computed(() => {
      if (Object.keys(formData.value).length === 0) return false
      return Object.entries(formData.value).every(([key, value]) => {
        return value !== null && value !== undefined && value !== ''
      })
    })
    /**
     * 初始化disabled状态
     */
    const initDisabled = () => {
      const els = props.columns.filter((column: any) => column.el)
      if (els.length <= 1) {
        isDisabled.value = true
      } else {
        isDisabled.value = false
      }
    }
 
    watch(
      () => props.columns,
      (val) => {
        if (props.columns.length) {
          onAddFilter()
          initDisabled()
          flag = true
        }
      },
      {
        immediate: true,
        deep: true,
      }
    )
 
    onMounted(() => {
      if (props.columns.length) {
        onAddFilter()
        initDisabled()
        flag = true
      }
      defaultData.value = { ...props.modelValue }
      document.addEventListener('click', onListener)
    })
 
    onUnmounted(() => {
      document.removeEventListener('click', onListener)
    })
 
    // const getOptions = async (
    //   item: Record<string, any>,
    //   $props: Record<string, any>
    // ) => {
    //   if (typeof item.options === 'function') {
    //     return await item.options()
    //   } else {
    //     let options: any =
    //       item.options || props.fieldMap?.[$props.item.prop] || []
    //     if (props.defaultOptions.length) {
    //       options = options.concat(props.defaultOptions)
    //     }
    //     return options
    //   }
    // }
    const getOptions = async () => {
      const columns = props.columns
      for (let index = 0; index < columns.length; index++) {
        const column: any = columns[index]
        if (column.prop) {
          let options: any[] = []
          if (typeof column.options === 'function') {
            options = await column.options()
          } else {
            options = column.options || props.fieldMap?.[column.prop] || []
          }
          if (props.defaultOptions.length) {
            options = props.defaultOptions.concat(options)
          }
          optionMap.value[column.prop] = options
        }
      }
    }
 
    const onClickBtn = (evt: Event) => {
      evt.stopPropagation()
      flag = false
      onAddFilter()
    }
 
    /**
     * option
     * @param $props
     * @returns
     */
    const Column = ($props: any) => {
      const column: Record<string, any> = $props.item
      const options = optionMap.value[column.prop] || []
      if (Type[column.el]) {
        return options.map(
          (el: {
            label: string
            description: string
            value: string | number
          }) => (
            <Option
              label={el.description || el.label}
              value={el.value}
            ></Option>
          )
        )
      }
      return null
    }
 
    return () => {
      const icon = isDisabled.value ? 'icon_add2' : 'add-p'
      return (
        <div onClick={onChildClick}>
          <el-popover
            visible={visible.value}
            placement="bottom-start"
            width={212}
            show-arrow={false}
            popper-class={styles.popover}
            persistent={false}
            popper-style={{
              marginTop: '-7px',
              padding: '8px',
            }}
            trigger="click"
            vSlots={{
              reference: () => (
                <span class={{ [styles.textColor]: hasFormData.value }}>
                  {slots.default?.()}
                </span>
              ),
            }}
            onShow={onReset}
          >
            <div
              class={styles.box}
              onClick={(evt: Event) => {
                evt.stopPropagation()
              }}
            >
              {columnsFilter.value.map((column: any) => {
                const Widget = formItemElementMap[column.el] || null
                return Widget ? (
                  <div class={styles.filter}>
                    <span>{column.title}: </span>
                    <Widget
                      v-model={formData.value[column.prop]}
                      style="width: 119px"
                      size="small"
                      placeholder={`${column.placeholder}`}
                      onChange={onSearchTable}
                      clearable
                      teleported={false}
                    >
                      <Column item={column} />
                    </Widget>
                  </div>
                ) : null
              })}
            </div>
            <IconButton
              onClick={onClickBtn}
              icon={icon}
              disabled={isDisabled.value}
            >
              {props.text}
            </IconButton>
          </el-popover>
        </div>
      )
    }
  },
})