小程序点击之数据绑定

<return />

<view class="all-wrap" style="padding-top:{{topHeight}}px;">

<view class="my-title">我的收藏</view>

<scroll-view

class="collect-list-container"

scroll-y

scroll-top="{{scrollTop}}"

refresher-enabled="{{true}}"

refresher-default-style="white"

refresher-triggered="{{isRefreshing}}"

bindrefresherrefresh="onPullDownRefresh"

>

<!-- 收藏列表 -->

<block wx:if="{{sortedKeys.length === 0}}">

<view class="collectList-nodata">暂无收藏内容</view>

</block>

<block wx:for="{{sortedKeys}}" wx:key="index" wx:for-item="type">

<view class="collect-part-title">{{type === 'FOOD' ? '美食卡' : (type === 'GOODS' ? '商品卡' : (type === 'ROUTE' ? '路线卡' : (type === 'KNOWLEDGE' ? '知识卡' : (type === 'ATTRACTION_VIEW' ? '景点卡' : (type === 'ATTRACTION_GUIDE' ? '讲解卡' : '')))))}}</view>

<scroll-view scroll-x class="collect-items">

<block wx:for="{{sortedCollects[type]}}" wx:key="id">

<view class="collect-item" bind:tap="onCardDetailAction">

<image class="collect-image" src="{{item.cardData.privData.imageUrl}}" mode="aspectFill"></image>

<text class="collect-name">{{item.cardData.name}}</text>

</view>

</block>

</scroll-view>

</block>

</scroll-view>

</view>

我们如果想要在onCardDetailAction关联绑定数据,应该如何呢?

在WXML中,你需要给每个收藏项绑定数据,这样在点击事件中才能获取到对应的item数据。以下是修改方案:

1. 修改WXML,添加data-item属性:

xml

复制代码
<view class="collect-item" bind:tap="onCardDetailAction" data-item="{{item}}">
  <image class="collect-image" src="{{item.cardData.privData.imageUrl}}" mode="aspectFill"></image>
  <text class="collect-name">{{item.cardData.name}}</text>
</view>

2. 修改JS,通过dataset获取数据:

javascript

复制代码
// 卡片详情
onCardDetailAction(e) {
  console.log('卡片详情', e);
  
  // 获取绑定的item数据
  const item = e.currentTarget.dataset.item;
  console.log('点击的item数据:', item);
  
  // 这里可以继续处理跳转逻辑
  if (item && item.cardData) {
    const cardId = item.cardData.id;
    const cardType = item.cardData.type;
    const resourceId = item.cardData.resourceId;
    
    console.log('卡片ID:', cardId, '卡片类型:', cardType, '资源ID:', resourceId);
    
    // 根据卡片类型跳转到不同页面
    // wx.navigateTo({
    //   url: `/pages/cardDetail/cardDetail?cardId=${cardId}&type=${cardType}`
    // });
  }
}

3. 如果你需要更精确的数据,可以只绑定必要的数据:

xml

复制代码
<!-- 只绑定必要的数据,减少数据传输量 -->
<view class="collect-item" bind:tap="onCardDetailAction" 
      data-cardid="{{item.cardData.id}}" 
      data-type="{{item.cardData.type}}"
      data-resourceid="{{item.cardData.resourceId}}">
  <image class="collect-image" src="{{item.cardData.privData.imageUrl}}" mode="aspectFill"></image>
  <text class="collect-name">{{item.cardData.name}}</text>
</view>

对应的JS代码:

javascript

复制代码
// 卡片详情
onCardDetailAction(e) {
  const dataset = e.currentTarget.dataset;
  console.log('卡片详情数据:', dataset);
  
  const cardId = dataset.cardid;
  const cardType = dataset.type;
  const resourceId = dataset.resourceid;
  
  console.log('卡片ID:', cardId, '卡片类型:', cardType, '资源ID:', resourceId);
  
  // 根据卡片类型跳转到不同页面
  // if (cardType === 'ATTRACTION_GUIDE') {
  //   wx.navigateTo({
  //     url: `/pages/guideDetail/guideDetail?id=${cardId}`
  //   });
  // } else if (cardType === 'FOOD') {
  //   wx.navigateTo({
  //     url: `/pages/foodDetail/foodDetail?id=${cardId}`
  //   });
  // }
  // ...其他类型处理
}

这样修改后,点击事件就能获取到对应的item数据了。建议使用第二种方式(只绑定必要数据),因为传输数据量更小,性能更好。

相关推荐
半兽先生10 分钟前
微信小程序与web-view页面双向通信
前端·微信小程序·小程序
小明记账簿11 分钟前
微信小程序中Crypto.js加密解密
微信小程序·小程序·加密·解密
qq_124987075318 分钟前
基于springboot的幼儿园家校联动小程序的设计与实现(源码+论文+部署+安装)
java·spring boot·后端·spring·微信小程序·小程序
游戏开发爱好者840 分钟前
苹果 App 上架流程,结合 Xcode、CI 等常见工具
macos·ios·ci/cd·小程序·uni-app·iphone·xcode
2501_915106321 小时前
用 HBuilder 上架 iOS 应用时如何管理Bundle ID、证书与描述文件
android·ios·小程序·https·uni-app·iphone·webview
2501_915909061 小时前
资源文件混淆在 iOS 应用安全中的实际价值
android·安全·ios·小程序·uni-app·iphone·webview
2501_915918411 小时前
iOS App 性能测试中常被忽略的运行期问题
android·ios·小程序·https·uni-app·iphone·webview
狮子也疯狂2 小时前
【鸿蒙小程序】| 鸿蒙系统中的元服务创新与实践
小程序·notepad++·harmonyos
汝生淮南吾在北16 小时前
SpringBoot3+Vue3小区物业报修系统+微信小程序
微信小程序·小程序·vue·毕业设计·springboot·课程设计·毕设
汤姆yu18 小时前
基于微信小程序的自习室座位预约系统
微信小程序·小程序