微信小程序如何实现列表数据的详情页功能

  1. 微信小程序点击列表页进入详情页,在列表页 list.wxml中,对于每一个数据项,可以通过 catchtap去绑定一个事件,去详情页的事件 toDetail。 对于详情页的传输数据,点击事件可以通过 data- 去实现,后面相当于是键值对,data-index="{{index}}",意思是传输的值的名字叫index,值为 index,代码如下:
<block wx:for="{{listArr}}" wx:key="{{index}}">
    <view catchtap="toDetail" data-index="{{index}}">
      <!-- 引入模版的数据 -->
      <template is="listTmp" data="{{...item}}"/>
    </view>
  </block>
  1. list.js中,可以定义 toDetail 方法,传入 event事件对象,可以 控制台打印一下 ,如图所示:
    在这里插入图片描述

  2. 通过控制台打印可以发现,event.currentTarget.dataset.index 的值就是从列表页传过来的 index 的值,通过这个值就可以获取点击对应的下标。之后,就可以通过 wx.navigateTo 进行详情页的跳转,通过拼接的 index的值。需要注意 wx.navigateTo会触发页面隐藏 onHidewx.redirectTo会触发页面卸载 onUnload,代码如下:

// 点击进入详情页
  toDetail: function (event) {
    // 获取 event 事件对象
    console.log(event)
    // 获取点击对应的下标
    const index = event.currentTarget.dataset.index

    //wx.navigateTo 会触发页面隐藏onHide
    wx.navigateTo({
      url: '/pages/detail/detail?index=' + index,
    })
  }
  1. 在详情页 detail.js 中,可以通过onLound生命周期函数,传入 options参数,页就是在页面一开始加载的时候就会执行,我们可以控制台打印一下,如图所示:
    在这里插入图片描述

  2. 通过控制台打印可以发现,options.index就是从列表页传入详情页的 index的值,我们就可以获取这个参数值,通过这个参数值找到数据的相应项,在data中定义detailObj对象,默认为空对象,定义index的值,默认为null,代码如下:

/**
   * 页面的初始数据
   */
  data: {
    detailObj: {},
    index: null,
    isCollected: false,
    isMusicPlay: false
  }
  1. detail.js 中,在onLound生命周期函数中,可以通过 this.setData方法,将 detailObj的值修改为 datas.list_data[index]数据的相应的下标值,这样就可以拿到详情页的数据了,代码如下所示:
/**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    // 获取参数值
    let index = options.index
    // 更新data中的detailObj的值
    this.setData({
      detailObj: datas.list_data[index],
      index
    })
  }
  1. 在 控制台的 AppData选项中,就可以看到详情页的数据,保存在 detailObj 对象中

  2. 渲染detailObj中详情的数据,代码如下所示:

<!--pages/detail/detail.wxml-->
<view class="detailContainer">
  <image class="headImg" src="{{detailObj.detail_img}}"></image>
  <image catchtap="handleMusicPlay" class="musicImg" src="{{isMusicPlay ? '/images/music/music-start.png' : '/images/music/music-stop.png'}}"></image>
  <view class="avatar_date">
    <image src="{{detailObj.avatar}}"></image>
    <text>{{detailObj.author}}</text>
    <text>发布于</text>
    <text>{{detailObj.date}}</text>
  </view>
  <text class="company">{{detailObj.title}}</text>
  <view class="collection_share_container">
    <view class="collection_share">
      <image catchtap="handleCollection" wx:if="{{!isCollected}}" src="/images/icon/collection-anti.png"></image>
      <image catchtap="handleCollection" wx:if="{{isCollected}}" src="/images/icon/collection.png"></image>
      <image catchtap="handleShare" src="../../images/icon/share.png"></image>
    </view>
    <view class="line"></view>
  </view>
  <button open-type="share">转发文章</button>
  <text class="content">{{detailObj.detail_content}}</text>
</view>

  • 7
    点赞
  • 54
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个微信小程序详情页的示例代码,包含了 wxml 和 wxss: wxml: ```html <view class="container"> <!-- 商品图片轮播 --> <swiper class="swiper" indicator-dots="{{true}}" autoplay="{{true}}"> <swiper-item> <image src="/images/product1.png"></image> </swiper-item> <swiper-item> <image src="/images/product2.png"></image> </swiper-item> <swiper-item> <image src="/images/product3.png"></image> </swiper-item> </swiper> <!-- 商品信息 --> <view class="product-info"> <text class="name">商品名称</text> <text class="price">¥99</text> <text class="desc">这是一款非常好用的商品,非常适合您的需求,赶快购买吧!</text> </view> <!-- 商品规格 --> <view class="product-specification"> <text class="title">商品规格</text> <view class="specification-group"> <text class="label">颜色</text> <view class="specification"> <text class="value">黑色</text> <text class="value">白色</text> <text class="value">红色</text> </view> </view> <view class="specification-group"> <text class="label">尺码</text> <view class="specification"> <text class="value">S</text> <text class="value">M</text> <text class="value">L</text> </view> </view> </view> <!-- 商品详情 --> <view class="product-detail"> <text class="title">商品详情</text> <image class="detail-image" src="/images/detail.png"></image> </view> <!-- 底部操作栏 --> <view class="bottom-bar"> <view class="left"> <text class="iconfont icon-shoucang"></text> <text>收藏</text> </view> <view class="right"> <button class="add-cart">加入购物车</button> <button class="buy-now">立即购买</button> </view> </view> </view> ``` wxss: ```css .container { display: flex; flex-direction: column; align-items: center; padding: 20rpx; } .swiper { width: 100%; height: 400rpx; margin-bottom: 20rpx; } .product-info { width: 100%; margin-bottom: 20rpx; } .product-info .name { font-size: 32rpx; font-weight: bold; margin-bottom: 20rpx; } .product-info .price { font-size: 28rpx; color: #f44336; margin-bottom: 20rpx; } .product-info .desc { font-size: 28rpx; color: #666; line-height: 40rpx; } .product-specification { width: 100%; margin-bottom: 20rpx; } .product-specification .title { font-size: 28rpx; font-weight: bold; margin-bottom: 20rpx; } .specification-group { display: flex; flex-direction: column; margin-bottom: 20rpx; } .specification-group .label { font-size: 28rpx; font-weight: bold; margin-bottom: 10rpx; } .specification { display: flex; flex-wrap: wrap; } .specification .value { display: flex; justify-content: center; align-items: center; width: 100rpx; height: 60rpx; margin-right: 20rpx; margin-bottom: 20rpx; font-size: 26rpx; border: 1rpx solid #ddd; border-radius: 5rpx; } .product-detail { width: 100%; margin-bottom: 20rpx; } .product-detail .title { font-size: 28rpx; font-weight: bold; margin-bottom: 20rpx; } .product-detail .detail-image { width: 100%; } .bottom-bar { display: flex; justify-content: space-between; align-items: center; width: 100%; height: 100rpx; background-color: #fff; border-top: 1rpx solid #ddd; position: fixed; bottom: 0; left: 0; z-index: 999; } .bottom-bar .left { display: flex; align-items: center; margin-left: 20rpx; } .bottom-bar .left .iconfont { font-size: 36rpx; color: #666; margin-right: 10rpx; } .bottom-bar .left text { font-size: 28rpx; color: #666; } .bottom-bar .right { display: flex; align-items: center; margin-right: 20rpx; } .bottom-bar button { display: flex; justify-content: center; align-items: center; width: 200rpx; height: 80rpx; font-size: 28rpx; color: #fff; border-radius: 5rpx; margin-left: 10rpx; } .bottom-bar .add-cart { background-color: #f44336; } .bottom-bar .buy-now { background-color: #388e3c; } ``` 以上代码实现了一个简单的微信小程序详情页,包含了商品图片轮播、商品信息、商品规格、商品详情和底部操作栏。你可以根据自己的需求进行修改和优化。同时,建议添加一些交互和逻辑代码,实现加入购物车、立即购买等功能

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值