zhou zhou
10 小时以前 46d872c1a5b77aa8799de4a64888a0a24a1422d6
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
<!-- 表格组件 -->
<!-- 支持:el-table 全部属性、事件、插槽,同官方文档写法 -->
<!-- 扩展功能:分页组件、渲染自定义列、loading、表格全局边框、斑马纹、表格尺寸、表头背景配置 -->
<!-- 获取 ref:默认暴露了 elTableRef 外部通过 ref.value.elTableRef 可以调用 el-table 方法 -->
<template>
  <div class="art-table" :class="{ 'is-empty': isEmpty }" :style="containerHeight">
    <ElTable ref="elTableRef" v-loading="!!loading" v-bind="mergedTableProps">
      <template v-for="col in columns" :key="col.prop || col.type">
        <!-- 渲染全局序号列 -->
        <ElTableColumn v-if="col.type === 'globalIndex'" v-bind="{ ...col }">
          <template #default="{ $index }">
            <span>{{ getGlobalIndex($index) }}</span>
          </template>
        </ElTableColumn>
 
        <!-- 渲染展开行 -->
        <ElTableColumn v-else-if="col.type === 'expand'" v-bind="cleanColumnProps(col)">
          <template #default="{ row }">
            <component :is="col.formatter ? col.formatter(row) : null" />
          </template>
        </ElTableColumn>
 
        <!-- 渲染普通列 -->
        <ElTableColumn v-else v-bind="cleanColumnProps(col)">
          <template v-if="col.useHeaderSlot && col.prop" #header="headerScope">
            <slot
              :name="col.headerSlotName || `${col.prop}-header`"
              v-bind="{ ...headerScope, prop: col.prop, label: col.label }"
            >
              {{ col.label }}
            </slot>
          </template>
          <template v-if="col.useSlot && col.prop" #default="slotScope">
            <slot
              v-if="shouldRenderSlotScope(slotScope)"
              :name="col.slotName || col.prop"
              v-bind="{
                ...slotScope,
                prop: col.prop,
                value: col.prop ? slotScope.row[col.prop] : undefined
              }"
            />
          </template>
        </ElTableColumn>
      </template>
 
      <template v-if="$slots.default" #default><slot /></template>
 
      <template #empty>
        <div v-if="loading"></div>
        <ElEmpty v-else :description="emptyText" :image-size="120" />
      </template>
    </ElTable>
 
    <div
      class="pagination custom-pagination"
      v-if="showPagination"
      :class="mergedPaginationOptions?.align"
      ref="paginationRef"
    >
      <ElPagination
        v-bind="mergedPaginationOptions"
        :total="pagination?.total"
        :disabled="loading"
        :page-size="pagination?.size"
        :current-page="pagination?.current"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
  </div>
</template>
 
<script setup>
  import { ref, computed, nextTick, watchEffect, getCurrentInstance, useAttrs } from 'vue'
  import { storeToRefs } from 'pinia'
  import { useTableStore } from '@/store/modules/table'
  import { useCommon } from '@/hooks/core/useCommon'
  import { useTableHeight } from '@/hooks/core/useTableHeight'
  import { useResizeObserver, useWindowSize } from '@vueuse/core'
  defineOptions({ name: 'ArtTable' })
  const { width } = useWindowSize()
  const elTableRef = ref(null)
  const paginationRef = ref()
  const tableHeaderRef = ref()
  const tableStore = useTableStore()
  const { isBorder, isZebra, tableSize, isFullScreen, isHeaderBackground } = storeToRefs(tableStore)
  const props = defineProps({
    loading: { required: false, default: false },
    columns: { required: false, default: () => [] },
    pagination: { required: false },
    paginationOptions: { required: false },
    fit: { required: false, default: true },
    showHeader: { required: false, default: true },
    stripe: { required: false, default: void 0 },
    border: { required: false, default: void 0 },
    size: { required: false, default: void 0 },
    emptyHeight: { required: false, default: '100%' },
    emptyText: { required: false, default: '暂无数据' },
    showTableHeader: { required: false, default: true }
  })
  const instance = getCurrentInstance()
  const attrs = useAttrs()
  const LAYOUT = {
    MOBILE: 'prev, pager, next, sizes, jumper, total',
    IPAD: 'prev, pager, next, jumper, total',
    DESKTOP: 'total, prev, pager, next, sizes, jumper'
  }
  const layout = computed(() => {
    if (width.value < 768) {
      return LAYOUT.MOBILE
    } else if (width.value < 1024) {
      return LAYOUT.IPAD
    } else {
      return LAYOUT.DESKTOP
    }
  })
  const DEFAULT_PAGINATION_OPTIONS = {
    pageSizes: [10, 20, 30, 50, 100],
    align: 'center',
    background: true,
    layout: layout.value,
    hideOnSinglePage: false,
    size: 'default',
    pagerCount: width.value > 1200 ? 7 : 5
  }
  const mergedPaginationOptions = computed(() => ({
    ...DEFAULT_PAGINATION_OPTIONS,
    ...props.paginationOptions
  }))
  const border = computed(() => props.border ?? isBorder.value)
  const stripe = computed(() => props.stripe ?? isZebra.value)
  const size = computed(() => props.size ?? tableSize.value)
  const isEmpty = computed(() => props.data?.length === 0)
  const paginationHeight = ref(0)
  const tableHeaderHeight = ref(0)
  useResizeObserver(paginationRef, (entries) => {
    const entry = entries[0]
    if (entry) {
      requestAnimationFrame(() => {
        paginationHeight.value = entry.contentRect.height
      })
    }
  })
  useResizeObserver(tableHeaderRef, (entries) => {
    const entry = entries[0]
    if (entry) {
      requestAnimationFrame(() => {
        tableHeaderHeight.value = entry.contentRect.height
      })
    }
  })
  const PAGINATION_SPACING = computed(() => (props.showTableHeader ? 6 : 15))
  const { containerHeight } = useTableHeight({
    showTableHeader: computed(() => props.showTableHeader),
    paginationHeight,
    tableHeaderHeight,
    paginationSpacing: PAGINATION_SPACING
  })
  const height = computed(() => {
    if (isFullScreen.value) return '100%'
    if (isEmpty.value && !props.loading) return props.emptyHeight
    if (props.height) return props.height
    return '100%'
  })
  const headerCellStyle = computed(() => ({
    background: isHeaderBackground.value
      ? 'var(--el-fill-color-lighter)'
      : 'var(--default-box-color)',
    ...(props.headerCellStyle || {})
    // 合并用户传入的样式
  }))
  const hasExplicitTableProp = (propName) => {
    const rawProps = instance?.vnode.props || {}
    const kebabName = propName.replace(/[A-Z]/g, (match) => `-${match.toLowerCase()}`)
    return propName in rawProps || kebabName in rawProps
  }
  const mergedTableProps = computed(() => ({
    ...attrs,
    ...props,
    height: height.value,
    stripe: stripe.value,
    border: border.value,
    size: size.value,
    headerCellStyle: headerCellStyle.value,
    // Element Plus 默认值为 true,未显式传入时不应被 ArtTable 覆盖成 false。
    selectOnIndeterminate: hasExplicitTableProp('selectOnIndeterminate')
      ? props.selectOnIndeterminate
      : void 0
  }))
  const showPagination = computed(() => props.pagination && !isEmpty.value)
  const shouldRenderSlotScope = (slotScope) => {
    return slotScope.$index === void 0 || slotScope.$index >= 0
  }
  const cleanColumnProps = (col) => {
    const columnProps = { ...col }
    delete columnProps.useHeaderSlot
    delete columnProps.headerSlotName
    delete columnProps.useSlot
    delete columnProps.slotName
    return columnProps
  }
  const handleSizeChange = (val) => {
    emit('pagination:size-change', val)
  }
  const handleCurrentChange = (val) => {
    emit('pagination:current-change', val)
    scrollToTop()
  }
  const { scrollToTop: scrollPageToTop } = useCommon()
  const scrollToTop = () => {
    nextTick(() => {
      elTableRef.value?.setScrollTop(0)
      scrollPageToTop()
    })
  }
  const getGlobalIndex = (index) => {
    if (!props.pagination) return index + 1
    const { current, size: size2 } = props.pagination
    return (current - 1) * size2 + index + 1
  }
  const emit = defineEmits(['pagination:size-change', 'pagination:current-change'])
  const findTableHeader = () => {
    if (!props.showTableHeader) {
      tableHeaderRef.value = void 0
      return
    }
    const tableHeader = document.getElementById('art-table-header')
    if (tableHeader) {
      tableHeaderRef.value = tableHeader
    } else {
      tableHeaderRef.value = void 0
    }
  }
  watchEffect(
    () => {
      void props.data?.length
      const shouldShow = props.showTableHeader
      if (shouldShow) {
        nextTick(() => {
          findTableHeader()
        })
      } else {
        tableHeaderRef.value = void 0
      }
    },
    { flush: 'post' }
  )
  defineExpose({
    scrollToTop,
    elTableRef
  })
</script>
 
<style lang="scss" scoped>
  @use './style';
</style>