当前位置:   article > 正文

微信小程序商城项目实战(第三篇:商品列表)_微信小程序 view 首页商品列表模板demo

微信小程序 view 首页商品列表模板demo

实现商品列表

利用组件实现…

Tabs

组件界面编写

  1. <view class="top">
  2. <view wx:for="{{tabList}}" wx:key="index" class="title {{index == curr ? 'active':''}}" bindtap="tapwhere" data-index="{{index}}">{{item}}</view>
  3. </view>
  4. <navigator class="goods" wx:for="{{GoodsList}}" wx:key="cat_id" url="/pages/goods_detail/goods_detail?goods_id={{item.goods_id}}">
  5. <view class="img">
  6. <image src="{{item.goods_small_logo==''?'https://ww1.sinaimg.cn/large/007rAy9hgy1g24by9t530j30i20i2glm.jpg':item.goods_small_logo}}" mode="widthFix" />
  7. </view>
  8. <view class="context">{{item.goods_name}}<view class="price">¥{{item.goods_price}}</view></view>
  9. </navigator>

组件样式

  1. .top{
  2. display: flex;
  3. justify-content: center;
  4. }
  5. .top .title{
  6. display: flex;
  7. flex: auto;
  8. height: 100rpx;
  9. align-items: center;
  10. justify-content: center;
  11. border-bottom: #666666 solid 1rpx;
  12. }
  13. .top .active{
  14. color: var(--themeColor);
  15. border-bottom: var(--themeColor) solid 1rpx;
  16. }
  17. .goods{
  18. display: flex;
  19. height: 280rpx;
  20. border-bottom: #666666 solid 1rpx;
  21. }
  22. .goods .img{
  23. flex: 2;
  24. width: 60%;
  25. justify-content: center;
  26. }
  27. .goods .img image{
  28. transform: scale(0.8)
  29. }
  30. .goods .context{
  31. flex: 3;
  32. font-size: 30rpx;
  33. margin-top: 20rpx;
  34. }
  35. .goods .context .price{
  36. color: red;
  37. font-size: 40rpx;
  38. margin-top: 50rpx;
  39. }

组件js(属性、方法)

tabList上方三个导航栏
GoodsList商品列表
curr:当前选中的导航栏下标…

  1. // components/Tabs.js
  2. Component({
  3. /**
  4. * 组件的属性列表
  5. */
  6. properties: {
  7. tabList:[],
  8. GoodsList:[],
  9. curr:0
  10. },
  11. /**
  12. * 组件的初始数据
  13. */
  14. data: {
  15. },
  16. /**
  17. * 组件的方法列表
  18. */
  19. methods: {
  20. //处理选中的导航栏下标,实现下方红色边框跟随....
  21. tapwhere(e){
  22. this.triggerEvent('tapwhere',{
  23. index:e.target.dataset.index
  24. })
  25. }
  26. }
  27. })

开始写列表页

引入搜索框和tab组件,并且允许下拉刷新…

  1. {
  2. "usingComponents": {
  3. "search-input":"../../components/SearchInput/SearchInput",
  4. "tab":"../../components/Tabs/Tabs"
  5. },
  6. "enablePullDownRefresh": true,
  7. "backgroundTextStyle": "dark",
  8. "navigationBarTitleText": "商品列表"
  9. }

界面编写

引用的两个组件,给tab组件传参,传递数据…

  1. <view>
  2. <search-input></search-input>
  3. <tab tabList="{{tabList}}" curr="{{curr}}" GoodsList="{{GoodsList}}" bind:tapwhere="_where"></tab>
  4. </view>

JS方法编写

data属性:

  • tabList:导航栏的数据
  • GoodsList:商品列表数据
  • curr:当前选中的下标
  • pagenum:当前页码
  • pagetotal:总数据量
  • cid 商品分类id(用于查询对应分类的id,由商品分类传递过来.)

方法:

  • _where:用于传递选中的选项卡下标,返回给组件…
  • getgoods:获取商品列表并且赋值给GoodsList
  • 然后还有一个下拉刷新事件和上拉继续加载事件…
  1. import {request} from '../../utils/request'
  2. Page({
  3. /**
  4. * 页面的初始数据
  5. */
  6. data: {
  7. tabList:['综合','销量','价格'],
  8. GoodsList:[],
  9. curr:0,
  10. pagenum:1,
  11. pagetotal:0,
  12. cid:0
  13. },
  14. _where(e){
  15. this.setData({
  16. curr:e.detail.index
  17. })
  18. },
  19. async getgoods(opt){
  20. if(opt!=undefined){
  21. console.log('有数据');
  22. this.setData({
  23. cid:opt.cid
  24. })
  25. }
  26. let ret=await request('goods/search',{pagenum:this.data.pagenum,pagesize:10,cid:this.data.cid})
  27. if(this.data.GoodsList.length!=0){
  28. var good=this.data.GoodsList;
  29. }
  30. this.setData({
  31. GoodsList:ret.data.message.goods,
  32. pagetotal:ret.data.message.total
  33. });
  34. good=[...good,...this.data.GoodsList];
  35. this.setData({
  36. GoodsList:good,
  37. })
  38. },
  39. /**
  40. * 生命周期函数--监听页面加载
  41. */
  42. onLoad: function (options) {
  43. // console.log(options);
  44. this.getgoods(options)
  45. },
  46. /**
  47. * 生命周期函数--监听页面初次渲染完成
  48. */
  49. onReady: function () {
  50. },
  51. /**
  52. * 生命周期函数--监听页面显示
  53. */
  54. onShow: function () {
  55. },
  56. /**
  57. * 生命周期函数--监听页面隐藏
  58. */
  59. onHide: function () {
  60. },
  61. /**
  62. * 生命周期函数--监听页面卸载
  63. */
  64. onUnload: function () {
  65. },
  66. /**
  67. * 页面相关事件处理函数--监听用户下拉动作
  68. */
  69. onPullDownRefresh: function () {
  70. this.data.GoodsList=[];
  71. this.data.pagenum=1;
  72. this.getgoods();
  73. },
  74. /**
  75. * 页面上拉触底事件的处理函数
  76. */
  77. onReachBottom: function () {
  78. if(this.data.pagenum===Math.ceil(this.data.pagetotal/10)){
  79. wx.showToast({
  80. title: '已经没有商品啦.....',
  81. icon: 'none',
  82. duration: 2000//持续的时间
  83. })
  84. }else{
  85. wx.showToast({
  86. title: '加载中...',
  87. icon: 'loading',
  88. duration: 800//持续的时间
  89. })
  90. this.data.pagenum++;
  91. this.getgoods();
  92. }
  93. },
  94. /**
  95. * 用户点击右上角分享
  96. */
  97. onShareAppMessage: function () {
  98. }
  99. })

效果图

刚进来时…
在这里插入图片描述
滑到底部时
在这里插入图片描述
没数据时:
在这里插入图片描述
下拉刷新:
在这里插入图片描述

成功实现~

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号