index.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. import { reactive, ref, shallowRef, computed, UnwrapRef } from 'vue'
  2. import { FilterValue, FilterOptions, DataTableOptions } from './types'
  3. export function useDataTable<T>(options: DataTableOptions = {}) {
  4. // 数据源
  5. const dataSource = ref<T[]>([])
  6. // 总条数
  7. const total = shallowRef(0)
  8. // 每页条数
  9. const pageSize = shallowRef(options.pageSize ?? 20)
  10. // 当前页码
  11. const pageIndex = shallowRef(options.pageIndex ?? 1)
  12. // 总页数
  13. const pageCount = computed(() => Math.ceil(total.value / pageSize.value) || 1)
  14. // 过滤筛选值
  15. const filters = shallowRef<FilterValue<T>[]>([])
  16. // 数据列表
  17. const dataList = computed<UnwrapRef<T[]>>({
  18. get() {
  19. const result = dataSource.value.filter((row) => {
  20. // 过滤所有查询条件
  21. return filters.value.every((e) => {
  22. return e.keys.some((key) => {
  23. const value = row[key]
  24. if (typeof value === 'number') {
  25. return e.filteredValue.includes(value as T[keyof T])
  26. }
  27. return e.filteredValue.some((text) => {
  28. return String(value).toLowerCase().indexOf(String(text)) >= 0
  29. })
  30. })
  31. })
  32. })
  33. // 本地分页
  34. if (options.localPagination) {
  35. total.value = result.length
  36. return result.slice((pageIndex.value - 1) * pageSize.value, pageIndex.value * pageSize.value)
  37. }
  38. return result
  39. },
  40. set(val) {
  41. dataSource.value = val
  42. }
  43. })
  44. return {
  45. dataList,
  46. pageIndex,
  47. pageSize,
  48. pageCount,
  49. total,
  50. filters,
  51. }
  52. }
  53. export function useDataFilter<T>() {
  54. const filterOptions: FilterOptions<T> = reactive({
  55. selectList: [],
  56. inputList: [],
  57. buttonList: [],
  58. })
  59. // 清空所有过滤条件
  60. const clearAll = () => {
  61. filterOptions.selectList.forEach((e) => {
  62. if (!e.locked) {
  63. e.selectedValue = undefined
  64. }
  65. })
  66. filterOptions.inputList.forEach((e) => {
  67. if (!e.locked) {
  68. e.value = undefined
  69. }
  70. })
  71. }
  72. // 获取过滤参数,支持多条件查询
  73. const getFilterParams = (clear = false) => {
  74. clear && clearAll()
  75. const options: FilterValue<T>[] = []
  76. filterOptions.selectList.forEach((e) => {
  77. const { key, selectedValue } = e
  78. if (selectedValue !== undefined) {
  79. options.push({
  80. keys: [key],
  81. filteredValue: [selectedValue],
  82. })
  83. }
  84. })
  85. filterOptions.inputList.forEach((e) => {
  86. const { keys, value } = e
  87. if (value) {
  88. options.push({
  89. keys,
  90. filteredValue: [value],
  91. })
  92. }
  93. })
  94. return options
  95. }
  96. // 获取查询参数,支持多条件查询
  97. const getQueryParams = (clear = false) => {
  98. clear && clearAll()
  99. const params: T = Object.create({})
  100. filterOptions.selectList.forEach((e) => {
  101. if (e.selectedValue !== undefined) {
  102. params[e.key] = e.selectedValue
  103. }
  104. })
  105. filterOptions.inputList.forEach((e) => {
  106. e.keys.forEach((key) => {
  107. if (e.value) {
  108. params[key] = e.value
  109. }
  110. })
  111. })
  112. return params
  113. }
  114. return {
  115. filterOptions,
  116. getFilterParams,
  117. getQueryParams,
  118. }
  119. }