|
|
@@ -1,4 +1,4 @@
|
|
|
-import { reactive, ref, shallowRef, toRefs, computed, UnwrapRef } from 'vue'
|
|
|
+import { reactive, ref, shallowRef, computed, UnwrapRef } from 'vue'
|
|
|
import { FilterValue, FilterOptions, DataTableOptions } from './interface'
|
|
|
|
|
|
export function useDataTable<T>(options: DataTableOptions = {}) {
|
|
|
@@ -14,48 +14,6 @@ export function useDataTable<T>(options: DataTableOptions = {}) {
|
|
|
const pageCount = computed(() => Math.ceil(total.value / pageSize.value) || 1)
|
|
|
// 过滤筛选值
|
|
|
const filters = shallowRef<FilterValue<T>[]>([])
|
|
|
- // 过滤选项
|
|
|
- const filterOptons: FilterOptions<T> = reactive({
|
|
|
- selectList: [],
|
|
|
- inputList: [],
|
|
|
- buttonList: [
|
|
|
- {
|
|
|
- lable: '重置',
|
|
|
- onClick: () => {
|
|
|
- filterOptons.selectList.forEach((e) => {
|
|
|
- if (!e.noClear) {
|
|
|
- e.selectedValue = undefined
|
|
|
- }
|
|
|
- })
|
|
|
- filterOptons.inputList.forEach((e) => {
|
|
|
- if (!e.noClear) {
|
|
|
- e.value = undefined
|
|
|
- }
|
|
|
- })
|
|
|
- pageIndex.value = 1
|
|
|
- filterMethod.onReset()
|
|
|
- }
|
|
|
- },
|
|
|
- {
|
|
|
- lable: '查询',
|
|
|
- className: 'el-button--primary', // 这里 className 不应该使用框架样式,以后再解决
|
|
|
- onClick: () => {
|
|
|
- pageIndex.value = 1
|
|
|
- filterMethod.onSearch()
|
|
|
- }
|
|
|
- },
|
|
|
- ]
|
|
|
- })
|
|
|
-
|
|
|
- // 数据过滤方法
|
|
|
- const filterMethod = {
|
|
|
- onReset: () => {
|
|
|
- filters.value = []
|
|
|
- },
|
|
|
- onSearch: () => {
|
|
|
- filters.value = getFilterParams()
|
|
|
- }
|
|
|
- }
|
|
|
|
|
|
// 数据列表
|
|
|
const dataList = computed<UnwrapRef<T[]>>({
|
|
|
@@ -75,7 +33,8 @@ export function useDataTable<T>(options: DataTableOptions = {}) {
|
|
|
})
|
|
|
})
|
|
|
// 本地分页
|
|
|
- if (options?.pagination) {
|
|
|
+ if (options.pagination) {
|
|
|
+ total.value = result.length
|
|
|
return result.slice((pageIndex.value - 1) * pageSize.value, pageIndex.value * pageSize.value)
|
|
|
}
|
|
|
return result
|
|
|
@@ -85,11 +44,45 @@ export function useDataTable<T>(options: DataTableOptions = {}) {
|
|
|
}
|
|
|
})
|
|
|
|
|
|
+ return {
|
|
|
+ dataList,
|
|
|
+ total,
|
|
|
+ pageIndex,
|
|
|
+ pageSize,
|
|
|
+ pageCount,
|
|
|
+ filters,
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+export function useDataFilter<T extends object>() {
|
|
|
+ const filterOptons: FilterOptions<T> = reactive({
|
|
|
+ selectList: [],
|
|
|
+ inputList: [],
|
|
|
+ buttonList: [],
|
|
|
+ })
|
|
|
+
|
|
|
+ // 清空所有过滤条件
|
|
|
+ const clearAll = () => {
|
|
|
+ filterOptons.selectList.forEach((e) => {
|
|
|
+ if (!e.locked) {
|
|
|
+ e.selectedValue = undefined
|
|
|
+ }
|
|
|
+ })
|
|
|
+
|
|
|
+ filterOptons.inputList.forEach((e) => {
|
|
|
+ if (!e.locked) {
|
|
|
+ e.value = undefined
|
|
|
+ }
|
|
|
+ })
|
|
|
+ }
|
|
|
+
|
|
|
// 获取过滤参数,支持多条件查询
|
|
|
- const getFilterParams = () => {
|
|
|
+ const getFilterParams = (callback: (params: FilterValue<T>[]) => void, clear = false) => {
|
|
|
+ clear && clearAll()
|
|
|
const options: FilterValue<T>[] = []
|
|
|
+
|
|
|
filterOptons.selectList.forEach((e) => {
|
|
|
- const { key, selectedValue } = e;
|
|
|
+ const { key, selectedValue } = e
|
|
|
if (selectedValue !== undefined) {
|
|
|
options.push({
|
|
|
keys: [key],
|
|
|
@@ -97,8 +90,9 @@ export function useDataTable<T>(options: DataTableOptions = {}) {
|
|
|
})
|
|
|
}
|
|
|
})
|
|
|
+
|
|
|
filterOptons.inputList.forEach((e) => {
|
|
|
- const { keys, value } = e;
|
|
|
+ const { keys, value } = e
|
|
|
if (value) {
|
|
|
options.push({
|
|
|
keys,
|
|
|
@@ -106,17 +100,21 @@ export function useDataTable<T>(options: DataTableOptions = {}) {
|
|
|
})
|
|
|
}
|
|
|
})
|
|
|
- return options
|
|
|
+
|
|
|
+ callback && callback(options)
|
|
|
}
|
|
|
|
|
|
// 获取查询参数,支持多条件查询
|
|
|
- const getQueryParams = () => {
|
|
|
- const params: { [K in keyof T]?: T[K] } = {}
|
|
|
+ const getQueryParams = (callback: (params: Partial<T>) => void, clear = false) => {
|
|
|
+ clear && clearAll()
|
|
|
+ const params: Partial<T> = {}
|
|
|
+
|
|
|
filterOptons.selectList.forEach((e) => {
|
|
|
if (e.selectedValue !== undefined) {
|
|
|
params[e.key] = e.selectedValue
|
|
|
}
|
|
|
})
|
|
|
+
|
|
|
filterOptons.inputList.forEach((e) => {
|
|
|
e.keys.forEach((key) => {
|
|
|
if (e.value) {
|
|
|
@@ -124,17 +122,13 @@ export function useDataTable<T>(options: DataTableOptions = {}) {
|
|
|
}
|
|
|
})
|
|
|
})
|
|
|
- return params
|
|
|
+
|
|
|
+ callback && callback(params)
|
|
|
}
|
|
|
|
|
|
return {
|
|
|
- dataList,
|
|
|
- total,
|
|
|
- pageIndex,
|
|
|
- pageSize,
|
|
|
- pageCount,
|
|
|
- filterMethod,
|
|
|
+ filterOptons,
|
|
|
+ getFilterParams,
|
|
|
getQueryParams,
|
|
|
- ...toRefs(filterOptons),
|
|
|
}
|
|
|
}
|