支付宝小程序开发表格组件(可固定首行、首列)

文章描述了如何在支付宝小程序中使用表格组件,实现固定表头和滚动到底部触发滚动加载(上拉刷新)功能的代码示例,包括HTML结构、CSS样式和JavaScript逻辑。
摘要由CSDN通过智能技术生成

此表格目前仅实现了固定首行、首列功能,但滚动到底部会触发函数,可以通过该方法实现上拉加载的功能。

page.axml

<view class="com-table-temp-wrap">
  <view class="table__box">
    <scroll-view scroll-x scroll-left="{{scrollLeft}}" disable-scroll class="table__head_wrap">
      <view class="table__head" style="width: {{tableWidth}}rpx;">
        <view
          a:for="{{headList}}"
          a:for-index="attrIndex"
          a:for-item="attrItem"
          class="table__head__td"
          a:key="attrIndex"
          style="width: {{attrItem.width || 150}}rpx;"
        >
          <text class="table__head__td__text">{{attrItem.label}}</text>
        </view>
      </view>
    </scroll-view>
    <scroll-view a:if="{{ list.length }}" scroll-y class="table" onScrollToLower="handleScrollToLower">
      <scroll-view scroll-x="true" onScroll="scrollX">
        <view
          a:for="{{list}}"
          a:for-index="dataIndex"
          a:for-item="dataItem"
          class="table__row"
          a:key="dataIndex"
          style="width: {{tableWidth}}rpx;"
        >
          <text class="table__row__td {{headItem.className}}" style="width: {{headItem.width || 150}}rpx;" a:for="{{ headList }}" a:for-item="headItem">{{dataItem[headItem.prop]}}</text>
        </view>
      </scroll-view>
    </scroll-view>
    <ant-empty className="bg-white" a:else message="暂无数据" />
  </view>
</view>

page.acss

.com-table-temp-wrap{
  height: 100%;
}
.com-table-temp-wrap ::-webkit-scrollbar {
  width: 0;
  height: 0;
  color: transparent;
  
}

.com-table-temp-wrap .table__box {
  border: 1rpx solid #ecf1f8;
  border-bottom: 0;
  height: 100%;
  display: flex;
  flex-direction: column;
}
.com-table-temp-wrap .table__box .table__head_wrap{
  flex: 0 0 60rpx;
}
.com-table-temp-wrap .table__box .table{
  flex: 1 1 auto;
}

.com-table-temp-wrap .table__head {
  position: sticky;
  top: 0rpx;
  z-index: 100;
  /* width: 1380rpx; */
  height: 60rpx;
  line-height: 60rpx;
  display: flex;
  white-space: nowrap;
  font-size: 24rpx;
  color: #969696;
  font-weight: 400;
  border-bottom: 2rpx solid #ecf1f8;
  background-color: #ffffff;
}

.com-table-temp-wrap .table__head__td {
  position: relative;
  /* width: 150rpx; */
  display: flex;
  justify-content: center;
  align-items: center;
  background-color: #ffffff;
  box-sizing: border-box;
  white-space: nowrap;
  overflow: hidden;
  -o-text-overflow: ellipsis;
  text-overflow: ellipsis;
}

.com-table-temp-wrap .table__head__td:nth-child(1) {
  /* width: 180rpx; */
  position: sticky;
  z-index: 101;
  left: -1rpx;
}


.com-table-temp-wrap .table {
  display: block;
  position: relative;
  overflow: scroll;
  width: 100%;
  height: 260rpx;
}

.com-table-temp-wrap .table__row {
  /* width: 1380rpx; */
  height: 60rpx;
  display: flex;
  justify-content: flex-start;
  align-items: center;
  position: relative;
  white-space: nowrap;
  border-bottom: 2rpx solid #ecf1f8;
}

.com-table-temp-wrap .table__row__td {
  /* width: 150rpx; */
  height: 60rpx;
  line-height: 60rpx;
  text-align: center;
  position: relative;
  display: inline-block;
  box-sizing: border-box;
  font-size: 26rpx;
  background-color: #ffffff;
  white-space: nowrap;
  overflow: hidden;
  -o-text-overflow: ellipsis;
  text-overflow: ellipsis;
}

.com-table-temp-wrap .table__row__td:nth-child(1) {
  /* width: 180rpx; */
  position: sticky;
  z-index: 10;
  left: 0;
}

.com-table-temp-wrap .ant-empty{
  margin-top: 0;
}

page.js

Component({
  mixins: [],
  props: {
    tableColumn: {
      type: Array,
      default: () => ([])
    },
    tableData: {
      type: Array,
      default: () => ([])
    }
  },
  data: {
    scrollLeft: 0,
    headList: [],
    list: [],
    tableWidth: 0
  },
  didMount() {
    let {
      tableColumn,
      tableData
    } = this.props;

    let tableWidth = tableColumn.reduce((count, curVal) => count + Number(curVal.width || 150), 0);


    this.setData({
      tableWidth,
      headList: tableColumn || [],
      list: tableData || [],
    })
  },
  didUpdate() {},
  didUnmount() {},
  methods: {
    // 触底事件
    handleScrollToLower() {
      console.log('scroll-view触底事件在这里处理加载下一页数据')
    },
    //监听左右滚动
    scrollX(e) {
      if (e.detail.scrollLeft > 0) {
        this.setData({
          scrollLeft: e.detail.scrollLeft,
        });
      } else {
        this.setData({
          scrollLeft: 0,
        });
      }
    },
  },
});

page.json

{
  "component": true,
  "usingComponents": {
    "ant-empty": "antd-mini/es/Empty/index"
  }
}

效果图

支付宝小程序-表格组件

  • 7
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值