cwg-tabel.vue 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662
  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. console.log(props.queryParams, 123123);
  201. refreshTable()
  202. }, { deep: true })
  203. // 加载数据
  204. const loadData = async () => {
  205. if (loading.value) return
  206. loading.value = true
  207. try {
  208. const params = {
  209. page: {
  210. current: pagination.value.current,
  211. size: pagination.value.pageSize
  212. },
  213. ...props.queryParams
  214. }
  215. const res = await props.api(params)
  216. // 处理不同的API返回格式
  217. if (res.code === 200 || res.code === 0) {
  218. const data = res.data || res
  219. // 支持不同的分页数据结构
  220. if (Array.isArray(data)) {
  221. tableData.value = data
  222. pagination.value.total = data.length
  223. pagination.value.pages = 1
  224. } else if (data.list || data.records) {
  225. tableData.value = data.list || data.records
  226. pagination.value.total = data.total || data.list?.length || 0
  227. pagination.value.pages = data.pages || Math.ceil(pagination.value.total / pagination.value.pageSize)
  228. } else {
  229. tableData.value = []
  230. }
  231. emit('load-success', res)
  232. } else {
  233. throw new Error(res.message || '加载失败')
  234. }
  235. } catch (error) {
  236. console.error('表格数据加载失败:', error)
  237. uni.showToast({
  238. title: error.message || '加载失败',
  239. icon: 'none'
  240. })
  241. emit('load-error', error)
  242. } finally {
  243. loading.value = false
  244. }
  245. }
  246. // 刷新表格
  247. const refreshTable = () => {
  248. pagination.value.current = 1
  249. loadData()
  250. }
  251. // 重新加载
  252. const reload = () => {
  253. loadData()
  254. }
  255. // 分页变化
  256. const handlePageChange = (page) => {
  257. if (page === 'prev') {
  258. if (pagination.value.current > 1) {
  259. pagination.value.current--
  260. } else {
  261. return
  262. }
  263. } else if (page === 'next') {
  264. if (pagination.value.current < pagination.value.pages) {
  265. pagination.value.current++
  266. } else {
  267. return
  268. }
  269. } else {
  270. pagination.value.current = page
  271. }
  272. loadData()
  273. emit('page-change', pagination.value)
  274. }
  275. // 每页条数变化
  276. const handlePageSizeChange = (e) => {
  277. const size = props.pageSizes[e.detail.value]
  278. pagination.value.pageSize = size
  279. pagination.value.current = 1
  280. loadData()
  281. }
  282. // 选择变化
  283. const handleSelectionChange = (e) => {
  284. selectedItems.value = e.detail.value
  285. emit('selection-change', selectedItems.value)
  286. }
  287. // 操作按钮点击
  288. const handleAction = (action, row) => {
  289. emit('action-click', { action: action.action, row, original: action })
  290. }
  291. // 底部操作点击
  292. const handleBottomAction = (action) => {
  293. emit('bottom-action-click', action)
  294. }
  295. // 判断是否显示操作按钮
  296. const showAction = (action, row) => {
  297. if (action.show) {
  298. return action.show(row)
  299. }
  300. return true
  301. }
  302. // 格式化单元格值
  303. const formatCellValue = (value, column, row) => {
  304. if (column.formatter) {
  305. return column.formatter({ value, row })
  306. }
  307. if (column.type === 'date' && value) {
  308. return formatDate(value, column.dateFormat || 'YYYY-MM-DD HH:mm:ss')
  309. }
  310. if (value === null || value === undefined) {
  311. return '-'
  312. }
  313. return value
  314. }
  315. // 格式化标签文本
  316. const formatTagText = (value, column) => {
  317. if (column.tagMap && column.tagMap[value]) {
  318. return column.tagMap[value]
  319. }
  320. return value || '-'
  321. }
  322. // 格式化标签类型
  323. const formatTagType = (value, column) => {
  324. if (column.tagTypeMap && column.tagTypeMap[value]) {
  325. return column.tagTypeMap[value]
  326. }
  327. return 'default'
  328. }
  329. // 格式化日期
  330. const formatDate = (date, format) => {
  331. if (!date) return '-'
  332. const d = new Date(date)
  333. const year = d.getFullYear()
  334. const month = String(d.getMonth() + 1).padStart(2, '0')
  335. const day = String(d.getDate()).padStart(2, '0')
  336. const hour = String(d.getHours()).padStart(2, '0')
  337. const minute = String(d.getMinutes()).padStart(2, '0')
  338. const second = String(d.getSeconds()).padStart(2, '0')
  339. return format
  340. .replace('YYYY', year)
  341. .replace('MM', month)
  342. .replace('DD', day)
  343. .replace('HH', hour)
  344. .replace('mm', minute)
  345. .replace('ss', second)
  346. }
  347. // 预览图片
  348. const handlePreviewImage = (url) => {
  349. uni.previewImage({
  350. urls: [url]
  351. })
  352. }
  353. // 获取选中项
  354. const getSelectedItems = () => {
  355. return selectedItems.value
  356. }
  357. // 清空选中
  358. const clearSelection = () => {
  359. selectedItems.value = []
  360. }
  361. // 暴露方法给父组件
  362. defineExpose({
  363. refreshTable,
  364. reload,
  365. getSelectedItems,
  366. clearSelection,
  367. loadData,
  368. tableData,
  369. pagination
  370. })
  371. // 初始化加载
  372. onMounted(() => {
  373. if (props.immediate) {
  374. loadData()
  375. }
  376. })
  377. </script>
  378. <style scoped lang="scss">
  379. @import "@/uni.scss";
  380. .table-container {
  381. max-height: calc(100vh - 209px);
  382. overflow: hidden;
  383. color: var(--color-slate-800);
  384. :deep(.uni-table-scroll) {
  385. width: 100%;
  386. max-height: calc(100vh - 375px);
  387. overflow: auto;
  388. }
  389. :depp(.table--border) {
  390. border: none !important;
  391. }
  392. :deep(.uni-table) {
  393. /* 固定表头样式 */
  394. .uni-table-th {
  395. position: sticky;
  396. top: 0;
  397. z-index: 100;
  398. background-color: var(--color-slate-200) !important;
  399. padding: px2rpx(12);
  400. color: var(--color-slate-800);
  401. font-weight: 600;
  402. &::after {
  403. display: none;
  404. }
  405. }
  406. .tr-table--border,
  407. .table--border {
  408. border: none !important;
  409. }
  410. .uni-table-tr {
  411. border-bottom: 5px solid var(--color-slate-200) !important;
  412. }
  413. .uni-table-th,
  414. .uni-table-td {
  415. padding: px2rpx(12) px2rpx(20);
  416. color: var(--color-slate-800);
  417. }
  418. }
  419. }
  420. .table-image {
  421. width: px2rpx(30);
  422. height: px2rpx(30);
  423. border-radius: px2rpx(8);
  424. }
  425. .operation-btns {
  426. display: flex;
  427. gap: px2rpx(10);
  428. justify-content: center;
  429. flex-wrap: wrap;
  430. }
  431. .btn {
  432. margin: 0 px2rpx(4);
  433. font-size: px2rpx(16);
  434. padding: 0 px2rpx(20);
  435. height: px2rpx(36);
  436. border-radius: none;
  437. display: flex;
  438. align-items: center;
  439. justify-content: center;
  440. cursor: pointer;
  441. }
  442. .empty-data {
  443. display: flex;
  444. flex-direction: column;
  445. align-items: center;
  446. justify-content: center;
  447. padding: px2rpx(60) 0;
  448. }
  449. .empty-text {
  450. margin-top: px2rpx(20);
  451. font-size: px2rpx(16);
  452. color: #999;
  453. }
  454. .bottom-actions {
  455. margin: px2rpx(30) px2rpx(20);
  456. display: flex;
  457. justify-content: space-between;
  458. align-items: center;
  459. }
  460. .left-actions,
  461. .right-actions {
  462. display: flex;
  463. gap: px2rpx(20);
  464. }
  465. /* 分页样式 */
  466. .pagination-container {
  467. margin: px2rpx(20) px2rpx(20);
  468. display: flex;
  469. align-items: center;
  470. justify-content: flex-end;
  471. gap: px2rpx(20);
  472. .pagination {
  473. display: flex;
  474. align-items: center;
  475. padding: px2rpx(10);
  476. }
  477. .page-item {
  478. display: flex;
  479. align-items: center;
  480. padding: 0 px2rpx(12);
  481. height: px2rpx(30);
  482. background-color: #fff;
  483. border: 1px solid #dcdfe6;
  484. border-radius: px2rpx(8);
  485. font-size: px2rpx(16);
  486. color: #606266;
  487. cursor: pointer;
  488. transition: all 0.3s;
  489. margin: 0 px2rpx(6);
  490. }
  491. .page-item.prev {
  492. margin-right: px2rpx(10);
  493. }
  494. .page-item.next {
  495. margin-left: px2rpx(10);
  496. }
  497. .page-item:not(.disabled):hover {
  498. color: #007aff;
  499. border-color: #007aff;
  500. }
  501. .page-item.disabled {
  502. opacity: 0.5;
  503. cursor: not-allowed;
  504. pointer-events: none;
  505. }
  506. .page-numbers {
  507. display: flex;
  508. gap: px2rpx(8);
  509. }
  510. .page-number {
  511. display: flex;
  512. align-items: center;
  513. justify-content: center;
  514. min-width: px2rpx(40);
  515. height: px2rpx(30);
  516. padding: 0 px2rpx(4);
  517. background-color: #fff;
  518. border-radius: px2rpx(8);
  519. font-size: px2rpx(16);
  520. color: #606266;
  521. cursor: pointer;
  522. transition: all 0.3s;
  523. }
  524. .page-number:hover {
  525. color: #007aff;
  526. border-color: #007aff;
  527. }
  528. .page-number.active {
  529. background-color: #007aff;
  530. border-color: #007aff;
  531. color: #fff;
  532. }
  533. .pagination-info {
  534. display: flex;
  535. align-items: center;
  536. gap: px2rpx(30);
  537. font-size: px2rpx(16);
  538. color: #909399;
  539. }
  540. .page-size-select {
  541. display: flex;
  542. align-items: center;
  543. gap: px2rpx(10);
  544. }
  545. .page-size-value {
  546. padding: px2rpx(6) px2rpx(20);
  547. border: 1px solid #dcdfe6;
  548. border-radius: px2rpx(8);
  549. color: #606266;
  550. }
  551. }
  552. @media screen and (max-width: 768px) {
  553. .pagination {
  554. flex-wrap: wrap;
  555. justify-content: center;
  556. }
  557. .page-item {
  558. padding: 0 px2rpx(16);
  559. }
  560. .page-number {
  561. min-width: px2rpx(60);
  562. }
  563. .pagination-info {
  564. flex-direction: column;
  565. gap: px2rpx(10);
  566. }
  567. }
  568. </style>