uniapp-分类配置

渲染分类页面的基本结构

1.首先需要定义页面结构:

<template>
  <view>
    <view class="scroll-view-container">
      <!-- 左侧的滚动视图区域 -->
      <scroll-view class="left-scroll-view" scroll-y :style="{height: wh + 'px'}">
        <view class="left-scroll-view-item active">xxx</view>
        <view class="left-scroll-view-item">xxx</view>
        <view class="left-scroll-view-item">xxx</view>
        <view class="left-scroll-view-item">xxx</view>
        <view class="left-scroll-view-item">xxx</view>
        <view class="left-scroll-view-item">多复制一些节点,演示纵向滚动效果...</view>
      </scroll-view>
      <!-- 右侧的滚动视图区域 -->
      <scroll-view class="right-scroll-view" scroll-y :style="{height: wh + 'px'}">
        <view class="left-scroll-view-item">zzz</view>
        <view class="left-scroll-view-item">zzz</view>
        <view class="left-scroll-view-item">zzz</view>
        <view class="left-scroll-view-item">zzz</view>
        <view class="left-scroll-view-item">多复制一些节点,演示纵向滚动效果</view>
      </scroll-view>
    </view>
  </view>
</template>

2.动态计算窗口的剩余高度:

<script>
  export default {
    data() {
      return {
        // 窗口的可用高度 = 屏幕高度 - navigationBar高度 - tabBar 高度
        wh: 0
      };
    },
    onLoad() {
      // 获取当前系统的信息
      const sysInfo = uni.getSystemInfoSync()
      // 为 wh 窗口可用高度动态赋值
      this.wh = sysInfo.windowHeight
    }
  }
</script>

3.美化页面结构:

.scroll-view-container {
  display: flex;

  .left-scroll-view {
    width: 120px;

    .left-scroll-view-item {
      line-height: 60px;
      background-color: #f7f7f7;
      text-align: center;
      font-size: 12px;

      // 激活项的样式
      &.active {
        background-color: #ffffff;
        position: relative;

        // 渲染激活项左侧的红色指示边线
        &::before {
          content: ' ';
          display: block;
          width: 3px;
          height: 30px;
          background-color: #c00000;
          position: absolute;
          left: 0;
          top: 50%;
          transform: translateY(-50%);
        }
      }
    }
  }
}

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

获取分类数据

1.在 data 中定义分类数据节点
2.调用获取分类列表数据的方法
3.定义获取分类列表数据的方法

data() {
  return {
    // 分类数据列表
    cateList: []
  }
}
onLoad() {
  // 调用获取分类列表数据的方法
  this.getCateList()
}
methods: {
  async getCateList() {
    // 发起请求
    const { data: res } = await uni.$http.get('/api/public/v1/categories')
    // 判断是否获取失败
    if (res.meta.status !== 200) return uni.$showMsg()
    // 转存数据
    this.cateList = res.message
  }
}

cateList内容:

在这里插入图片描述

动态渲染左侧的一级分类列表

1.循环渲染列表结构,为选中项动态添加 .active 类名,为一级分类的 item 项绑定点击事件处理函数 activeChanged
2.在 data 中定义默认选中项的索引
3.定义 activeChanged 事件处理函数,动态修改选中项的索引

<!-- 左侧的滚动视图区域 -->
<scroll-view class="left-scroll-view" scroll-y :style="{height: wh + 'px'}">
  <block v-for="(item, i) in cateList" :key="i">
    <view :class="['left-scroll-view-item', i === active ? 'active' : '']" @click="activeChanged(i)">{{item.cat_name}}</view>
  </block>
</scroll-view>

data() {
  return {
    // 当前选中项的索引,默认让第一项被选中
    active: 0
  }
}

methods: {
  // 选中项改变的事件处理函数
  activeChanged(i) {
    this.active = i
  }
}

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

动态渲染右侧的二级分类列表

1.在 data 中定义二级分类列表的数据节点
2.修改 getCateList 方法,在请求到数据之后,为二级分类列表数据赋值
3.修改 activeChanged 方法,在一级分类选中项改变之后,为二级分类列表数据重新赋值
4.循环渲染右侧二级分类列表的 UI 结构
5.美化二级分类的标题样式

<!-- 右侧的滚动视图区域 -->
<scroll-view class="right-scroll-view" scroll-y :style="{height: wh + 'px'}">
  <view class="cate-lv2" v-for="(item2, i2) in cateLevel2" :key="i2">
    <view class="cate-lv2-title">/ {{item2.cat_name}} /</view>
  </view>
</scroll-view>

data() {
  return {
    // 二级分类列表
    cateLevel2: []
  }
}

methods: {
  // 选中项改变的事件处理函数
  activeChanged(i) {
    this.active = i
    // 为二级分类列表重新赋值
    this.cateLevel2 = this.cateList[i].children
  }
  async getCateList() {
    const { data: res } = await uni.$http.get('/api/public/v1/categories')
    if (res.meta.status !== 200) return uni.$showMsg()
    this.cateList = res.message
    // 为二级分类赋值
    this.cateLevel2 = res.message[0].children
  }
}

.cate-lv2-title {
  font-size: 12px;
  font-weight: bold;
  text-align: center;
  padding: 15px 0;
}

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

动态渲染右侧的三级分类列表

1.在二级分类的 <view> 组件中,循环渲染三级分类的列表结构
2.美化三级分类的样式

<!-- 右侧的滚动视图区域 -->
<scroll-view class="right-scroll-view" scroll-y :style="{height: wh + 'px'}">
  <view class="cate-lv2" v-for="(item2, i2) in cateLevel2" :key="i2">
    <view class="cate-lv2-title">/ {{item2.cat_name}} /</view>
    <!-- 动态渲染三级分类的列表数据 -->
    <view class="cate-lv3-list">
      <!-- 三级分类 Item 项 -->
      <view class="cate-lv3-item" v-for="(item3, i3) in item2.children" :key="i3">
        <!-- 图片 -->
        <image :src="item3.cat_icon"></image>
        <!-- 文本 -->
        <text>{{item3.cat_name}}</text>
      </view>
    </view>
  </view>
</scroll-view>

.cate-lv3-list {
  display: flex;
  flex-wrap: wrap;

  .cate-lv3-item {
    width: 33.33%;
    margin-bottom: 10px;
    display: flex;
    flex-direction: column;
    align-items: center;

    image {
      width: 60px;
      height: 60px;
    }

    text {
      font-size: 12px;
    }
  }
}

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

切换一级分类后重置滚动条的位置

1.在 data 中定义滚动条距离顶部的距离
2.动态为右侧的 <scroll-view> 组件绑定 scroll-top 属性的值
3.切换一级分类时,动态设置 scrollTop 的值

<!-- 右侧的滚动视图区域 -->
<scroll-view class="right-scroll-view" scroll-y :style="{height: wh + 'px'}" :scroll-top="scrollTop"></scroll-view>


data() {
  return {
    // 滚动条距离顶部的距离
    scrollTop: 0
  }
}

methods: {
  // 选中项改变的事件处理函数
  // 选中项改变的事件处理函数
  activeChanged(i) {
    this.active = i
    this.cateLevel2 = this.cateList[i].children

    // 让 scrollTop 的值在 0 与 1 之间切换
    this.scrollTop = this.scrollTop === 0 ? 1 : 0

    // 可以简化为如下的代码:
    // this.scrollTop = this.scrollTop ? 0 : 1
  }
}
点击三级分类跳转到商品列表页面

1.为三级分类的 Item 项绑定点击事件处理函数
2.定义事件处理函数

<view class="cate-lv3-item" v-for="(item3, i3) in item2.children" :key="i3" @click="gotoGoodsList(item3)">
  <image :src="item3.cat_icon"></image>
  <text>{{item3.cat_name}}</text>
</view>


// 点击三级分类项跳转到商品列表页面
gotoGoodsList(item3) {
  uni.navigateTo({
    url: '/subpkg/goods_list/goods_list?cid=' + item3.cat_id
  })
}
  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
前端:微信小程序 采用 uni-app 开发框架,uni-app 是一个使用 Vue.js 开发所有前端应用的框架,开发者编写一套代码,可发布到iOS、Android、H5、以及各种小程序(微信/支付宝/百度/头条/QQ/钉钉)等多个平台。 后端:采用 SpringBoot 2 构建后端服务,才 Swagger2 构建 Restful风格接口文档,数据库采用 Mysql ,使用 Mybatis-Plus 做数据访问层。 语音识别和图像识别 采用 百度智能云平台服务。 安装教程 开发需要准备相关的 IDE和 JDK8 开发环境 , 前端开发 uni-app 需要 下载 Hbuilder ,后端开发 需要下载 Eclipse 或 IDEA。 其中的 语音识别和图像识别 功能使用百度智能云平台服务,需要注册登录拥有自己的应用 (有5万次调用 api 的免费使用量),在 后端 afterend 的 uniapp-api 当中的配置文件里面需要进行配置 appid、apikey、secretkey。 数据库准备,创建一个 名称为 garbageSort 的数据库,把跟目录下 garbagesort.sql数据库文件进行导入进去即可。 后端启动:第一步、在 afterend 目录下找到 uniapp-api 模块,把配置文件 application.yml.example 的 example 后缀去掉,命名为 application.yml。第二步、在 application.yml 中配置好 数据库相关信息 和 上述 百度请求 api 管理的配置信息即可。 第三步、运行 UniappApiApplication ,启动后端服务,可以访问 http://localhost:8899/wx/doc.html 查看接文档 前端启动:在 Hbuilder 中打开目录 frontend ,在运行菜单中 点击 运行到小程序模拟器 —> 微信开发者工具,或者 使用 微信开发者工具 打开 forntend -> unpackage -> dist -> dev -> mp-weixin,即可运行微信小程序。(二次开发的时候在 Hbuilder 中修改文件,保存后可以到微信开发者工具看预览效果)

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值