你期待的微信小程序表格组件来啦

你期待的微信小程序表格组件来啦

背景

在作H5开发中表格table是最多见的标签,如此好用的标签在微信小程序中却没有,无奈,叹息,绝望!!!html

奔着利国利猿的使命,我***开始了造轮之路。前端

功能

主要用于展现大量结构化数据。git

支持分页、自定义操做、长,宽表格滚动等功能。github

实现

前言json

  • 暴露必要属性例如表头,数据。
  • 支持外部样式控制表格。
  • 手机端的列表较轻暂时不须要暴露事件。

细节小程序

Component({
  properties: {
       list: {  // 表格数据
      type: Array,
      value: []
    },
    headers: {  //表头
      type: Array,
      value: []
    },
    hasBorder: {  // 表格中间边框
      type: String,
      value: "no"
    },
      height: { //table的高度
      type: Number || String,
      value: ''
    },
    width: {
      type: Number,
      value: 0
    },
    tdWidth: {
      type: Number,
      value: 35
    }
      
  }
})


复制代码
/*
s-class-header // 外部定义表头样式

s-class-row  // 外部定义行数据样式

*/


Component({
  externalClasses: ['s-class-header', 's-class-row']
})

复制代码
  • index.wxml文件经过嵌套循环动态渲染表格
<view class="table table-noborder">
    <view class="tr thead s-class-header">
      <view wx:for="{{headers}}" class="td td-{{hasBorder}}border">{{item.display}}</view>
    
    </view>
    <block wx:for-item='i' wx:for="{{list}}">
      <view class="tr s-class-row">
        <view wx:for-item='j' wx:for="{{headers}}" class="td td-{{hasBorder}}border">{{i[j['text']]}}</view>        
      </view>
    </block>
  </view>

复制代码
  • index.wxss 使用者对于很个性的样式能够自行修改源代码index.wxss文件
.table {
  border: 1px solid #ccc;
  font-size: 28rpx;
  background: #fff; 
  border-right:none;
}
.table-noborder {
  border-right:1rpx solid #ccc;
}
 
.tr{
  display: flex;
}
 
.td {
  text-align: center;
  border: 1px solid #ccc;
  display: inline-block;  
  border-bottom: none;
  flex: 1;
  padding: 16rpx;  
  border-left: none;
}
.td-noborder{  
  border-right: none;
}
 
.thead .td{
  border-top: none;
  padding: 16rpx;
  font-weight: bold;
}

复制代码

快速上手

.json文件引入组件微信小程序

{
  "usingComponents": {
    "s-table": "实际路径/table/index"
  }
}
 
复制代码

index.wxml 文件使用组件bash

<s-table hasBorder='no' width='200' tdWidth='100' height='200' s-class-header='my-class' s-class-row='my-class-row' headers='{{headers}}' list='{{row}}'></s-table>

复制代码

完整源代码和使用说明见github

github地址微信

更多更好的组件持续更新中...

关于咱们


快狗打车前端团队专一前端技术分享,按期推送高质量文章,欢迎关注点赞。 文章同步发布在公众号哟,想要第一时间获得最新的资讯,just scan it !app

image
相关文章
相关标签/搜索