提交
This commit is contained in:
1 parent
7f1f60e259
commit
c55f22a603
855 files changed
+4758807
-1373323
No files matched your search
@@ -0,0 +1,73 @@
|
||||
/**
|
||||
* 列头动态 facet(搜选项 + 勾选)选项加载。
|
||||
* 与 useServerColumnFilters 配合:defs 中目标列 type=enum,打开漏斗时拉去重可选值。
|
||||
*/
|
||||
import { reactive } from 'vue'
|
||||
import { ElMessage } from 'element-plus'
|
||||
import type { ColumnFilterDef } from '@/composables/useClientColumnFilters'
|
||||
|
||||
export type FacetOption = { value: string; label: string; count: number }
|
||||
|
||||
export function useColumnFacetOptions(options: {
|
||||
/** 需要动态拉可选值的列 prop(与 defs.prop 一致,亦作为 field 传给接口) */
|
||||
facetFields: string[]
|
||||
/** 请求可选值;应返回 { list: FacetOption[] } 或已解包的 list 结构 */
|
||||
fetchFacet: (payload: {
|
||||
field: string
|
||||
keyword?: string
|
||||
limit?: number
|
||||
}) => Promise<unknown>
|
||||
/** 非 facet 的静态 enum(字典等)回退 */
|
||||
columnFilterDefs?: ColumnFilterDef[]
|
||||
limit?: number
|
||||
}) {
|
||||
const facetFieldSet = new Set(options.facetFields)
|
||||
const facetOptionsMap = reactive<Record<string, FacetOption[]>>({})
|
||||
const facetLoadingMap = reactive<Record<string, boolean>>({})
|
||||
const limit = options.limit ?? 500
|
||||
|
||||
function enumOptionsFor(prop: string): FacetOption[] {
|
||||
if (Object.prototype.hasOwnProperty.call(facetOptionsMap, prop)) {
|
||||
return facetOptionsMap[prop]
|
||||
}
|
||||
const def = options.columnFilterDefs?.find((d) => d.prop === prop)
|
||||
return (def?.enumOptions || []).map((o) => ({
|
||||
value: String(o.value),
|
||||
label: o.label,
|
||||
count: (o as any).count ?? 0
|
||||
}))
|
||||
}
|
||||
|
||||
function optionsLoadingFor(prop: string) {
|
||||
return Boolean(facetLoadingMap[prop])
|
||||
}
|
||||
|
||||
async function onFilterOpen(prop: string) {
|
||||
if (!facetFieldSet.has(prop)) return
|
||||
facetLoadingMap[prop] = true
|
||||
try {
|
||||
const res = await options.fetchFacet({ field: prop, limit })
|
||||
const data = (res as any)?.list ? res : (res as any)?.data || res || {}
|
||||
const list = (data as any).list || []
|
||||
facetOptionsMap[prop] = (Array.isArray(list) ? list : []).map((item: any) => ({
|
||||
value: String(item?.value ?? ''),
|
||||
label: String(item?.label ?? item?.value ?? ''),
|
||||
count: Number(item?.count ?? 0)
|
||||
}))
|
||||
} catch (e: any) {
|
||||
facetOptionsMap[prop] = []
|
||||
ElMessage.error(e?.message || '加载可选值失败')
|
||||
} finally {
|
||||
facetLoadingMap[prop] = false
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
facetOptionsMap,
|
||||
facetLoadingMap,
|
||||
enumOptionsFor,
|
||||
optionsLoadingFor,
|
||||
onFilterOpen,
|
||||
isFacetField: (prop: string) => facetFieldSet.has(prop)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user