当前位置: 首页 > news >正文

微信小程序 -- 原生封装table

文章目录

    • table.wxml
    • table.wxss
      • 注意
    • table.js
      • 注意
    • 结果
    • 数据结构

最近菜鸟做微信小程序的一个查询功能,需要展示excel里面的数据,但是菜鸟找了一圈,也没发现什么组件库有table,毕竟手机端好像确实不太适合做table!

菜鸟只能自己封装一个table了,当然菜鸟就是简单的封装,只是为自己留一个记录,免得后面要实现类似的东西去翻代码,对大佬们肯定是没啥帮助,但是对小白可能有点启发!

table.wxml

<view class="table-container">
  <scroll-view 
    scroll-x="true" 
    scroll-y="true" 
    class="table-body"
    style="max-height: {{height}}"
    enhanced="true"
    show-scrollbar="false"
  >
    <view class="table">
      <!-- 固定表头 -->
      <view class="table-header">
        <view class="tr">
          <block wx:for="{{columns}}" wx:key="index">
            <view
              class="th"
              style="min-width: {{item.width || '300rpx'}};width: {{item.width || '300rpx'}};"
            >
              {{ item.name }}
            </view>
          </block>
        </view>
      </view>

      <!-- 表格内容 -->
      <view class="table-rows">
        <block wx:for="{{listData}}" wx:key="item">
          <view class="tr borderbtm">
            <block wx:for="{{columns}}" wx:for-item="cell" wx:key="cell"> 
              <view
                class="td"
                style="min-width: {{cell.width || '300rpx'}};width: {{cell.width || '300rpx'}};"
              >
                {{item[cell.key]}}
              </view>
            </block>
          </view>
        </block>
      </view>
    </view>
  </scroll-view>
</view>

table.wxss

.table-container {
  width: 100%;
}

.table-body {
  width: 100%;
}

.table {
  width: fit-content;
  min-width: 100%;
}

.table-header {
  position: sticky; //position: sticky 是相对于最近的可滚动祖先元素,也就是 scroll-view.table-body 元素
  top: 0;
  z-index: 1;
  background: #fff;
}

.tr {
  display: flex;
  white-space: nowrap;
}

.th,
.td {
  display: inline-flex;
  align-items: center;
  box-sizing: border-box;
  padding: 20rpx;
  word-break: break-all;
  word-wrap: break-word;
  white-space: normal;
  user-select: text; /* 添加这行使文本可选择 */
}

.td {
  -webkit-user-select: text; /* 兼容性处理 */
  cursor: text; /* 显示文本选择光标 */
}

.th {
  font-weight: bold;
  background-color: #f8f8f8;
}

.borderbtm {
  border-bottom: 1px solid #eee;
}

注意

在这里插入图片描述

table.js

Component({
  properties: {
    listData: {
      type: Array,
      value: "",
    },
    columns: {
      type: Array,
      value: "",
    },
    height: {
      type: String,
      value: "",
    },
  },

  lifetimes: {
    attached() {
      const windowInfo = wx.getWindowInfo();
      const menuButtonInfo = wx.getMenuButtonBoundingClientRect();
      const headerHeight = menuButtonInfo.bottom + 40;

      const tableHeight = `${(windowInfo.windowHeight - headerHeight) / 2}px`;
      this.setData({
        height: tableHeight,
      });
    },
  },
});

注意

菜鸟这里是正好两个表要一上一下对半占屏幕,要是不是,可以不要计算这里的部分,直接靠传参设置!

结果

有图有真相

在这里插入图片描述

数据结构

表头

 b2_header_title: [
  { name: "客户批次号", key: "customerBatchNum" },
  { name: "项目批次号", key: "projectBatchNum" },
  { name: "执行合同编号", key: "executionContractNum", width: "500rpx" },
  { name: "执行合同名称", key: "executionContractName", width: "1000rpx" },
  ……
]

数据

[
  {
    "id": "2441568",
    "customerBatchNum": "SP2410303188",
    "projectBatchNum": "BC2024110238",
    "executionContractNum": "BN2401105NJ01S02N2",
    "executionContractName": "xxxxx",
    "uploadTime": "2025-04-01 14:31:18",
    ……
  },
  {
    "id": "2441570",
    "customerBatchNum": "SP2410303178",
    "projectBatchNum": "BC2024110239",
    "executionContractNum": "BN2401105NJ01S02N2",
    "executionContractName": "xxxxxxx",
    "uploadTime": "2025-04-01 14:32:18",
    ……
  }
]
http://www.dtcms.com/a/122354.html

相关文章:

  • UV安装与使用
  • asp.net core 项目发布到 IIS 服务器
  • 场外期权只适合上涨行情吗?
  • CSS语言的游戏AI
  • ResNet改进(18):添加 CPCA通道先验卷积注意力机制
  • 从个人博客到电商中台:EdgeOne Pages的MCP Server弹性架构×DeepSeek多场景模板实战解析
  • 1.VTK 使用CMakeLists
  • Linux 编程中的 I/O 复用
  • Element UI 设置 el-table-column 宽度 width 为百分比无效
  • React九案例中
  • 敏捷迭代实战经验分享
  • leetcode_数组 189. 轮转数组
  • Odrive0.5.1-FOC电机控制 arm_cos_f32.cpp arm_sin_f32.cpp代码实现(一)
  • 中科岩创基坑自动化监测解决方案
  • 【11】数据结构之基于线性表的查找算法
  • 【消息队列kafka_中间件】一、快速入门分布式消息队列
  • Android 中Intent 相关问题
  • STM32CubeMX-H7-12-IIC读写MPU6050模块(中)-MPU6050模块详解以及软件IIC驱动
  • Node.js是js语言在服务器编译运行的环境,什么是IP和域名
  • Python包管理工具uv简单使用
  • nginx或tengine服务器,配置HTTPS下使用WebSocket的线上环境实践!
  • 【详细】MySQL 8 安装解压即用 (包含MySQL 5 卸载)
  • Python从入门到精通全套视频教程免费
  • UniApp基于xe-upload实现文件上传组件
  • 12. git merge
  • 【LeetCode 题解】数据库:1321.餐馆营业额变化增长
  • 使用RabbitMQ实现异步秒杀
  • 网络安全公司推荐:F5荣膺IDC全球Web应用与API防护领导者
  • 游戏引擎学习第212天
  • TimeDART:结合扩散去噪与自回归建模的时间序列自监督学习新框架