cwg-tabel.vue 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840
  1. <template>
  2. <view>
  3. <!-- 统一表格容器,根据屏幕宽度自适应 -->
  4. <view class="table-container" :class="{ 'mobile-table': isMobile }">
  5. <uni-table :type="selectionType" :border="false" @selection-change="handleSelectionChange" emptyText="">
  6. <!-- 表头:根据设备类型显示不同的列 -->
  7. <uni-tr class="table-header">
  8. <uni-th v-for="column in displayColumns" :key="column.prop" :align="column.align || 'center'"
  9. :width="column.width || '200'" :class="[headerClass, { sortable: column.sortable }]"
  10. :style="getHeaderStyle(column)" @click="column.sortable && handleSort(column)">
  11. <view class="header-content">
  12. {{ column.label }}
  13. <view v-if="column.sortable" class="sort-icon">
  14. <uni-icons :type="getSortIcon(column)" :size="14" color="#999" />
  15. </view>
  16. </view>
  17. </uni-th>
  18. </uni-tr>
  19. <!-- 表格主体 -->
  20. <template v-for="(row, rowIndex) in tableData" :key="rowIndex">
  21. <uni-tr>
  22. <!-- 数据列:根据设备类型动态渲染 -->
  23. <uni-td v-for="column in displayColumns" :key="column.prop" :align="column.align || 'center'"
  24. :class="getCellClass(column, row)" :style="getCellStyle(column, row)"
  25. @click="openRowDetail(row)">
  26. <template v-if="column.slot">
  27. <slot :name="column.slot" :row="row" :column="column" :index="rowIndex">
  28. {{ row[column.prop] }}
  29. </slot>
  30. </template>
  31. <uni-tag v-else-if="column.type === 'tag'" :text="formatTagText(row[column.prop], column)"
  32. :type="formatTagType(row[column.prop], column)" size="small" />
  33. <view v-else-if="column.type === 'file'">
  34. <cwg-file :path="row[column.prop]" />
  35. </view>
  36. <cwg-icon v-else-if="column.type === 'more'" name="crm-chevron-down"
  37. class="crm-chevron-down" :size="16" color="#007" />
  38. <template v-else>
  39. {{ formatCellValue(row[column.prop], column, row) }}
  40. </template>
  41. </uni-td>
  42. </uni-tr>
  43. <!-- 桌面端展开行(仅当非移动端且行展开时显示) -->
  44. <uni-tr v-if="!isMobile && expandedRows[rowIndex]" class="expand-row">
  45. <uni-td :colspan="columnSpan" class="expand-cell">
  46. <slot name="expand" :row="row" :rowIndex="rowIndex">
  47. <view class="default-expand-content">
  48. <text class="no-content">暂无展开内容</text>
  49. </view>
  50. </slot>
  51. </uni-td>
  52. </uni-tr>
  53. </template>
  54. <!-- 总计行 -->
  55. <uni-tr v-if="showSummary" class="summary-row">
  56. <uni-td v-for="(column, index) in displayColumns" :key="'summary-' + column.prop" :align="column.align || 'center'"
  57. class="summary-cell" :style="getCellStyle(column, currentSummaryData)">
  58. <!-- 当有 summaryMethod 时,按照返回的数组直接渲染 -->
  59. <template v-if="props.summaryMethod">
  60. <text>{{ computedSummaryRow[index] !== undefined ? computedSummaryRow[index] : '' }}</text>
  61. </template>
  62. <template v-else>
  63. <!-- 特定列的总计自定义插槽,例如 #summary-volume="{ row }" -->
  64. <template v-if="$slots['summary-' + column.prop]">
  65. <slot :name="'summary-' + column.prop" :row="currentSummaryData" :column="column" :index="index"></slot>
  66. </template>
  67. <!-- 如果没有特定插槽,尝试常规渲染 -->
  68. <template v-else>
  69. <template v-if="currentSummaryData && currentSummaryData[column.prop] !== undefined && currentSummaryData[column.prop] !== null">
  70. <!-- 如果有常规插槽,并且总计数据里有该字段的值,则复用常规插槽渲染 -->
  71. <slot v-if="column.slot" :name="column.slot" :row="currentSummaryData" :column="column" :index="index">
  72. {{ currentSummaryData[column.prop] }}
  73. </slot>
  74. <!-- 否则使用格式化函数 -->
  75. <text v-else>{{ formatCellValue(currentSummaryData[column.prop], column, currentSummaryData) }}</text>
  76. </template>
  77. <!-- 如果总计数据里没有该字段的值,第一列显示总计文本,其他留空 -->
  78. <text v-else>{{ index === 0 ? summaryText : '' }}</text>
  79. </template>
  80. </template>
  81. </uni-td>
  82. </uni-tr>
  83. </uni-table>
  84. </view>
  85. <!-- 空状态 -->
  86. <view v-if="tableData.length === 0">
  87. <cwg-empty-state />
  88. </view>
  89. <!-- 分页 -->
  90. <view class="pagination-container" v-if="showPagination && tableData.length > 0">
  91. <view class="pagination-info">
  92. <text>共 {{ pagination.total }} 条记录</text>
  93. <view v-if="showPageSize" class="page-size-select">
  94. <text>每页显示</text>
  95. <picker @change="handlePageSizeChange" :value="pageSizeIndex" :range="pageSizes">
  96. <view class="page-size-value">{{ pagination.pageSize }}条/页</view>
  97. </picker>
  98. </view>
  99. </view>
  100. <view class="pagination">
  101. <view class="page-item prev" :class="{ disabled: pagination.current === 1 }"
  102. @click="handlePageChange('prev')">
  103. <uni-icons type="arrowleft" size="16" color="#666" />
  104. <text>上一页</text>
  105. </view>
  106. <view class="page-numbers">
  107. <view v-for="page in visiblePages" :key="page" class="page-number"
  108. :class="{ active: pagination.current === page }" @click="handlePageChange(page)">
  109. {{ page }}
  110. </view>
  111. </view>
  112. <view class="page-item next" :class="{ disabled: pagination.current === pagination.pages }"
  113. @click="handlePageChange('next')">
  114. <text>下一页</text>
  115. <uni-icons type="arrowright" size="16" color="#666" />
  116. </view>
  117. </view>
  118. </view>
  119. <!-- 移动端详情弹窗 -->
  120. <cwg-detail-popup v-model:visible="detailVisible" title="详情" :items="detailItems" />
  121. </view>
  122. </template>
  123. <script setup>
  124. import { ref, computed, watch, onMounted, onUnmounted } from 'vue'
  125. const props = defineProps({
  126. /**
  127. * 表格列配置(columns 数组中每一项是一个列配置对象)
  128. * - prop: 字段名,对应 row[prop],用于取值/排序/详情展示
  129. * - label: 表头显示文本(也会作为详情弹窗的 label)
  130. * - align: 对齐方式('left' | 'center' | 'right')
  131. * - width: 列宽(设置后该列固定宽度;未设置则自适应
  132. * - slot: 自定义渲染插槽名(<template #xxx="{ row, column, index }">),优先级最高
  133. * - formatter: ({ value, row }) => string | number,自定义格式化显示值(无 slot 时生效)
  134. * - sortable: 是否可排序(点击表头切换 asc/desc)
  135. * - headerStyle: 表头样式(对象),会合并到表头 style
  136. * - cellStyle: 单元格样式(对象或函数({ row, column }) => style)
  137. * - cellClass: 单元格 class(string 或函数({ row, column }) => string | string[])
  138. * - isTabel: false 时不在表格中显示该列,但仍会在“详情弹窗”中显示
  139. * - type: 内置渲染类型
  140. * - 'tag': 使用 uni-tag 渲染,搭配 tagMap/tagTypeMap
  141. * - 'file': 使用 cwg-file 渲染
  142. * - 'more': 展示“更多”图标(用于移动端点击查看详情等)
  143. * - 'date': 日期格式化(搭配 dateFormat)
  144. * - tagMap: 标签映射对象
  145. * - tagTypeMap: 标签类型映射对象
  146. * - dateFormat: 日期格式
  147. * */
  148. columns: { type: Array, required: true, default: () => [] },
  149. // 移动端配置
  150. mobilePrimaryCount: { type: Number, default: 3 },
  151. // 参数如columns
  152. mobilePrimaryFields: { type: Array, default: () => [] },
  153. // API 请求函数
  154. api: { type: Function, required: true },
  155. // 查询参数
  156. queryParams: { type: Object, default: () => ({}) },
  157. // 是否立即加载
  158. immediate: { type: Boolean, default: true },
  159. // 选择类型:'selection' | null
  160. selectionType: { type: String, default: null },
  161. // 是否显示每页条数选择
  162. showPageSize: { type: Boolean, default: true },
  163. // 是否显示分页
  164. showPagination: { type: Boolean, default: true },
  165. isViewDetail: { type: Boolean, default: true },
  166. // 每页条数选项
  167. pageSizes: { type: Array, default: () => [10, 20, 30, 50, 100] },
  168. // 默认每页条数
  169. defaultPageSize: { type: Number, default: 10 },
  170. // 表头样式自定义
  171. headerBackground: { type: String, default: '#fff' },
  172. headerColor: { type: String, default: 'var(--color-slate-800)' },
  173. headerFontSize: { type: [String, Number], default: '14rpx' },
  174. headerFontWeight: { type: [String, Number], default: 600 },
  175. headerHeight: { type: [String, Number], default: '40rpx' },
  176. headerClass: { type: [String, Array], default: '' },
  177. headerStyle: { type: Object, default: () => ({}) },
  178. stickyHeader: { type: Boolean, default: true },
  179. stickyOffset: { type: [String, Number], default: '0' },
  180. isPages: { type: Boolean, default: false },
  181. // 是否显示总计行
  182. showSummary: { type: Boolean, default: false },
  183. // 自定义总计数据,若不传则尝试使用 api 响应中的 res.sum
  184. summaryData: { type: Object, default: () => null },
  185. // 自定义的合计计算方法,如果配置了,则使用该方法计算总计数据
  186. summaryMethod: { type: Function, default: null },
  187. // 总计行第一列的默认文本
  188. summaryText: { type: String, default: '总计' },
  189. })
  190. const emit = defineEmits([
  191. 'selection-change',
  192. 'action-click',
  193. 'page-change',
  194. 'load-success',
  195. 'load-error',
  196. 'sort-change',
  197. 'go-pages'
  198. ])
  199. // ========== 响应式状态 ==========
  200. const tableData = ref([])
  201. const selectedItems = ref([])
  202. const detailVisible = ref(false)
  203. const detailItems = ref([])
  204. const pagination = ref({
  205. current: 1,
  206. pageSize: props.defaultPageSize,
  207. total: 0,
  208. pages: 0
  209. })
  210. const loading = ref(false)
  211. const expandedRows = ref({})
  212. const internalSummaryData = ref(null)
  213. const currentSummaryData = computed(() => {
  214. if (props.summaryData && Object.keys(props.summaryData).length > 0) return props.summaryData
  215. return internalSummaryData.value
  216. })
  217. const computedSummaryRow = computed(() => {
  218. if (props.showSummary && props.summaryMethod && typeof props.summaryMethod === 'function') {
  219. const result = props.summaryMethod({
  220. columns: displayColumns.value,
  221. data: tableData.value,
  222. summaryData: currentSummaryData.value
  223. });
  224. return Array.isArray(result) ? result : [];
  225. }
  226. return [];
  227. });
  228. // 移动端检测
  229. const isMobile = ref(false)
  230. // 排序状态
  231. const sortState = ref({ prop: '', order: '' }) // order: 'asc' | 'desc' | ''
  232. // ========== 计算属性 ==========
  233. // 显示的列(根据设备类型)
  234. const displayColumns = computed(() => {
  235. const filterForTable = (cols) => (cols || []).filter((c) => c && c.isTabel !== false)
  236. const normalizeColumns = (cols) => {
  237. if (!Array.isArray(cols)) return []
  238. if (cols.length === 0) return []
  239. const first = cols[0]
  240. if (typeof first === 'string') {
  241. return cols
  242. .map((prop) => props.columns.find((c) => c && c.prop === prop))
  243. .filter(Boolean)
  244. }
  245. return cols
  246. }
  247. if (!isMobile.value) return filterForTable(normalizeColumns(props.columns))
  248. if (props.mobilePrimaryFields && props.mobilePrimaryFields.length) {
  249. return filterForTable(normalizeColumns(props.mobilePrimaryFields))
  250. }
  251. return filterForTable(normalizeColumns(props.columns.slice(0, props.mobilePrimaryCount)))
  252. })
  253. // 列跨度(用于展开行)
  254. const columnSpan = computed(() => {
  255. let span = displayColumns.value.length
  256. if (props.showOperation) span += 1
  257. return span
  258. })
  259. // 每页条数索引
  260. const pageSizeIndex = computed(() => {
  261. return props.pageSizes.indexOf(pagination.value.pageSize)
  262. })
  263. // 可见页码
  264. const visiblePages = computed(() => {
  265. const maxVisible = 5
  266. const half = Math.floor(maxVisible / 2)
  267. let start = Math.max(1, pagination.value.current - half)
  268. let end = Math.min(pagination.value.pages, start + maxVisible - 1)
  269. if (end - start + 1 < maxVisible) {
  270. start = Math.max(1, end - maxVisible + 1)
  271. }
  272. return Array.from({ length: end - start + 1 }, (_, i) => start + i)
  273. })
  274. // ========== 工具函数 ==========
  275. // 获取表头样式
  276. const getHeaderStyle = (column) => {
  277. const baseStyle = {
  278. backgroundColor: props.headerBackground,
  279. color: props.headerColor,
  280. fontSize: typeof props.headerFontSize === 'number' ? props.headerFontSize + 'rpx' : props.headerFontSize,
  281. fontWeight: props.headerFontWeight,
  282. height: typeof props.headerHeight === 'number' ? props.headerHeight + 'rpx' : props.headerHeight,
  283. lineHeight: typeof props.headerHeight === 'number' ? props.headerHeight + 'rpx' : props.headerHeight,
  284. ...props.headerStyle
  285. }
  286. if (props.stickyHeader) {
  287. baseStyle.position = 'sticky'
  288. baseStyle.top = props.stickyOffset
  289. baseStyle.zIndex = 100
  290. }
  291. if (column.width) {
  292. const w = typeof column.width === 'number' ? column.width + 'px' : column.width
  293. baseStyle.width = w
  294. baseStyle.minWidth = w
  295. baseStyle.maxWidth = w
  296. }
  297. if (column.headerStyle) {
  298. Object.assign(baseStyle, column.headerStyle)
  299. }
  300. return baseStyle
  301. }
  302. // 获取单元格样式
  303. const getCellStyle = (column, row) => {
  304. const style = {}
  305. if (column.width) {
  306. const w = typeof column.width === 'number' ? column.width + 'px' : column.width
  307. style.width = w
  308. style.minWidth = w
  309. style.maxWidth = w
  310. style.whiteSpace = 'nowrap'
  311. style.overflow = 'hidden'
  312. style.textOverflow = 'ellipsis'
  313. }
  314. if (column.cellStyle) {
  315. if (typeof column.cellStyle === 'function') {
  316. Object.assign(style, column.cellStyle({ row, column }))
  317. } else {
  318. Object.assign(style, column.cellStyle)
  319. }
  320. }
  321. return style
  322. }
  323. // 获取单元格类名
  324. const getCellClass = (column, row) => {
  325. const classes = []
  326. if (column.cellClass) {
  327. if (typeof column.cellClass === 'function') {
  328. const result = column.cellClass({ row, column })
  329. if (result) {
  330. if (Array.isArray(result)) {
  331. classes.push(...result)
  332. } else {
  333. classes.push(result)
  334. }
  335. }
  336. } else {
  337. classes.push(column.cellClass)
  338. }
  339. }
  340. return classes.join(' ')
  341. }
  342. // 格式化单元格值
  343. const formatCellValue = (value, column, row) => {
  344. if (column.formatter) {
  345. return column.formatter({ value, row })
  346. }
  347. if (column.type === 'date' && value) {
  348. return formatDate(value, column.dateFormat || 'YYYY-MM-DD HH:mm:ss')
  349. }
  350. if (value === null || value === undefined) {
  351. return '-'
  352. }
  353. return value
  354. }
  355. // 格式化标签文本
  356. const formatTagText = (value, column) => {
  357. if (column.tagMap && column.tagMap[value]) {
  358. return column.tagMap[value]
  359. }
  360. return value || '-'
  361. }
  362. // 格式化标签类型
  363. const formatTagType = (value, column) => {
  364. if (column.tagTypeMap && column.tagTypeMap[value]) {
  365. return column.tagTypeMap[value]
  366. }
  367. return 'default'
  368. }
  369. // 格式化日期
  370. const formatDate = (date, format) => {
  371. if (!date) return '-'
  372. const d = new Date(date)
  373. const year = d.getFullYear()
  374. const month = String(d.getMonth() + 1).padStart(2, '0')
  375. const day = String(d.getDate()).padStart(2, '0')
  376. const hour = String(d.getHours()).padStart(2, '0')
  377. const minute = String(d.getMinutes()).padStart(2, '0')
  378. const second = String(d.getSeconds()).padStart(2, '0')
  379. return format
  380. .replace('YYYY', year)
  381. .replace('MM', month)
  382. .replace('DD', day)
  383. .replace('HH', hour)
  384. .replace('mm', minute)
  385. .replace('ss', second)
  386. }
  387. // 排序图标
  388. const getSortIcon = (column) => {
  389. if (sortState.value.prop !== column.prop) return 'arrow-up'
  390. return sortState.value.order === 'asc' ? 'arrow-up' : 'arrow-down'
  391. }
  392. // 处理排序
  393. const handleSort = (column) => {
  394. if (!column.sortable) return
  395. let newOrder = ''
  396. if (sortState.value.prop === column.prop) {
  397. if (sortState.value.order === '') newOrder = 'asc'
  398. else if (sortState.value.order === 'asc') newOrder = 'desc'
  399. else newOrder = ''
  400. } else {
  401. newOrder = 'asc'
  402. }
  403. sortState.value = { prop: newOrder ? column.prop : '', order: newOrder }
  404. emit('sort-change', { prop: sortState.value.prop, order: sortState.value.order })
  405. refreshTable()
  406. }
  407. // 展开行切换
  408. const toggleRowExpand = (rowIndex) => {
  409. const key = rowIndex
  410. if (expandedRows.value[key]) {
  411. expandedRows.value[key] = false
  412. } else {
  413. expandedRows.value = {}
  414. expandedRows.value[key] = true
  415. }
  416. emit('expand-change', { rowIndex, expanded: expandedRows.value[key] })
  417. }
  418. // 打开详情弹窗(移动端使用)
  419. const openRowDetail = (row) => {
  420. if (props.isPages) {
  421. emit('go-pages', row)
  422. } else {
  423. detailItems.value = props.columns
  424. .filter((column) => column && column.prop && column.label)
  425. .map((column) => ({
  426. label: column.label,
  427. value: String(formatCellValue(row[column.prop], column, row))
  428. }))
  429. detailVisible.value = true
  430. }
  431. }
  432. // ========== 数据加载 ==========
  433. const loadData = async () => {
  434. if (loading.value) return
  435. loading.value = true
  436. try {
  437. const applyPaginationFromRes = (res) => {
  438. const page = res && res.page
  439. if (!page) return false
  440. if (typeof page.current === 'number') pagination.value.current = page.current
  441. if (typeof page.row === 'number') pagination.value.pageSize = page.row
  442. if (typeof page.rowTotal === 'number') pagination.value.total = page.rowTotal
  443. if (typeof page.pageTotal === 'number') pagination.value.pages = page.pageTotal
  444. else if (typeof pagination.value.total === 'number' && typeof pagination.value.pageSize === 'number') {
  445. pagination.value.pages = Math.ceil(pagination.value.total / pagination.value.pageSize)
  446. }
  447. return true
  448. }
  449. const params = {
  450. page: {
  451. current: pagination.value.current,
  452. row: pagination.value.pageSize
  453. },
  454. ...props.queryParams
  455. }
  456. // 添加排序参数
  457. if (sortState.value.prop && sortState.value.order) {
  458. params.sort = { field: sortState.value.prop, order: sortState.value.order }
  459. }
  460. const res = await props.api(params)
  461. if (res.code === 200 || res.code === 0 || res.code === 10000) {
  462. if (res.sum) {
  463. internalSummaryData.value = res.sum
  464. } else {
  465. internalSummaryData.value = null
  466. }
  467. const data = res.data || res
  468. const hasPage = applyPaginationFromRes(res)
  469. if (Array.isArray(data)) {
  470. tableData.value = data
  471. if (!hasPage) {
  472. pagination.value.total = data.length
  473. pagination.value.pages = 1
  474. }
  475. } else if (data.list || data.records) {
  476. tableData.value = data.list || data.records
  477. if (!hasPage) {
  478. pagination.value.total = data.total || data.list?.length || 0
  479. pagination.value.pages = data.pages || Math.ceil(pagination.value.total / pagination.value.pageSize)
  480. }
  481. } else {
  482. tableData.value = []
  483. }
  484. emit('load-success', res)
  485. } else {
  486. throw new Error(res.message || '加载失败')
  487. }
  488. } catch (error) {
  489. console.error('表格数据加载失败:', error)
  490. uni.showToast({
  491. title: error.message || '加载失败',
  492. icon: 'none'
  493. })
  494. emit('load-error', error)
  495. } finally {
  496. loading.value = false
  497. }
  498. }
  499. // 刷新表格
  500. const refreshTable = () => {
  501. pagination.value.current = 1
  502. loadData()
  503. }
  504. const reload = () => {
  505. loadData()
  506. }
  507. // 分页变化
  508. const handlePageChange = (page) => {
  509. if (page === 'prev') {
  510. if (pagination.value.current > 1) pagination.value.current--
  511. else return
  512. } else if (page === 'next') {
  513. if (pagination.value.current < pagination.value.pages) pagination.value.current++
  514. else return
  515. } else {
  516. pagination.value.current = page
  517. }
  518. loadData()
  519. emit('page-change', pagination.value)
  520. }
  521. // 每页条数变化
  522. const handlePageSizeChange = (e) => {
  523. const size = props.pageSizes[e.detail.value]
  524. pagination.value.pageSize = size
  525. pagination.value.current = 1
  526. loadData()
  527. }
  528. // 选择变化
  529. const handleSelectionChange = (e) => {
  530. selectedItems.value = e.detail.value
  531. emit('selection-change', selectedItems.value)
  532. }
  533. // 获取选中项
  534. const getSelectedItems = () => selectedItems.value
  535. const clearSelection = () => { selectedItems.value = [] }
  536. // ========== 移动端检测 ==========
  537. const checkIsMobile = () => {
  538. // 适配 uni-app 环境
  539. // #ifdef H5
  540. const width = window.innerWidth
  541. isMobile.value = width < 991
  542. // #endif
  543. // #ifndef H5
  544. const systemInfo = uni.getSystemInfoSync()
  545. isMobile.value = systemInfo.windowWidth < 991
  546. // #endif
  547. }
  548. // 监听窗口大小变化(仅 H5)
  549. // #ifdef H5
  550. const handleResize = () => {
  551. checkIsMobile()
  552. }
  553. // #endif
  554. // ========== 监听参数变化 ==========
  555. watch(() => props.queryParams, () => {
  556. refreshTable()
  557. }, { deep: true })
  558. // ========== 生命周期 ==========
  559. onMounted(() => {
  560. checkIsMobile()
  561. // #ifdef H5
  562. window.addEventListener('resize', handleResize)
  563. // #endif
  564. if (props.immediate) {
  565. loadData()
  566. }
  567. })
  568. onUnmounted(() => {
  569. // #ifdef H5
  570. window.removeEventListener('resize', handleResize)
  571. // #endif
  572. })
  573. // 暴露方法
  574. defineExpose({
  575. refreshTable,
  576. reload,
  577. getSelectedItems,
  578. clearSelection,
  579. loadData,
  580. tableData,
  581. toggleRowExpand,
  582. pagination
  583. })
  584. </script>
  585. <style scoped lang="scss">
  586. @import "@/uni.scss";
  587. .table-container {
  588. width: 100%;
  589. // overflow-x: auto;
  590. // -webkit-overflow-scrolling: touch;
  591. max-height: calc(100vh - 209px);
  592. color: var(--color-slate-800);
  593. :deep(.uni-table-scroll) {
  594. width: 100%;
  595. max-height: calc(100vh - 375px);
  596. // overflow: auto;
  597. }
  598. :deep(.uni-table) {
  599. min-height: px2rpx(100) !important;
  600. .uni-table-th {
  601. position: sticky;
  602. top: 0;
  603. z-index: 100;
  604. transition: all 0.3s;
  605. .header-content {
  606. display: flex;
  607. align-items: center;
  608. justify-content: center;
  609. gap: 4px;
  610. .sort-icon {
  611. display: inline-flex;
  612. cursor: pointer;
  613. }
  614. }
  615. &.sortable {
  616. cursor: pointer;
  617. &:hover .sort-icon .uni-icons {
  618. color: var(--color-primary) !important;
  619. }
  620. }
  621. &::after {
  622. display: none;
  623. }
  624. }
  625. .uni-table-tr {
  626. &:last-child {
  627. border-bottom: none !important;
  628. }
  629. .uni-table-th {
  630. border-bottom: 1px solid #141d22 !important;
  631. }
  632. }
  633. .summary-row {
  634. background-color: #fafafa;
  635. font-weight: 600;
  636. .uni-table-td {
  637. border-top: 1px solid #ebeef5 !important;
  638. position: sticky;
  639. bottom: 0;
  640. z-index: 99;
  641. background-color: #fafafa;
  642. }
  643. }
  644. .uni-table-th,
  645. .uni-table-td {
  646. padding: px2rpx(16) px2rpx(5);
  647. color: var(--color-slate-800);
  648. box-sizing: border-box;
  649. }
  650. .expand-cell {
  651. padding: 0 !important;
  652. }
  653. }
  654. }
  655. .crm-chevron-down {
  656. transform: rotate(-90deg);
  657. cursor: pointer;
  658. }
  659. .mobile-table {
  660. :deep(.uni-table) {
  661. min-width: 0 !important;
  662. }
  663. }
  664. .detail-btn-mobile {
  665. background-color: #f0f0f0;
  666. color: #333;
  667. }
  668. /* 分页样式 */
  669. .pagination-container {
  670. margin: px2rpx(20) px2rpx(20);
  671. display: flex;
  672. align-items: center;
  673. justify-content: flex-end;
  674. gap: px2rpx(20);
  675. .pagination {
  676. display: flex;
  677. align-items: center;
  678. padding: px2rpx(10);
  679. }
  680. .page-item {
  681. display: flex;
  682. align-items: center;
  683. padding: 0 px2rpx(12);
  684. height: px2rpx(30);
  685. background-color: var(--color-white);
  686. border: 1px solid #dcdfe6;
  687. border-radius: px2rpx(8);
  688. font-size: px2rpx(16);
  689. color: #606266;
  690. cursor: pointer;
  691. transition: all 0.3s;
  692. margin: 0 px2rpx(6);
  693. }
  694. .page-item.prev {
  695. margin-right: px2rpx(10);
  696. }
  697. .page-item.next {
  698. margin-left: px2rpx(10);
  699. }
  700. .page-item:not(.disabled):hover {
  701. color: #007aff;
  702. border-color: #007aff;
  703. }
  704. .page-item.disabled {
  705. opacity: 0.5;
  706. cursor: not-allowed;
  707. pointer-events: none;
  708. }
  709. .page-numbers {
  710. display: flex;
  711. gap: px2rpx(8);
  712. }
  713. .page-number {
  714. display: flex;
  715. align-items: center;
  716. justify-content: center;
  717. min-width: px2rpx(40);
  718. height: px2rpx(30);
  719. padding: 0 px2rpx(4);
  720. background-color: var(--color-white);
  721. border-radius: px2rpx(8);
  722. font-size: px2rpx(16);
  723. color: #606266;
  724. cursor: pointer;
  725. transition: all 0.3s;
  726. }
  727. .page-number:hover {
  728. color: #007aff;
  729. border-color: #007aff;
  730. }
  731. .page-number.active {
  732. background-color: #007aff;
  733. border-color: #007aff;
  734. color: #fff;
  735. }
  736. .pagination-info {
  737. display: flex;
  738. align-items: center;
  739. gap: px2rpx(30);
  740. font-size: px2rpx(16);
  741. color: #909399;
  742. }
  743. .page-size-select {
  744. display: flex;
  745. align-items: center;
  746. gap: px2rpx(10);
  747. }
  748. .page-size-value {
  749. padding: px2rpx(6) px2rpx(20);
  750. border: 1px solid #dcdfe6;
  751. border-radius: px2rpx(8);
  752. color: #606266;
  753. }
  754. }
  755. @media screen and (max-width: 768px) {
  756. .pagination {
  757. flex-wrap: wrap;
  758. justify-content: center;
  759. }
  760. .page-item {
  761. padding: 0 px2rpx(16);
  762. }
  763. .page-number {
  764. min-width: px2rpx(60);
  765. }
  766. .pagination-info {
  767. flex-direction: column;
  768. gap: px2rpx(10);
  769. }
  770. }
  771. </style>