微信小程序:动态表格实现,表头单元格数据完全从data中获取,宽度自定义,自定义文本框,行勾选,样式效果,横向滚动表格(解决背景色不足的问题)等

一、样式效果

二、代码

1、wxml

html 复制代码
<view class="line flex flex-center">
  <view class="none" wx:if="{{info.length == 0}}">暂无料号</view>
  <view wx:else class="table-container">
    <!-- 动态生成表头 -->
    <view class="table-header-wrapper">
      <view class="table-header flex">
        <view wx:for="{{tableColumns}}" wx:key="key" class="th" style="flex: 0 0 {{item.width}}; min-width: {{item.width}};">
          {{item.title}}
        </view>
      </view>
    </view>
    <!-- 表格数据行 -->
    <view class="table-body-wrapper">
      <view class="table-body">
        <view class="table-row flex {{rowData.checked ? 'row-selected' : ''}}" wx:for="{{info}}" wx:key="index" wx:for-item="rowData" data-id="{{rowData.id}}">
          <view wx:for="{{tableColumns}}" wx:key="key" wx:for-item="colConfig" class="td" style="flex: 0 0 {{colConfig.width}}; min-width: {{colConfig.width}};">
            <block wx:if="{{colConfig.key === 'checked'}}">
              <checkbox value="{{rowData.id}}" checked="{{rowData.checked}}" bindtap="handleSelectItem" data-id="{{rowData.id}}" />
            </block>
            <block wx:elif="{{colConfig.editable}}">
              <!-- 新增判断条件 -->
              <input type="number" value="{{rowData[colConfig.key]}}" placeholder="请输入" bindinput="handleInputChange" data-id="{{rowData.id}}" data-index="{{index}}" data-key="{{colConfig.key}}" />
            </block>
            <block wx:else>
              {{rowData[colConfig.key] || '--'}}
            </block>
          </view>
        </view>
      </view>
    </view>
  </view>
</view>
<view class="btn" bindtap="submitCheckedItems">
  提交
</view>

2、wxss

css 复制代码
/* 行关系 */
.line {
  width: 100%;
  margin-top: 50px;
}

.none {
  color: rgb(153, 153, 153);
}

/* 表格容器 */
.table-container {
  width: 100%;
  font-size: 12px;
  overflow-x: auto;
}

/* 表头包装器 */
.table-header-wrapper {
  width: max-content;
  min-width: 100%;
}

/* 表头样式 */
.table-header {
  border-bottom: 1px solid #eaeaea;
  background-color: #b9e3fc;
}

/* 表格内容包装器 */
.table-body-wrapper {
  width: fit-content;
}

/* 表头和表格行通用样式 */
.table-header,
.table-row {
  display: flex;
  align-items: center;
}

/* 表头单元格和表格单元格 */
.th,
.td {
  padding: 8px 0;
  text-align: center;
  word-break: break-word;
  display: flex;
  justify-content: center;
  align-items: center;
}

/* 表格行样式 */
.table-row {
  min-width: 100%;
}

/* 隔行变色效果(可选) */
.table-row:nth-child(even) {
  background-color: #ececec;
}

/* 复选框样式调整 */
.td checkbox {
  transform: scale(0.7);
}

/* 选中行样式 */
.row-selected {
  background-color: #ebf9ff !important;
  position: relative;
}

.td input {
  width: 80%;
  text-align: center;
  background-color: rgb(255, 255, 255);
  border: 1px solid rgb(165, 165, 165);
}

/* 按钮 */
.btn {
  width: 100%;
  background-color: #4cc46b;
  position: absolute;
  margin-top: 100px;
  display: flex;
  justify-content: center;
  align-items: center;
  height: 30px;
  color: #fff;
}

3、js

javascript 复制代码
// pages/test3/index.js
Page({
  data: {
    // 动态表头配置
    tableColumns: [{
        key: 'checked',
        title: '选择',
        width: '80rpx',
        maxWidth: '100rpx'
      },
      {
        key: 'Qty2',
        title: '输入数量',
        width: '150rpx',
        maxWidth: '150rpx',
        editable: true
      },
      {
        key: 'Qty1',
        title: '总数量',
        width: '150rpx',
        maxWidth: '150rpx'
      },
      {
        key: 'code',
        title: '唯一码',
        width: '150rpx',
        maxWidth: '200rpx'
      },
      {
        key: 'name',
        title: '名称',
        width: '180rpx',
        maxWidth: '250rpx'
      },
      {
        key: 'type',
        title: '类型',
        width: '150rpx',
        maxWidth: '300rpx'
      },
    ],
    // 表格数据
    info: [{
        id: 1,
        checked: false,
        code: "111",
        name: "名称1",
        type: "类型1",
        Qty1: 444,
        Qty2: 0,
      },
      {
        id: 2,
        checked: false,
        code: "222",
        name: "名称2",
        type: "类型1",
        Qty1: 497,
        Qty2: 0,
      },
      {
        id: 3,
        checked: false,
        code: "333",
        name: "名称3",
        type: "类型2",
        Qty1: 234,
        Qty2: 0,
      },
      {
        id: 4,
        checked: false,
        code: "444",
        name: "名称4",
        type: "类型1",
        Qty1: 5,
        Qty2: 0,
      }
    ]
  },
  // 复选框选择事件
  handleSelectItem: function (e) {
    const id = e.currentTarget.dataset.id;
    const info = this.data.info.map(item => {
      if (item.id == id) {
        return {
          ...item,
          checked: !item.checked
        };
      }
      return item;
    });
    this.setData({
      info
    });
  },
  // 处理输入框变化
  handleInputChange: function (e) {
    const {
      id,
      key
    } = e.currentTarget.dataset;
    const value = e.detail.value;
    const info = this.data.info.map(item => {
      if (item.id == id) {
        return {
          ...item,
          [key]: value
        };
      }
      return item;
    });
    this.setData({
      info
    });
  },
  // 提交方法 - 获取勾选的数据
  submitCheckedItems: function () {
    const checkedItems = this.data.info.filter(item => item.checked);
    if (checkedItems.length === 0) {
      wx.showToast({
        title: '请至少选择一项',
        icon: 'none'
      });
      return;
    }
    // 这里可以发送到服务器或处理数据
    console.log('提交的数据:', checkedItems);
  }
})
相关推荐
陈拉斯21 小时前
无后端的人格测试小程序:当「数据不出设备」变成架构约束,我学到了什么
微信小程序
李纲明1 天前
微信小程序开了 Skyline 后样式全乱、滚动对不上甚至部分组件不能用?Skyline 怎么接才稳
javascript·微信小程序·小程序·devdiy
李纲明1 天前
微信小程序等位大桌被小桌占掉或过号回来就闹:先核桌型分队与过号容忍次数,再查门口屏与推送同源
微信小程序·devdiy·等位排队·桌型分队·过号重排·门口屏·推送同源
李纲明2 天前
微信小程序团课临开课才取消或空位没人补:先核取消时限与自动释放,再查候补确认与预扣回滚
微信小程序·devdiy·团课预约·取消时限·空位释放·候补确认·预扣回滚
用户034187671382 天前
用秒哒做了个拍照识餐小程序,上线两周踩的 4 个坑
微信小程序
李纲明2 天前
微信小程序租赁归还说本来就坏或配件对不上:先核借还点验与随单存证,再查差异计价公示
微信小程序·devdiy·租赁点验·借还拍照·随单存证·差异计价·押金释放
李纲明3 天前
微信小程序次卡跨店对不上账或前台随手改余次:先核流水只增不改与统一日切,再查冲正权限与总部只读
微信小程序·devdiy·核销流水·跨店次卡·日切对账·冲正权限·总部只读
李纲明3 天前
微信小程序维修拆机后口头加价或未确认就开工:先核验机快照与报价确认,再查补充报价与取机核验
微信小程序·devdiy·维修验机·报价确认·报价快照·补充报价·取机核验
wx1qq1com3 天前
一桩带几台车:共享租赁桩位配比、取电与结算闭环设计
物联网·微信小程序·小程序
李纲明3 天前
微信小程序场地长租月底对不上或口头占场撞场:先核合约预约与核销流水出账,再查附加费与转租归属
微信小程序·devdiy·场地长租·月结账单·核销流水·附加费·转租归属