码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • 【小程序项目开发 -- 京东商城】uni-app 商品分类页面(下)


    在这里插入图片描述

    👋👋欢迎来到👋👋
    🎩魔术之家!!🎩

    该文章收录专栏
    ✨-- 2022微信小程序京东商城实战 --✨

    专栏内容
    ✨-- uni-app项目搭建 --✨
    ✨-- 京东商城uni-app 配置tabBar & 窗口样式 --✨
    ✨-- 京东商城uni-app开发之分包配置 --✨
    ✨-- 京东商城uni-app开发之轮播图 --✨
    ✨-- 京东商城uni-app之分类导航区域 --✨
    ✨-- 京东商城uni-app 楼层数据获取 渲染页面UI --✨
    ✨-- 京东商城uni-app 商品分类页面(上) --✨

    • 姊妹篇 【小程序项目开发 – 京东商城】uni-app 商品分类页面(上)

    文章目录

    • 一、渲染右侧二级和三级分类
      • 1.1 动态渲染二级分类页面
    • 二、渲染二级分类UI结构
    • 三、渲染三级分类UI结构
    • 四、切换一级分类重置滚动条位置
    • 五、点击三级分类跳转到商品页面
    • 六、分支的提交和合并

    一、渲染右侧二级和三级分类

    在上文 【小程序项目开发 – 京东商城】uni-app 商品分类页面(上)5.1 章节接口数据格式可以看到,我们的数据,在一级分类下,存贮了二级分类,二级分类又存贮了三级分类,嵌套存贮。

    1.1 动态渲染二级分类页面

    • 在data节点定义数据cateList2
        data() {
          return {
            //当前设备可用高度
            windowHeight: '',
            // 分类页数据
            cateList: [],
            // active 索引判断
            active: 0,
            // 二级分类数据
            cateList2: [],
          };
        },
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 请求数据时在函数getCateList为其赋值(默认为第一个数据,动态数据变化在active
          async getCateList() { // async 异步不能使用箭头函数
            const {
              data: res
            } = await uni.$http.get('/api/public/v1/categories')
            // 判断是否赋值成功
            if (res.meta.status != 200) return uni.$showMsg()
            // 一级分类赋值 
            this.cateList = res.message
            // 二级分类赋值
            this.cateList2 = this.cateList[0].children
          }
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 在active激活项函数activeTap也对其进行动态数据绑定
    methods: {
          // 触击事件绑定
          activeTap(options) {
            // 传参方法一:
            // this.active = options.target.dataset.active
            // 传参方法二
            this.active = options
            // 动态修改二级列表
            this.cateList2 = this.cateList[options].children
          },
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    效果:
    在这里插入图片描述

    二、渲染二级分类UI结构

    • 结构
        <!-- 右侧container -->
          <scroll-view scroll-y="true" class="scroll-view-right" :style="{height: windowHeight + 'px'}">
            <view class="cate-level2" v-for="(item,index) in cateList2" v-bind:key="index">
              <!-- 标题 -->
            <view class="cate-level2-title">{{'/ ' + item.cat_name + ' /'}}</view>           <!-- / {{item.cat_name}} / -->
            <!-- 项目容器 -->
            <view>
              <view class="cate-level2-list" v-for="(prd,prdindex) in item.children" v-bind:key="prdindex">
                <view class="cate-level2-prd">
                  <image v-bind:src="prd.cat_icon" mode="widthFix"></image>
                </view>
              </view>
            </view>
            </view>
          </scroll-view>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 样式
      .cate-level2-title {
        font-weight: 700;
        padding: 2px;
        font-size: 14px;
      }
    
    • 1
    • 2
    • 3
    • 4
    • 5

    效果:
    在这里插入图片描述

    三、渲染三级分类UI结构

    注意:在样式image组件的属性mode尽量不要使用,样式会很难调整

    • 结构
      <!-- 右侧container -->
          <scroll-view scroll-y="true" class="scroll-view-right" :style="{height: windowHeight + 'px'}">
            <view class="cate-level2" v-for="(item,i2) in cateList2" v-bind:key="i2">
              <!-- 二级分类项目标题 -->
              <view class="cate-level2-title">{{'/ ' + item.cat_name + ' /'}}</view> <!-- / {{item.cat_name}} / -->
              <!-- 三级分类列表 -->
              <view class="cate-level3-list">
                <!-- 三级分类的item项 -->
                <view class="cate-level3-list-item" v-for="(prd,i3) in item.children" v-bind:key="i3">
                  <!-- 三级分类项目的图片 -->
                  <image v-bind:src="prd.cat_icon"></image>
                  <!-- 三级分类项目的文本 -->
                  <text>{{prd.cat_name}}</text>
                </view>
              </view>
            </view>
          </scroll-view>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 样式
    
      .scroll-view-right {
    
        background-color: #fff;
    
        .cate-level2-title {
          font-weight: 700;
          padding: 2px;
          font-size: 14px;
          text-align: center;
        }
    
      }
    
      .cate-level2 {
        padding: 10rpx;
        padding-bottom: 20rpx;
      }
    
      // 三级分类样式
      .cate-level3-list {
        display: flex;
        // 允许自动换行
        flex-wrap: wrap;
    
        .cate-level3-list-item {
          // 整体三分之一
          width: 33.33%;
          display: flex;
          flex-direction: column;
          box-sizing: border-box;
          justify-content: space-around;
          align-items: center;
    
          image {
            width: 160rpx;
            height: 160rpx;
            margin-bottom: 5rpx;
          }
    
          text {
            font-size: 25rpx;
          }
        }
    
      }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46

    效果:
    在这里插入图片描述

    四、切换一级分类重置滚动条位置

    • 在data节点定义数据scrollTop

    注意:对scrollTop 赋值前后值不变情况 下会没有效果,如果默认值为0,函数动态赋值也为0,那么组件就会默认为0,视为没有变化,这里解决方法是在0,1变化(1像素默认其为顶部,一点点偏差用户看不出来的😎)

    data() {
          return {
            //当前设备可用高度
            windowHeight: '',
            // 分类页数据
            cateList: [],
            // active 索引判断
            active: 0,
            // 二级分类数据
            cateList2: [],
            // 滚动条位置 
            scrollTop: 1 
          };
        },
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 为scroll-view动态绑定scroll-top属性值
     <!-- 右侧container -->
     <scroll-view scroll-y="true" class="scroll-view-right" :style="{height: windowHeight + 'px'}" v-bind:scroll-top="scrollTop">
    
    • 1
    • 2
    • 切换一级分类,动态设置scrollTop
    // 触击事件绑定
     activeTap(options) {
       // 传参方法一:
       // this.active = options.target.dataset.active
       // 传参方法二
       this.active = options
       // 动态修改二级列表
       this.cateList2 = this.cateList[options].children
       // 重置滚动条位置 动态变化
       this.scrollTop = this.scrollTop === 0 ? 1 : 0
     },
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    在这里插入图片描述

    五、点击三级分类跳转到商品页面

    • 绑定事件函数
    <!-- 三级分类的item项 -->
    <view class="cate-level3-list-item" v-for="(prd,i3) in item.children" v-bind:key="i3" v-on:click="gotoGoodsList(prd)">
    
    • 1
    • 2
    • 定义函数跳转页面,并传参数 商品id
    gotoGoodsList: prd => {
        uni.navigateTo({
          url: '/subpackages/goods_list/goods_list?cat_id=' + prd.cat_id
        })
    
    • 1
    • 2
    • 3
    • 4

    效果:
    在这里插入图片描述

    六、分支的提交和合并

    • git status 注释:查看当前文件状态
    • git add . 注释: 提交所有文件到暂存区
    • git commit -m "完成分类页面的开发" 注释:提交到本地仓库
    • git push -u origin cate 注释:提交到远程仓库的cate分支
    • git branch 注释:查看当前分支
    • git checkout master 注释:切换到主分支
    • git merge cate 注释:合并cate分支
    • git push 注释:上传主分支到远程仓库
    • git branch -d cate 注释:本地cate分支

    在这里插入图片描述

      ✨谢谢你的阅读,您的点赞和收藏就是我创造的最大动力!✨
    
    • 1
  • 相关阅读:
    Numpy解决找出二维随机矩阵中每行数据中最接近某个数字的数字
    QML自定义BusyIndicator样式
    part-02 C++知识总结(类型转换)
    obsidian加git备份,同时忽略掉自己不想同步的文件夹
    七月集训day06 最长回文子串 —— 一题多解
    SSM整合项目(添加家居)
    【智能优化算法-粒子群算法】基于改进粒子群算法实现汽车动力传动参数优化设计附matlab代码
    [思维]Longest Common Subsequence 2022牛客多校第8场 F
    100天精通Andriod逆向——第1天:ADB原理及其常用命令
    阿里云函数计算 GPU 宣布降价,最高幅度达 93%,阶梯计费越用越便宜!
  • 原文地址:https://blog.csdn.net/weixin_66526635/article/details/125500353
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号