index.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. import { reactive, ref, shallowRef, computed, UnwrapRef } from 'vue'
  2. import { FilterData, FilterOption, DataTableOptions } from './types'
  3. import _ from 'lodash'
  4. export function useDataTable<T>(options: DataTableOptions = {}) {
  5. // 数据源
  6. const dataSource = ref<T[]>([])
  7. // 总条数
  8. const total = shallowRef(0)
  9. // 每页条数
  10. const pageSize = shallowRef(options.pageSize ?? 20)
  11. // 当前页码
  12. const pageIndex = shallowRef(options.pageIndex ?? 1)
  13. // 总页数
  14. const pageCount = computed(() => Math.ceil(total.value / pageSize.value) || 1)
  15. // 过滤筛选值
  16. const filterData = shallowRef<FilterData<T>[]>([])
  17. // 数据列表
  18. const dataList = computed<UnwrapRef<T[]>>({
  19. get() {
  20. const result = dataSource.value.filter((row) => {
  21. // 过滤所有查询条件
  22. return filterData.value.every((e) => {
  23. return e.fields.some((field) => {
  24. const value = row[field]
  25. if (typeof value === 'number') {
  26. return e.values.includes(value as T[keyof T])
  27. }
  28. return e.values.some((text) => {
  29. return String(value).toLowerCase().indexOf(String(text)) >= 0
  30. })
  31. })
  32. })
  33. })
  34. // 本地分页
  35. if (options.localPagination) {
  36. total.value = result.length
  37. return result.slice((pageIndex.value - 1) * pageSize.value, pageIndex.value * pageSize.value)
  38. }
  39. return result
  40. },
  41. set(val) {
  42. dataSource.value = val
  43. }
  44. })
  45. return {
  46. dataList,
  47. pageIndex,
  48. pageSize,
  49. pageCount,
  50. total,
  51. filterData,
  52. }
  53. }
  54. export function useDataFilter<T>(defaultOption: FilterOption<T, keyof T>) {
  55. const queryParams = shallowRef<Partial<T>>({})
  56. const defaultOptionCopy = _.cloneDeep(defaultOption)
  57. const filterOption: FilterOption<T, keyof T> = reactive(Object.create(defaultOptionCopy))
  58. // 重置所有过滤条件
  59. const resetFilters = () => {
  60. const { filters } = _.cloneDeep(defaultOption)
  61. const items = filters.map((defaultItem, index) => {
  62. const currentItem = filterOption.filters[index]
  63. return currentItem.locked ? currentItem : defaultItem
  64. })
  65. filterOption.filters = items
  66. }
  67. // 获取过滤参数,支持多条件查询
  68. const getFilterParams = (clear = false) => {
  69. if (clear) resetFilters()
  70. const options: FilterData<T>[] = []
  71. // filterOption.items.forEach((e) => {
  72. // const { keys, value } = e
  73. // if (value) {
  74. // options.push({
  75. // keys,
  76. // filteredValue: [value],
  77. // })
  78. // }
  79. // })
  80. return options
  81. }
  82. // 获取查询参数,支持多条件查询
  83. const getQueryParams = (clear = false) => {
  84. if (clear) resetFilters()
  85. const params: Partial<T> = Object.create({})
  86. filterOption.filters.forEach((e) => {
  87. if (e.value !== undefined) {
  88. params[e.field] = e.value
  89. }
  90. })
  91. return params
  92. }
  93. return {
  94. queryParams,
  95. filterOption,
  96. getFilterParams,
  97. getQueryParams,
  98. resetFilters
  99. }
  100. }