cwg-tabel.vue 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659
  1. <template>
  2. <view class="table-container">
  3. <!-- 表格 -->
  4. <uni-table :type="selectionType" :border="false" @selection-change="handleSelectionChange">
  5. <uni-tr class="tabel-header">
  6. <uni-th v-for="column in columns" :key="column.prop" :align="column.align || 'center'"
  7. :width="column.width || '50'">
  8. {{ column.label }}
  9. </uni-th>
  10. <uni-th v-if="showOperation" align="center" width="200">操作</uni-th>
  11. </uni-tr>
  12. <uni-tr v-for="(row, rowIndex) in tableData" :key="rowIndex">
  13. <uni-td v-for="column in columns" :key="column.prop" :align="column.align || 'center'">
  14. <!-- 自定义渲染插槽 -->
  15. <slot v-if="column.slot" :name="column.slot" :row="row" :column="column" :index="rowIndex">
  16. {{ row[column.prop] }}
  17. </slot>
  18. <!-- 标签类型渲染 -->
  19. <uni-tag v-else-if="column.type === 'tag'" :text="formatTagText(row[column.prop], column)"
  20. :type="formatTagType(row[column.prop], column)" size="small" />
  21. <!-- 文件类型渲染 -->
  22. <view v-else-if="column.type === 'file'">
  23. <cwg-file :path="row[column.prop]" />
  24. </view>
  25. <!-- 默认文本渲染 -->
  26. <template v-else>{{ formatCellValue(row[column.prop], column, row) }}</template>
  27. </uni-td>
  28. <!-- 操作按钮 -->
  29. <uni-td v-if="showOperation" align="center">
  30. <view class="operation-btns">
  31. <template v-for="(action, index) in operationActions" :key="index">
  32. <button v-if="showAction(action, row)" class="btn" size="mini"
  33. :type="action.type || 'default'" @click="handleAction(action, row)">
  34. <cwg-icon :name="action.icon" :size="16" color="#fff" v-if="action.icon" />
  35. {{ action.text }}
  36. </button>
  37. </template>
  38. </view>
  39. </uni-td>
  40. </uni-tr>
  41. <!-- 空数据提示 -->
  42. <uni-tr v-if="tableData.length === 0">
  43. <uni-td :colspan="columnSpan" align="center">
  44. <view class="empty-data">
  45. <uni-icons type="info" size="30" color="#999"></uni-icons>
  46. <text class="empty-text">暂无数据</text>
  47. </view>
  48. </uni-td>
  49. </uni-tr>
  50. </uni-table>
  51. <!-- 分页组件 -->
  52. <view class="pagination-container" v-if="showPagination">
  53. <view class="pagination-info">
  54. <text>共 {{ pagination.total }} 条记录</text>
  55. <view v-if="showPageSize" class="page-size-select">
  56. <text>每页显示</text>
  57. <picker @change="handlePageSizeChange" :value="pageSizeIndex" :range="pageSizes">
  58. <view class="page-size-value">{{ pagination.pageSize }}条/页</view>
  59. </picker>
  60. </view>
  61. </view>
  62. <view class="pagination">
  63. <view class="page-item prev" :class="{ disabled: pagination.current === 1 }"
  64. @click="handlePageChange('prev')">
  65. <uni-icons type="arrowleft" size="16" color="#666"></uni-icons>
  66. <text>上一页</text>
  67. </view>
  68. <view class="page-numbers">
  69. <view v-for="page in visiblePages" :key="page" class="page-number"
  70. :class="{ active: pagination.current === page }" @click="handlePageChange(page)">
  71. {{ page }}
  72. </view>
  73. </view>
  74. <view class="page-item next" :class="{ disabled: pagination.current === pagination.pages }"
  75. @click="handlePageChange('next')">
  76. <text>下一页</text>
  77. <uni-icons type="arrowright" size="16" color="#666"></uni-icons>
  78. </view>
  79. </view>
  80. </view>
  81. </view>
  82. </template>
  83. <script setup>
  84. import { ref, computed, watch, onMounted } from 'vue'
  85. const props = defineProps({
  86. // 表格列配置
  87. columns: {
  88. type: Array,
  89. required: true,
  90. default: () => []
  91. },
  92. // API 请求函数
  93. api: {
  94. type: Function,
  95. required: true
  96. },
  97. // 查询参数
  98. queryParams: {
  99. type: Object,
  100. default: () => ({})
  101. },
  102. // 是否立即加载
  103. immediate: {
  104. type: Boolean,
  105. default: true
  106. },
  107. // 选择类型:'selection' | null
  108. selectionType: {
  109. type: String,
  110. default: null
  111. },
  112. // 是否显示操作列
  113. showOperation: {
  114. type: Boolean,
  115. default: false
  116. },
  117. // 操作按钮配置
  118. operationActions: {
  119. type: Array,
  120. default: () => [
  121. { text: '编辑', type: 'primary', action: 'edit', icon: 'crm-puls' },
  122. { text: '删除', type: 'warn', action: 'delete', icon: 'crm-delete' },
  123. { text: '查看', type: 'default', action: 'view', icon: 'view' }
  124. ]
  125. },
  126. // 是否显示底部操作栏
  127. showBottomActions: {
  128. type: Boolean,
  129. default: false
  130. },
  131. // 底部操作按钮
  132. bottomActions: {
  133. type: Array,
  134. default: () => []
  135. },
  136. // 是否显示每页条数选择
  137. showPageSize: {
  138. type: Boolean,
  139. default: true
  140. },
  141. // 是否显示分页
  142. showPagination: {
  143. type: Boolean,
  144. default: true
  145. },
  146. // 每页条数选项
  147. pageSizes: {
  148. type: Array,
  149. default: () => [10, 20, 30, 50, 100]
  150. },
  151. // 默认每页条数
  152. defaultPageSize: {
  153. type: Number,
  154. default: 10
  155. }
  156. })
  157. const emit = defineEmits([
  158. 'selection-change',
  159. 'action-click',
  160. 'bottom-action-click',
  161. 'page-change',
  162. 'load-success',
  163. 'load-error'
  164. ])
  165. // 表格数据
  166. const tableData = ref([])
  167. const selectedItems = ref([])
  168. // 分页参数
  169. const pagination = ref({
  170. current: 1,
  171. pageSize: props.defaultPageSize,
  172. total: 0,
  173. pages: 0
  174. })
  175. // 加载状态
  176. const loading = ref(false)
  177. // 计算列跨度
  178. const columnSpan = computed(() => {
  179. let span = props.columns.length
  180. if (props.showOperation) span += 1
  181. return span
  182. })
  183. // 计算每页条数索引
  184. const pageSizeIndex = computed(() => {
  185. return props.pageSizes.indexOf(pagination.value.pageSize)
  186. })
  187. // 计算显示的页码
  188. const visiblePages = computed(() => {
  189. const maxVisible = 5
  190. const half = Math.floor(maxVisible / 2)
  191. let start = Math.max(1, pagination.value.current - half)
  192. let end = Math.min(pagination.value.pages, start + maxVisible - 1)
  193. if (end - start + 1 < maxVisible) {
  194. start = Math.max(1, end - maxVisible + 1)
  195. }
  196. return Array.from({ length: end - start + 1 }, (_, i) => start + i)
  197. })
  198. // 监听查询参数变化
  199. watch(() => props.queryParams, () => {
  200. refreshTable()
  201. }, { deep: true })
  202. // 加载数据
  203. const loadData = async () => {
  204. if (loading.value) return
  205. loading.value = true
  206. try {
  207. const params = {
  208. page: {
  209. current: pagination.value.current,
  210. size: pagination.value.pageSize
  211. },
  212. ...props.queryParams
  213. }
  214. const res = await props.api(params)
  215. // 处理不同的API返回格式
  216. if (res.code === 200 || res.code === 0) {
  217. const data = res.data || res
  218. // 支持不同的分页数据结构
  219. if (Array.isArray(data)) {
  220. tableData.value = data
  221. pagination.value.total = data.length
  222. pagination.value.pages = 1
  223. } else if (data.list || data.records) {
  224. tableData.value = data.list || data.records
  225. pagination.value.total = data.total || data.list?.length || 0
  226. pagination.value.pages = data.pages || Math.ceil(pagination.value.total / pagination.value.pageSize)
  227. } else {
  228. tableData.value = []
  229. }
  230. emit('load-success', res)
  231. } else {
  232. throw new Error(res.message || '加载失败')
  233. }
  234. } catch (error) {
  235. console.error('表格数据加载失败:', error)
  236. uni.showToast({
  237. title: error.message || '加载失败',
  238. icon: 'none'
  239. })
  240. emit('load-error', error)
  241. } finally {
  242. loading.value = false
  243. }
  244. }
  245. // 刷新表格
  246. const refreshTable = () => {
  247. pagination.value.current = 1
  248. loadData()
  249. }
  250. // 重新加载
  251. const reload = () => {
  252. loadData()
  253. }
  254. // 分页变化
  255. const handlePageChange = (page) => {
  256. if (page === 'prev') {
  257. if (pagination.value.current > 1) {
  258. pagination.value.current--
  259. } else {
  260. return
  261. }
  262. } else if (page === 'next') {
  263. if (pagination.value.current < pagination.value.pages) {
  264. pagination.value.current++
  265. } else {
  266. return
  267. }
  268. } else {
  269. pagination.value.current = page
  270. }
  271. loadData()
  272. emit('page-change', pagination.value)
  273. }
  274. // 每页条数变化
  275. const handlePageSizeChange = (e) => {
  276. const size = props.pageSizes[e.detail.value]
  277. pagination.value.pageSize = size
  278. pagination.value.current = 1
  279. loadData()
  280. }
  281. // 选择变化
  282. const handleSelectionChange = (e) => {
  283. selectedItems.value = e.detail.value
  284. emit('selection-change', selectedItems.value)
  285. }
  286. // 操作按钮点击
  287. const handleAction = (action, row) => {
  288. emit('action-click', { action: action.action, row, original: action })
  289. }
  290. // 底部操作点击
  291. const handleBottomAction = (action) => {
  292. emit('bottom-action-click', action)
  293. }
  294. // 判断是否显示操作按钮
  295. const showAction = (action, row) => {
  296. if (action.show) {
  297. return action.show(row)
  298. }
  299. return true
  300. }
  301. // 格式化单元格值
  302. const formatCellValue = (value, column, row) => {
  303. if (column.formatter) {
  304. return column.formatter({ value, row })
  305. }
  306. if (column.type === 'date' && value) {
  307. return formatDate(value, column.dateFormat || 'YYYY-MM-DD HH:mm:ss')
  308. }
  309. if (value === null || value === undefined) {
  310. return '-'
  311. }
  312. return value
  313. }
  314. // 格式化标签文本
  315. const formatTagText = (value, column) => {
  316. if (column.tagMap && column.tagMap[value]) {
  317. return column.tagMap[value]
  318. }
  319. return value || '-'
  320. }
  321. // 格式化标签类型
  322. const formatTagType = (value, column) => {
  323. if (column.tagTypeMap && column.tagTypeMap[value]) {
  324. return column.tagTypeMap[value]
  325. }
  326. return 'default'
  327. }
  328. // 格式化日期
  329. const formatDate = (date, format) => {
  330. if (!date) return '-'
  331. const d = new Date(date)
  332. const year = d.getFullYear()
  333. const month = String(d.getMonth() + 1).padStart(2, '0')
  334. const day = String(d.getDate()).padStart(2, '0')
  335. const hour = String(d.getHours()).padStart(2, '0')
  336. const minute = String(d.getMinutes()).padStart(2, '0')
  337. const second = String(d.getSeconds()).padStart(2, '0')
  338. return format
  339. .replace('YYYY', year)
  340. .replace('MM', month)
  341. .replace('DD', day)
  342. .replace('HH', hour)
  343. .replace('mm', minute)
  344. .replace('ss', second)
  345. }
  346. // 预览图片
  347. const handlePreviewImage = (url) => {
  348. uni.previewImage({
  349. urls: [url]
  350. })
  351. }
  352. // 获取选中项
  353. const getSelectedItems = () => {
  354. return selectedItems.value
  355. }
  356. // 清空选中
  357. const clearSelection = () => {
  358. selectedItems.value = []
  359. }
  360. // 暴露方法给父组件
  361. defineExpose({
  362. refreshTable,
  363. reload,
  364. getSelectedItems,
  365. clearSelection,
  366. loadData,
  367. tableData,
  368. pagination
  369. })
  370. // 初始化加载
  371. onMounted(() => {
  372. if (props.immediate) {
  373. loadData()
  374. }
  375. })
  376. </script>
  377. <style scoped lang="scss">
  378. @import "@/uni.scss";
  379. .table-container {
  380. max-height: calc(100vh - 209px);
  381. overflow: hidden;
  382. color: var(--color-slate-800);
  383. :deep(.uni-table-scroll) {
  384. width: 100%;
  385. max-height: calc(100vh - 375px);
  386. overflow: auto;
  387. }
  388. :depp(.table--border) {
  389. border: none !important;
  390. }
  391. :deep(.uni-table) {
  392. /* 固定表头样式 */
  393. .uni-table-th {
  394. position: sticky;
  395. top: 0;
  396. z-index: 100;
  397. background-color: var(--color-slate-200) !important;
  398. padding: px2rpx(12);
  399. color: var(--color-slate-800);
  400. font-weight: 600;
  401. &::after {
  402. display: none;
  403. }
  404. }
  405. .tr-table--border,
  406. .table--border {
  407. border: none !important;
  408. }
  409. .uni-table-tr {
  410. border-bottom: 5px solid var(--color-slate-200) !important;
  411. }
  412. .uni-table-th,
  413. .uni-table-td {
  414. padding: px2rpx(12) px2rpx(20);
  415. color: var(--color-slate-800);
  416. }
  417. }
  418. }
  419. .table-image {
  420. width: px2rpx(30);
  421. height: px2rpx(30);
  422. border-radius: px2rpx(8);
  423. }
  424. .operation-btns {
  425. display: flex;
  426. gap: px2rpx(10);
  427. justify-content: center;
  428. flex-wrap: wrap;
  429. }
  430. .btn {
  431. margin: 0 px2rpx(4);
  432. font-size: px2rpx(16);
  433. padding: 0 px2rpx(20);
  434. height: px2rpx(36);
  435. border-radius: none;
  436. display: flex;
  437. align-items: center;
  438. justify-content: center;
  439. cursor: pointer;
  440. }
  441. .empty-data {
  442. display: flex;
  443. flex-direction: column;
  444. align-items: center;
  445. justify-content: center;
  446. padding: px2rpx(60) 0;
  447. }
  448. .empty-text {
  449. margin-top: px2rpx(20);
  450. font-size: px2rpx(16);
  451. color: #999;
  452. }
  453. .bottom-actions {
  454. margin: px2rpx(30) px2rpx(20);
  455. display: flex;
  456. justify-content: space-between;
  457. align-items: center;
  458. }
  459. .left-actions,
  460. .right-actions {
  461. display: flex;
  462. gap: px2rpx(20);
  463. }
  464. /* 分页样式 */
  465. .pagination-container {
  466. margin: px2rpx(20) px2rpx(20);
  467. display: flex;
  468. align-items: center;
  469. justify-content: flex-end;
  470. gap: px2rpx(20);
  471. .pagination {
  472. display: flex;
  473. align-items: center;
  474. padding: px2rpx(10);
  475. }
  476. .page-item {
  477. display: flex;
  478. align-items: center;
  479. padding: 0 px2rpx(12);
  480. height: px2rpx(30);
  481. background-color: #fff;
  482. border: 1px solid #dcdfe6;
  483. border-radius: px2rpx(8);
  484. font-size: px2rpx(16);
  485. color: #606266;
  486. cursor: pointer;
  487. transition: all 0.3s;
  488. margin: 0 px2rpx(6);
  489. }
  490. .page-item.prev {
  491. margin-right: px2rpx(10);
  492. }
  493. .page-item.next {
  494. margin-left: px2rpx(10);
  495. }
  496. .page-item:not(.disabled):hover {
  497. color: #007aff;
  498. border-color: #007aff;
  499. }
  500. .page-item.disabled {
  501. opacity: 0.5;
  502. cursor: not-allowed;
  503. pointer-events: none;
  504. }
  505. .page-numbers {
  506. display: flex;
  507. gap: px2rpx(8);
  508. }
  509. .page-number {
  510. display: flex;
  511. align-items: center;
  512. justify-content: center;
  513. min-width: px2rpx(40);
  514. height: px2rpx(30);
  515. padding: 0 px2rpx(4);
  516. background-color: #fff;
  517. border-radius: px2rpx(8);
  518. font-size: px2rpx(16);
  519. color: #606266;
  520. cursor: pointer;
  521. transition: all 0.3s;
  522. }
  523. .page-number:hover {
  524. color: #007aff;
  525. border-color: #007aff;
  526. }
  527. .page-number.active {
  528. background-color: #007aff;
  529. border-color: #007aff;
  530. color: #fff;
  531. }
  532. .pagination-info {
  533. display: flex;
  534. align-items: center;
  535. gap: px2rpx(30);
  536. font-size: px2rpx(16);
  537. color: #909399;
  538. }
  539. .page-size-select {
  540. display: flex;
  541. align-items: center;
  542. gap: px2rpx(10);
  543. }
  544. .page-size-value {
  545. padding: px2rpx(6) px2rpx(20);
  546. border: 1px solid #dcdfe6;
  547. border-radius: px2rpx(8);
  548. color: #606266;
  549. }
  550. }
  551. @media screen and (max-width: 768px) {
  552. .pagination {
  553. flex-wrap: wrap;
  554. justify-content: center;
  555. }
  556. .page-item {
  557. padding: 0 px2rpx(16);
  558. }
  559. .page-number {
  560. min-width: px2rpx(60);
  561. }
  562. .pagination-info {
  563. flex-direction: column;
  564. gap: px2rpx(10);
  565. }
  566. }
  567. </style>