当前位置:   article > 正文

小程序跨行跨列多列复杂表格实现

小程序table跨行

今天来实现个跨行跨列多列表格。

如图,这是个列数不确定,有的单元格还要跨行跨列的复杂表格。

这里暂时最多支持4列,列数再多就放不下了。

实现原理

实现原理比较简单,通过多个嵌套的循环将数据取出。

上面的例子中,最外层一共有4行:基础工资,加班工资,岗位工资,合计。第一层数据的 name 展示为第一列,如果每组数据有 children,取出 children 展示为第二列… 如果 children 长度为0,则直接显示工资数额。

这样一层一层把数据剖开,就做到了上面的效果。

数据格式

模拟的数据如下,如果是最后一层 value 值为工资数额,否则值为 null。嵌套的数据在 children 中。

  1. // 模拟的数据
  2. export default {
  3. status: 200,
  4. code: "ok",
  5. data: [{
  6. id: 'table001',
  7. name: '基础工资',
  8. value: null,
  9. children: [{
  10. id: 'table0011',
  11. name: '基本工资',
  12. value: 3000.0,
  13. children: []
  14. },
  15. {
  16. id: 'table0012',
  17. name: '绩效工资',
  18. value: 1200.0,
  19. children: []
  20. },
  21. {
  22. id: 'table0013',
  23. name: '基本工作量',
  24. value: null,
  25. children: [{
  26. id: 'table00131',
  27. name: '课时工资',
  28. value: 800.0,
  29. children: []
  30. },
  31. {
  32. id: 'table00132',
  33. name: '超课时工资',
  34. value: 200.0,
  35. children: []
  36. },
  37. ]
  38. },
  39. ]
  40. },
  41. {
  42. id: 'table002',
  43. name: '加班工资',
  44. value: null,
  45. children: [{
  46. id: 'table0021',
  47. name: '工作日加班',
  48. value: 1000.0,
  49. children: []
  50. },
  51. {
  52. id: 'table0022',
  53. name: '周末加班',
  54. value: 600.0,
  55. children: []
  56. },
  57. ]
  58. },
  59. {
  60. id: 'table003',
  61. name: '岗位工资',
  62. value: 1800.0,
  63. children: [
  64. ]
  65. },
  66. {
  67. id: 'table004',
  68. name: '合计',
  69. value: 8600.0,
  70. children: []
  71. },
  72. ]
  73. }
  74. 复制代码

页面布局

wxml文件

  1. <view class='container'>
  2. <picker class='picker' mode='date' fields='month' bindchange='dateChange'>
  3. <view class='picker-content'>
  4. <image class='date-icon' src='../../assets/date_48.png'></image>
  5. <view class='date-text'>{{currentDate}}</view>
  6. </view>
  7. </picker>
  8. <view class='title-wrapper'>
  9. <text class='title'>{{username + " 老师 " + currentDate + " 月工资表"}}</text>
  10. <text class='yuan'>单位:元</text>
  11. </view>
  12. <view class='table-wrapper'>
  13. <view class='nodata' wx:if='{{list.length === 0}}'>本月暂无工资数据</view>
  14. <view class='row1' wx:if='{{list.length > 0}}' wx:for='{{list}}' wx:key='{{item.id}}'>
  15. <text class='text'>{{item.name}}</text>
  16. <view class='column2-wrapper'>
  17. <view class='column-value' wx:if='{{item.value}}'>{{item.value}}</view>
  18. <view class='column2' wx:if='{{item.children.length > 0}}' wx:for='{{item.children}}' wx:for-item='item2' wx:key='{{item2.id}}'>
  19. <text class='text'>{{item2.name}}</text>
  20. <view class='column3-wrapper'>
  21. <view class='column-value' wx:if='{{item2.value}}'>{{item2.value}}</view>
  22. <view class='column3' wx:if='{{item2.children.length > 0}}' wx:for='{{item2.children}}' wx:for-item='item3' wx:key='{{item3.id}}'>
  23. <text class='text'>{{item3.name}}</text>
  24. <view class='column4-wrapper'>
  25. <view class='column-value' wx:if='{{item3.value}}'>{{item3.value}}</view>
  26. </view>
  27. </view>
  28. </view>
  29. </view>
  30. </view>
  31. </view>
  32. </view>
  33. </view>
  34. 复制代码

wxss 文件

  1. .container {
  2. width: 100%;
  3. display: flex;
  4. flex-direction: column;
  5. box-sizing: border-box;
  6. background: white;
  7. }
  8. .picker {
  9. width: 100%;
  10. }
  11. .date-text {
  12. font-size: 32rpx;
  13. padding: 20rpx 10rpx;
  14. text-align: center;
  15. }
  16. .title-wrapper {
  17. display: flex;
  18. width: 100%;
  19. justify-content: center;
  20. align-items: center;
  21. padding: 20rpx;
  22. box-sizing: border-box;
  23. }
  24. .title {
  25. flex: 1;
  26. font-size: 34rpx;
  27. text-align: center;
  28. font-weight: 700;
  29. color: #09bb07;
  30. }
  31. .yuan {
  32. font-size: 24rpx;
  33. color: #09bb07;
  34. }
  35. .table-wrapper {
  36. width: 100%;
  37. display: flex;
  38. flex-direction: column;
  39. border-top: 1rpx solid #DCDFE6;
  40. }
  41. .row1 {
  42. width: 100%;
  43. display: flex;
  44. flex-direction: row;
  45. align-items: center;
  46. font-size: 32rpx;
  47. box-sizing: border-box;
  48. border-bottom: 1rpx solid #DCDFE6;
  49. }
  50. .text {
  51. flex: 1;
  52. padding: 10rpx;
  53. line-height: 60rpx;
  54. height: 60rpx;
  55. }
  56. .column2-wrapper {
  57. display: flex;
  58. flex-direction: column;
  59. flex: 3;
  60. justify-content: center;
  61. border-left: 1rpx solid #DCDFE6;
  62. }
  63. .column2 {
  64. display: flex;
  65. flex: 1;
  66. align-items: center;
  67. border-bottom: 1rpx solid #DCDFE6;
  68. }
  69. .column2:last-child{
  70. border-bottom: none;
  71. }
  72. .column3-wrapper {
  73. display: flex;
  74. flex-direction: column;
  75. flex: 2;
  76. justify-content: center;
  77. border-left: 1rpx solid #DCDFE6;
  78. }
  79. .column3 {
  80. display: flex;
  81. flex: 1;
  82. align-items: center;
  83. border-bottom: 1rpx solid #DCDFE6;
  84. }
  85. .column3:last-child{
  86. border-bottom: none;
  87. }
  88. .column-value{
  89. display: flex;
  90. align-self: flex-end;
  91. margin-right: 10rpx;
  92. padding: 10rpx;
  93. line-height: 60rpx;
  94. height: 60rpx;
  95. }
  96. .column4-wrapper{
  97. display: flex;
  98. flex-direction: column;
  99. flex: 1;
  100. justify-content: center;
  101. border-left: 1rpx solid #DCDFE6;
  102. }
  103. .picker-content{
  104. display: flex;
  105. width: 100%;
  106. justify-content: center;
  107. align-items: center;
  108. border-bottom: 1rpx solid rgba(7, 17, 27, 0.1);
  109. }
  110. .date-icon{
  111. width: 80rpx;
  112. height: 80rpx;
  113. }
  114. .nodata{
  115. width: 100%;
  116. text-align: center;
  117. font-size: 32rpx;
  118. color: #666;
  119. padding: 20rpx;
  120. }
  121. 复制代码

js 文件

  1. import MockData from './mockdata.js'
  2. import {
  3. formatTime
  4. } from '../../utils/util.js'
  5. Page({
  6. data: {
  7. currentDate: '',
  8. username: '张三',
  9. list: ''
  10. },
  11. onLoad: function () {
  12. wx.setNavigationBarTitle({
  13. title: '工资查询',
  14. })
  15. //设置当前年月
  16. this.setCurrentDate()
  17. this._getSalary()
  18. },
  19. setCurrentDate(){
  20. //获取当前年月
  21. let date = new Date()
  22. let fmtDate = formatTime(date).substring(0, 7)
  23. this.setData({
  24. currentDate: fmtDate,
  25. })
  26. console.log(fmtDate)
  27. },
  28. //日期变化回调
  29. dateChange(res) {
  30. console.log(res)
  31. let value = res.detail.value
  32. this.setData({
  33. currentDate: value
  34. })
  35. //请求数据
  36. this._getSalary()
  37. },
  38. //模拟请求数据
  39. _getSalary(){
  40. this.setData({
  41. list: MockData.data
  42. })
  43. }
  44. })
  45. 复制代码

逻辑很简单,主要是布局稍微复杂些,理清楚了也挺好理解。

源码地址: https://github.com/cachecats/wechat-table

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小蓝xlanll/article/detail/80947
推荐阅读
相关标签
  

闽ICP备14008679号