LinUI学习11 瀑布流分页加载的实现

LinUI学习11 瀑布流分页加载的实现

在10中我们完成了对数据的分页请求,这为我们使用LinUI自带的瀑布流组件提供了数据基础。

要实现如图效果

 

 

 

要在home页面使用LinUI的瀑布流,我们需要在home.json中引入

"usingComponents": { "l-water-flow":"/miniprogram_npm/lin-ui/water-flow/index" }

LinUI采用的是抽象组件,在其内部提供了插槽,给予了很大的便利性。

1、自定义瀑布流内部的组件

因此我们需要自定义瀑布流内部的组件

在conpomts文件夹下新建一个一个spu-preview文件夹,用于存放自定义spu-preview组件

 

记得在app.json中引入该组件

"s-spu-preview":"/components/spu-preview/index"

2、引用瀑布流组件

在home.wxml中如下使用瀑布流组件

<l-water-flow generic:l-water-flow-item="s-spu-preview"></l-water-flow> //“s-spu-preview”为自定义组件的名称与在app.json导入的一致

在home.js编写下列代码获取数据

/** * 页面上拉触底事件的处理函数 */ onReachBottom: async function () { const data = await this.data.spuPaging.getMoreData() if(!data){ return } wx.lin.renderWaterFlow(data.items) if(!data.moreData){ this.setData({ loadingType:"end" }) } }, // 瀑布流 async initBottomSpuList(){ const paging = await SpuPaging.getLatestPaging() this.data.spuPaging= paging const data =await paging.getMoreData() if(!data){ return } wx.lin.renderWaterFlow(data.items) //linui瀑布流传入数组的方法 },

注意: wx.lin.renderWaterFlow(data.items) 是其传入数组的方法

3、瀑布流内部自定义组件的编写

index.js

 

// components/spu-preview/index.js Component({ /** * 组件的属性列表 */ properties: { data:Object }, observers:{ data:function(data){ if(!data){ return } if(!data.tags){ return } const tags = data.tags.split('$') //依据关键字“$”截断字符串组成数组 this.setData({ tags }) } }, /** * 组件的初始数据 */ data: { tags:Array }, /** * 组件的方法列表 */ methods: { } })

 

index.json

{ "component": true, "usingComponents": {} }

index.wxml

 

<wxs src="../../wxs/price.wxs" module="p"></wxs> //这里是将折扣前后价格的逻辑放在了price.wxs内部 <view class="container"> <image mode="widthFix" src="{{data.img}}" class="img"></image> <view class="content-container"> <text class="title">{{data.title}} </text> <view class="tags"> <block wx:for="{{tags}}"> <l-tag l-class="l-tag">{{item}}</l-tag> </block> </view> <view class="price-row"> <l-price color=" #157658" value-size="28" unit-size="20" autofix value="{{p.mainPrice(data.price,data.discount_price)}}"> </l-price> <l-price l-class="discount_price" color="#999999" size="26" wx:if="{{data.discount_price?true:false}}" deleted value="{{p.slasherPriced(data.price,data.discount_price)}}"> </l-price> </view> <text class="subtitle">{{data.subtitle}}</text> </view> </view>

index.wxss

.container{ display: flex; flex-direction: column; box-shadow: 0px 0px 8px 0px rgba(119, 163, 149, 0.2); margin-bottom: 30rpx; background-color: white; width: 340rpx; } .img { width: 100%; height: 360rpx; } .l-tag{ background-color: #dcebe6 !important; color: #157658 !important; padding-left: 6rpx !important; padding-right: 6rpx !important; height: 36rpx !important ; margin-right: 5rpx !important; } .title{ font-size: 28rpx; color: #333333; } .content-container{ display: flex; flex-direction: column; margin: 16rpx 16rpx 22rpx 16rpx ; } .price-row{ display: flex; flex-direction: row; } .discount_price{ margin-left: 20rpx; } .tags{ display: flex; flex-direction: row; margin-bottom: 8rpx; margin-top:6rpx ; flex-wrap: wrap; } .subtitle{ font-size: 24rpx; font-weight: 300; margin-top: 6rpx; color: #888; }

 

price.wxs

function mainPrice(price,discountPrice){ if(!discountPrice){ return price } else{ return discountPrice } } function slasherPriced(price,discountPrice){ if(discountPrice){ return price } else{ return } } module.exports={ mainPrice:mainPrice, slasherPriced:slasherPriced }

 

这样就实现了瀑布流分页加载的效果。

 


__EOF__

本文作者Mr丶Kr's blog
本文链接https://www.cnblogs.com/mrkr/p/14305994.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角推荐一下。您的鼓励是博主的最大动力!
posted @   Mr、Kr  阅读(608)  评论(1编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· winform 绘制太阳,地球,月球 运作规律
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 上周热点回顾(3.3-3.9)
点击右上角即可分享
微信分享提示