广告
返回顶部
首页 > 资讯 > 精选 >微信小程序中怎么实现一个固定表头、列表格组件
  • 617
分享到

微信小程序中怎么实现一个固定表头、列表格组件

2023-06-20 12:06:46 617人浏览 泡泡鱼
摘要

微信小程序中怎么实现一个固定表头、列表格组件,针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。实现思路开始想用三个ScrollView去实现滚动联动,固定表头、列的话,表格内容

微信小程序中怎么实现一个固定表头、列表格组件,针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。

实现思路

开始想用三个ScrollView去实现滚动联动,固定表头、列的话,表格内容滚动表头、列也应该对应滚动,写了demo后发现监听一个ScrollView的位置信息去设置另外两个ScrollView的位置真机会很卡,体验极差
使用position:sticky; 让表头相对表格顶部sticky,每行的第一个元素相对当前行左侧sticky。

遇到的问题:

  • 表格左滑的时候,滑动一个屏幕后固定列跟着滑出屏幕了。解决方法:动态设置表格的宽度,原理:滑出去的原因是整行滑出屏幕了,而sticky是相对整行左侧定位的。

  • 表格高度设置为100%后useReachBottom上拉监听失效 将表格高度设高的话固定表头就失效了。解决方法:表格用ScrollView套一层使用onScrollToLower监听加载

具体代码(React\taro3.0)

index.tsx

import React, { useState, useMemo, useEffect } from 'react'import classNames from 'classnames'// componentsimport { View, Text, ScrollView } from '@tarojs/components'// utilsimport { noop } from '@/utils/util'// stylesimport styles from './index.module.less'interface DataAttributeItem {  title: string  key: string | number  sorTKEy?: string | number}interface Props {  data: Array<any>  dataAttribute: Array<DataAttributeItem>  sortTypeChange?: (sort_item_id: any, sort_desc: boolean) => void  handleRow?: (data: any) => void  handleScrollToLower?: (e: any) => void}export default function Table(props: Props) {  const { data, dataAttribute, sortTypeChange = noop, handleRow = noop, handleScrollToLower = noop } = props  const [isSortDesc, setIsSortDesc] = useState<boolean>(true)  const [sortIndex, setSortIndex] = useState<number>(1)  const tableWidth = useMemo(() => {    return `${(dataAttribute.length * 148 + 48)}rpx`  }, [dataAttribute])  const tableHeight =  useMemo(() => {    return `${((data.length + 1) * 96)}rpx`  }, [data])  const handleSortItem = (attrItem, attrIndex) => {    if (attrIndex === 0) {      return    }    const beforeIndex = sortIndex    const sortKey = attrItem.sortKey    dataAttribute.map((item, index)=>{      if (item.sortKey === sortKey) {        if (beforeIndex === index) {          setIsSortDesc(!isSortDesc)        } else {          setSortIndex(index)          setIsSortDesc(true)        }      }    })  }  useEffect(()=>{    const sort_desc = isSortDesc    const sort_item_id = dataAttribute[sortIndex].sortKey    sortTypeChange(sort_item_id,sort_desc)  },[sortIndex, isSortDesc])  return (    <ScrollView className={styles['table']} scrollY scrollX onScrollToLower={handleScrollToLower}>      <View className={styles['sticky-box']} style={{height: tableHeight}}>        <View className={styles['grey-box']} style={{width: tableWidth, position: 'sticky'}}/>        <View className={styles['table__head']} style={{width: tableWidth, position: 'sticky'}}>          {dataAttribute.map((attrItem, attrIndex) => (            <View className={styles['table__head__td']} key={attrIndex} onClick={()=>handleSortItem(attrItem, attrIndex)}>              <Text                className={classNames({                  [styles['table__head__td__text']]: true,                  [styles['table__head__td__text-active']]: sortIndex === attrIndex,                })}                key={attrIndex}              >{attrItem.title}</Text>              {attrIndex !== 0 && <View                className={classNames({                  [styles['table__head__td__sorter-indicate']]: true,                  [styles['table__head__td__sorter-indicate--asc-active']]: sortIndex === attrIndex && !isSortDesc,                  [styles['table__head__td__sorter-indicate--desc-active']]: sortIndex === attrIndex && isSortDesc                })}              />}            </View>          ))}        </View>        {data.map((dataitem, dataIndex) => (          <View className={styles['table__row']} key={dataIndex} style={{width: tableWidth}} onClick={() => handleRow(dataItem)}>            {dataAttribute.map((attrItem, attrIndex) => {              return (                <Text className={styles['table__row__td']} key={attrIndex}>{dataItem[attrItem.key] || '-'}</Text>              )            })}          </View>        ))}      </View>    </ScrollView>  )}

index.module.less

@import '~@/assets/style/mixins/ellipsis.less';page{  font-size: 26rpx;  line-height: 60rpx;  color: #222;  height: 100%;  width: 100%;}.grey-box{  height: 10rpx;  top: 0;  background: #f8f8f8;  z-index: 100;}.table{  position: relative;  overflow: scroll;  width: 100%;  height: 100%;  overflow: scroll;  &__head{    position: relative;    height: 96rpx;    white-space: nowrap;    // position: sticky;    top: 10rpx;    z-index: 100;    height: 88rpx;    font-size: 24rpx;    line-height: 88rpx;    color: #aaabbd;    background-color: #f8f8f8;    border-bottom: 2rpx solid #ecf1f8;    background-color: #fff;    white-space: nowrap;    display: flex;    &__td{      .ellipsis();      width: 148rpx;      // padding-right: 40rpx;      display: flex;      justify-content: flex-start;      align-items: center;      background-color: #fff;      position: relative;      box-sizing: border-box;      &:nth-child(1) {        padding-left: 24rpx;        width: 154rpx;        margin-right: 40rpx;        position: sticky;        z-index: 10;        left: 0;      }      &__text{        display: inline;        &-active{          color: #6d70ff;        }      }      &__sorter-indicate{        width: 24rpx;        height: 24rpx;        display: inline-block;        background-repeat: no-repeat;        background-size: 100% 100%;        background-image: url('https://file.lsjlt.com/upload/202306/20/oeghpbd5onr.png');        &--asc-active {          background-image: url('Https://file.lsjlt.com/upload/202306/20/iowgnhflhp5.png');        }        &--desc-active {          background-image: url('https://file.lsjlt.com/upload/202306/20/2o1eqsrlziv.png');        }      }    }  }  &__row{    position: relative;    height: 96rpx;    white-space: nowrap;    display: flex;    justify-content: flex-start;    align-items: center;    border-bottom: 2rpx solid #ecf1f8;    &__td{      // .ellipsis();      overflow: scroll;      white-space: nowrap;      width: 148rpx;      // padding-right: 40rpx;      display: inline-block;      background-color: #fff;      position: relative;      box-sizing: border-box;      font-size: 26rpx;      line-height: 96rpx;      &:nth-child(1) {        margin-right: 40rpx;        padding-left: 24rpx;        width: 154rpx;        position: sticky;        z-index: 10;        left: 0;      }    }  }}

具体代码(小程序原生)

<ScrollView class="table" scroll-x scroll-y bindscrolltolower="handleScrollToLower">  <View class="sticky-box" >    <View class="table__head" >      <View class="table__head__td" wx:for="{{dataAttribute}}" wx:key="attrIndex" wx:for-index="attrIndex" wx:for-item="attrItem">        <Text          class="table__head__td__text"        >{{attrItem.title}}</Text>      </View>    </View>    <View class="table__row" wx:for="{{data}}" wx:key="dataIndex" wx:for-index="dataIndex" wx:for-item="dataItem" >      <Text class="table__row__td" wx:for="{{dataAttribute}}" wx:key="dataIndex" wx:for-index="attrIndex" wx:for-item="attrItem">{{dataItem[attrItem.key] || '-'}}</Text>    </View>  </View></ScrollView>
const app = getApp()Page({  data: {    data: [      {        a: 123,        b: 456,        c: 489,        d: 789,        e: 458,        f: 789      },      {        a: 123,        b: 456,        c: 489,        d: 789,        e: 458,        f: 789      },      {        a: 123,        b: 456,        c: 489,        d: 789,        e: 458,        f: 789      },      {        a: 123,        b: 456,        c: 489,        d: 789,        e: 458,        f: 789      },      {        a: 123,        b: 456,        c: 489,        d: 789,        e: 458,        f: 789      },      {        a: 123,        b: 456,        c: 489,        d: 789,        e: 458,        f: 789      },      {        a: 123,        b: 456,        c: 489,        d: 789,        e: 458,        f: 789      },      {        a: 123,        b: 456,        c: 489,        d: 789,        e: 458,        f: 789      },      {        a: 123,        b: 456,        c: 489,        d: 789,        e: 458,        f: 789      },      {        a: 123,        b: 456,        c: 489,        d: 789,        e: 458,        f: 789      },      {        a: 123,        b: 456,        c: 489,        d: 789,        e: 458,        f: 789      },      {        a: 123,        b: 456,        c: 489,        d: 789,        e: 458,        f: 789      },      {        a: 123,        b: 456,        c: 489,        d: 789,        e: 458,        f: 789      },      {        a: 123,        b: 456,        c: 489,        d: 789,        e: 458,        f: 789      },      {        a: 123,        b: 456,        c: 489,        d: 789,        e: 458,        f: 789      },      {        a: 123,        b: 456,        c: 489,        d: 789,        e: 458,        f: 789      },      {        a: 123,        b: 456,        c: 489,        d: 789,        e: 458,        f: 789      },      {        a: 123,        b: 456,        c: 489,        d: 789,        e: 458,        f: 789      },      {        a: 123,        b: 456,        c: 489,        d: 789,        e: 458,        f: 789      },      {        a: 123,        b: 456,        c: 489,        d: 789,        e: 458,        f: 789      },    ],    dataAttribute: [      {        title: '第一列',        key: 'a'      },      {        title: '第2列',        key: 'b'      },      {        title: '第3列',        key: 'c'      },      {        title: '第4列',        key: 'd'      },      {        title: '第5列',        key: 'e'      },      {        title: '第6列',        key: 'f'      }    ],    tableHeight: (20 + 1) * 96,    tableWidth: 200 * 6 + 60  }})
page{  font-size: 26rpx;  line-height: 60rpx;  color: #222;  height: 100%;  width: 100%;}.table{  display: block;  position: relative;  overflow: scroll;  width: 100%;  height: 100%;}.sticky-box{}.table__head{  height: 96rpx;  white-space: nowrap;  position: sticky;  top: 0rpx;  z-index: 100;  height: 88rpx;  font-size: 24rpx;  line-height: 88rpx;  color: #aaabbd;  background-color: #f8f8f8;  border-bottom: 2rpx solid #ecf1f8;  background-color: #fff;  white-space: nowrap;  display: flex;}.table__head__td{  width: 200rpx;  display: flex;  justify-content: flex-start;  align-items: center;  background-color: #fff;  box-sizing: border-box;  position: relative;  overflow: hidden;  white-space: nowrap;  -o-text-overflow: ellipsis;  text-overflow: ellipsis;}.table__head__td:nth-child(1) {  padding-left: 24rpx;  width: 260rpx;  margin-right: 40rpx;  position: sticky;  z-index: 101;  left: 0rpx;}.table__head__td__text{  display: inline;}.table__row{  position: relative;  height: 96rpx;  white-space: nowrap;  display: flex;  justify-content: flex-start;  align-items: center;  border-bottom: 2rpx solid #ecf1f8;}.table__row__td{  overflow: scroll;  white-space: nowrap;  width: 200rpx;  display: inline-block;  background-color: #fff;  box-sizing: border-box;  font-size: 26rpx;  line-height: 96rpx;  position: relative;  overflow: hidden;  white-space: nowrap;  -o-text-overflow: ellipsis;  text-overflow: ellipsis;}.table__row__td:nth-child(1) {  margin-right: 40rpx;  padding-left: 24rpx;  width: 260rpx;  position: sticky;  z-index: 10;  left: 0;}

关于微信小程序中怎么实现一个固定表头、列表格组件问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注编程网精选频道了解更多相关知识。

--结束END--

本文标题: 微信小程序中怎么实现一个固定表头、列表格组件

本文链接: https://www.lsjlt.com/news/296929.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • 微信小程序中怎么实现一个固定表头、列表格组件
    微信小程序中怎么实现一个固定表头、列表格组件,针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。实现思路开始想用三个ScrollView去实现滚动联动,固定表头、列的话,表格内容...
    99+
    2023-06-20
  • 微信小程序实现固定表头、列表格组件
    目录需求:功能点效果图实现思路具体代码(react\taro3.0)具体代码(小程序原生)总结需求: 微信小程序实现固定表头固定列表格组件(移动端做点小修改通用) 功能点 ...
    99+
    2022-11-12
  • 如何实现一个微信小程序仪表盘组件
    小编给大家分享一下如何实现一个微信小程序仪表盘组件,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!前言最近开发了一个小程序动态仪表...
    99+
    2022-10-19
  • 微信小程序怎么实现列表渲染
    这篇文章主要讲解了“微信小程序怎么实现列表渲染”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“微信小程序怎么实现列表渲染”吧!wx:for通过wx:for可以根据指定的数组,循环渲染重复的组件...
    99+
    2023-07-04
  • 微信小程序中怎么自定义一个components组件
    本篇文章为大家展示了微信小程序中怎么自定义一个components组件,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。小程序自定义组件找到components目录,没...
    99+
    2022-10-19
  • 微信小程序中怎么实现一个搜索框组件
    今天就跟大家聊聊有关微信小程序中怎么实现一个搜索框组件,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。search.wxml<view ...
    99+
    2022-10-19
  • 微信小程序怎么实现一个手写签名组件
    这期内容当中小编将会给大家带来有关微信小程序怎么实现一个手写签名组件,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。背景:在做项目过程中,需要在微信小程序中实现手写签名组件。在网上找了微信小程序手写签名实现...
    99+
    2023-06-20
  • 微信小程序中怎么实现一个picker多列选择器
    微信小程序中怎么实现一个picker多列选择器,针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。一、效果图(多列)二、普通选择器:mode = selector、多列选择器:m...
    99+
    2023-06-20
  • 微信小程序中怎么自定义一个多列选择器
    这篇文章给大家介绍微信小程序中怎么自定义一个多列选择器,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。在.wxml建一个自定义picker组件:<picker  &nbs...
    99+
    2022-10-19
  • 微信小程序中怎么实现组件传值
    今天就跟大家聊聊有关微信小程序中怎么实现组件传值,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。父传子<!-- 父组件A w...
    99+
    2022-10-19
  • 怎么在微信小程序中实现一个导航功能
    本篇文章给大家分享的是有关怎么在微信小程序中实现一个导航功能,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。操作步骤申请腾讯地图key——地址小程序后台添加腾讯插件——开发文档小...
    99+
    2023-06-08
  • 微信小程序中怎么实现一个计算器功能
    微信小程序中怎么实现一个计算器功能,很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。目录结构第一次进到页面它的目录结构如下:需要注意的问题(1)添加的新页面文件,都...
    99+
    2023-06-20
  • 怎么在Vue中使用Element实现一个树列表组件
    怎么在Vue中使用Element实现一个树列表组件?针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。1、常规树列表控件的使用众所周知,一般界面很多情况涉及到树列表的处理,如类型...
    99+
    2023-06-15
  • 怎么在微信小程序中实现一个Tab切换效果
    这篇文章给大家介绍怎么在微信小程序中实现一个Tab切换效果,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。使用步骤代码如下(示例):定义一个状态statusdata: {   st...
    99+
    2023-06-15
  • 微信小程序中怎么实现一个下拉菜单效果
    这篇文章将为大家详细讲解有关微信小程序中怎么实现一个下拉菜单效果,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。//wcss     ...
    99+
    2022-10-19
  • 微信小程序怎么实现商品列表跳转商品详情页功能
    这篇文章主要介绍“微信小程序怎么实现商品列表跳转商品详情页功能”,在日常操作中,相信很多人在微信小程序怎么实现商品列表跳转商品详情页功能问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”微信小程序怎么实现商品列表...
    99+
    2023-06-30
  • 微信小程序实战中位置闹铃如何利用条件渲染实现列表控件
    微信小程序实战中位置闹铃如何利用条件渲染实现列表控件,针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。需求在视频第32秒左右,有一个选择编辑对象的画面,它的功能是一个列表控件。...
    99+
    2023-06-19
  • 怎么在微信小程序中实现一个视频弹幕效果
    本篇文章为大家展示了怎么在微信小程序中实现一个视频弹幕效果,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。wxml代码<!--pages/study/video/videoplay/videop...
    99+
    2023-06-14
  • 怎么在微信小程序中实现一个文字滚动功能
    怎么在微信小程序中实现一个文字滚动功能?很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。具体内容如下wxml:<view>显示完后再显示:</vi...
    99+
    2023-06-14
  • 怎么在微信小程序中实现一个多行文字滚动效果
    这期内容当中小编将会给大家带来有关怎么在微信小程序中实现一个多行文字滚动效果,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。具体内容如下wxml<view class="full&...
    99+
    2023-06-14
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作