分类界面,左边是一级目录,右边是一级目录对应的二级目录,根据这个需求,我们数据设计的结构一定是数组嵌套数组,第一个数组包含一级目录数据,嵌套的数组包含的是二级目录的数据。

wxml代码:

  1. <view class="page">
  2.  
  3. <!--左侧栏-->
  4. <view class="nav_left">
  5. <block wx:for="{{cateItems}}" wx:key="unique">
  6. <!--当前项的id等于item项的id,那个就是当前状态-->
  7. <!--用data-index记录这个数据在数组的下标位置,使用data-id设置每个item的id值,供打开2级页面使用-->
  8. <view class="nav_left_items {{curNav == item.cate_id ? 'active' : ''}}" bindtap="switchRightTab" data-index="{{index}}" data-id="{{item.cate_id}}">{{item.cate_name}}</view>
  9. </block>
  10. </view>
  11. <!--右侧栏-->
  12. <view class="nav_right">
  13. <!--如果有数据,才遍历项-->
  14. <view wx:if="{{cateItems[curIndex].ishaveChild}}">
  15. <block wx:for="{{cateItems[curIndex].children}}" wx:key="unique">
  16. <view class="nav_right_items" data-id="{{item.DetailId}}" bindtap="isDetail">
  17. <image src="{{item.image}}"></image>
  18. <text>{{item.name}}</text>
  19. </view>
  20. </block>
  21. </view>
  22. <!--如果无数据,则显示数据-->
  23. <view class="nodata_text" wx:else>该分类暂无数据</view>
  24. </view>
  25.  
  26. </view>

nav_left_items {{curNav == item.cate_id ? 'active' : ''}} 在classify.js代码中已经说了curNav的作用,就是在这里实现的。

根据是否和一级目录cate_id相同,来判断是否点亮文字。相同执行.nav_left_items.active样式,不相同则执行.nav_left_items样式

wxss:

  1. /*左侧栏主盒子*/
  2. .nav_left{
  3. /*设置行内块级元素(没使用定位)*/
  4. display: inline-block; width: 25%; height: 100%; background: #f5f5f5; text-align: center; }
  5. /*左侧栏list的item*/
  6. .nav_left .nav_left_items{ height: 80rpx;line-height: 80rpx;font-size: 28rpx; border-bottom: 1px solid #dedede; }
  7. /*左侧栏list的item被选中时*/
  8. .nav_left .nav_left_items.active{background: #fff; color: #f0145a; }
  9. /*右侧栏主盒子*/
  10. .nav_right{
  11. /*右侧盒子使用了绝对定位*/
  12. position: absolute; top:;right:; flex:; width: 75%; height: 1000rpx;padding: 10rpx; box-sizing: border-box; background: #fff; }
  13. /*右侧栏list的item*/
  14. .nav_right .nav_right_items{ float: left;width: 33.33%;height: 220rpx;text-align: center; }
  15. .nav_right .nav_right_items image{ width:120rpx; height:120rpx;margin-top: 25rpx;}
  16. .nav_right .nav_right_items text{display: block; margin-top: 25rpx; ont-size: 28rpx; color: black;
  17. /*设置文字溢出部分为...*/overflow: hidden; white-space: nowrap; text-overflow: ellipsis; }
  18. .nodata_text{color: black;font-size: 28rpx; text-align: center; }

js:

  1. Page({
  2.  
  3. /**
  4. * 页面的初始数据
  5. */
  6. data: {
  7. cateItems: [
  8. {
  9. cate_id: 1,
  10. cate_name: "护肤",
  11. ishaveChild: true,
  12. children:
  13. [
  14. {
  15. child_id: 1,
  16. name: '洁面皂',
  17. image: "http://mz.djmall.xmisp.cn/files/logo/20161208/148117972563.jpg"
  18. },
  19. {
  20. child_id: 2,
  21. name: '卸妆',
  22. image: "http://mz.djmall.xmisp.cn/files/logo/20161207/148110444480.jpg"
  23. },
  24. {
  25. child_id: 3,
  26. name: '洁面乳',
  27. image: "http://mz.djmall.xmisp.cn/files/logo/20161208/148117973270.jpg"
  28. },
  29. {
  30. child_id: 4,
  31. name: '面部祛角质',
  32. image: "http://mz.djmall.xmisp.cn/files/logo/20161208/148117981591.jpg"
  33. }
  34. ]
  35. },
  36. {
  37. cate_id: 2,
  38. cate_name: "彩妆",
  39. ishaveChild: true,
  40. children:
  41. [
  42. {
  43. child_id: 1,
  44. name: '气垫bb',
  45. image: "http://mz.djmall.xmisp.cn/files/logo/20161212/14815381301.jpg"
  46. },
  47. {
  48. child_id: 2,
  49. name: '修容/高光',
  50. image: "http://mz.djmall.xmisp.cn/files/logo/20161212/14815381411.jpg"
  51. },
  52. {
  53. child_id: 3,
  54. name: '遮瑕',
  55. image: "http://mz.djmall.xmisp.cn/files/logo/20161212/148153815181.jpg"
  56. },
  57. {
  58. child_id: 4,
  59. name: '腮红',
  60. image: "http://mz.djmall.xmisp.cn/files/logo/20161212/148153815759.jpg"
  61. },
  62. {
  63. child_id: 5,
  64. name: '粉饼',
  65. image: "http://mz.djmall.xmisp.cn/files/logo/20161212/148153816983.jpg"
  66. },
  67. {
  68. child_id: 6,
  69. name: '粉底',
  70. image: "http://mz.djmall.xmisp.cn/files/logo/20161212/148153817721.jpg"
  71. },
  72. {
  73. child_id: 7,
  74. name: '蜜粉/散粉',
  75. image: "http://mz.djmall.xmisp.cn/files/logo/20161212/148153819354.jpg"
  76. },
  77. {
  78. child_id: 8,
  79. name: '隔离霜',
  80. image: "http://mz.djmall.xmisp.cn/files/logo/20161215/148179053369.jpg"
  81. }
  82. ]
  83. },
  84. {
  85. cate_id: 3,
  86. cate_name: "香水/香氛",
  87. ishaveChild: true,
  88. children:
  89. [
  90. {
  91. child_id: 1,
  92. name: '淡香水EDT',
  93. image: "http://mz.djmall.xmisp.cn/files/logo/20161213/14815978910.jpg"
  94. },
  95. {
  96. child_id: 2,
  97. name: '浓香水EDP',
  98. image: "http://mz.djmall.xmisp.cn/files/logo/20161213/148159789883.jpg"
  99. },
  100. {
  101. child_id: 3,
  102. name: '香体走珠',
  103. image: "http://mz.djmall.xmisp.cn/files/logo/20161213/14815979307.jpg"
  104. },
  105. {
  106. child_id: 4,
  107. name: '古龙香水男士的最爱',
  108. image: "http://mz.djmall.xmisp.cn/files/logo/20161213/148159765589.jpg"
  109. }
  110. ]
  111. },
  112. {
  113. cate_id: 4,
  114. cate_name: "个人护理",
  115. ishaveChild: false,
  116. children: []
  117. }
  118. ],
  119. curNav: 1,
  120. curIndex: 0
  121. },
  122.  
  123. //事件处理函数
  124. switchRightTab: function (e) {
  125. // 获取item项的id,和数组的下标值
  126. let id = e.target.dataset.id,
  127. index = parseInt(e.target.dataset.index);
  128. // 把点击到的某一项,设为当前index
  129. this.setData({
  130. curNav: id,
  131. curIndex: index
  132. })
  133. },
  134.  
  135. isDetail:function(e){
  136. console.log(e.currentTarget.dataset.id);
  137. wx.navigateTo({
  138. url: "../Detail/Detail?child_id=" + e.currentTarget.dataset.id
  139. })
  140. },
  141.  
  142. /**
  143. * 生命周期函数--监听页面加载
  144. */
  145. onLoad: function (options) {
  146.  
  147. },
  148.  
  149. })

cateItems 展示的数据
curNav 控制当前那个按钮点亮
curIndex 根据此参数来拿第几个分类的数据
switchRightTab 分类tab事件的处理

cateItems里的数据每一个对象都是一个品类的数据,拿第一个品类护肤来说,

cate_id 识别的id
cate_name 一级分类名称
ishaveChild 判断是否有子集
children 二级目录的数据

微信小程序 左右分类列表的更多相关文章

  1. 微信小程序信息展示列表

    微信小程序信息展示列表 效果展示: 代码展示: wxml <view class="head"> <view class="head_item" ...

  2. 微信小程序开发-新闻列表之新闻列表绑定

    微信小程序开发-新闻列表之新闻列表绑定开发教程: 1.效果图预览 2.准备工作 在拿到效果图后不要先急着去写代码,而是要去分析一下页面的整体结构,用什么方式定位和布局.小程序里建议使用flex布局,因 ...

  3. 微信小程序新闻信息列表展示

    微信小程序信息展示列表 wxml <!-- 轮播图 --> <view class='haibao' bindtap="seeDetail" id="{ ...

  4. 基于微信小程序的用户列表点赞功能

    代码地址如下:http://www.demodashi.com/demo/13997.html 一.前言 (1).适合人群 1.微信小程序开发者 2.前端工程师 3.想入门学习小程序开发的人员 4.想 ...

  5. 图解微信小程序---获取电影列表

    图解微信小程序---获取电影列表 代码笔记 list跳转 第一步:编写前端页面获取相关的电影列表参数(对于显示参数不熟悉,可以先写js,通过console  Log的方式获取我们电影的相关数据字段,后 ...

  6. 微信小程序,我的英雄列表

    最近微信小程序炒得火热,就跟成都的这个房价一样.昨天我也尝试了一下,做了一个自己的英雄列表.今天将自己的制作过程记录于此. 1.下载微信开发者工具 官网链接:https://mp.weixin.qq. ...

  7. 微信小程序 - 展开收缩列表

    代码源自于:微信小程序示例官方 index.wxml <block wx:for-items="{{list}}" wx:key="{{item.id}}" ...

  8. 微信小程序 左右分类滚动列表

    今天需求个类似得到app分类的功能,效果如图: 左右分别滚动,互不干扰,先把简单的布局和样式搭好. <view class="page"> <view class ...

  9. 微信小程序云开发-列表数据分页加载显示

    一.准备工作 1.创建数据库nums,向数据库中导入108条数据 2.修改数据库表nums的权限 二.新建页面ListPaginated 1.wxml文件 <!-- 显示列表数据 --> ...

随机推荐

  1. 理解 shared_ptr实现copy-on-write(COW)

    看muduo库某个生产者消费者的地方,利用shared_ptr有效减少了锁的范围及无用的拷贝,下面来看一看 // reader 消费者, shared_ptr<map<string,int ...

  2. xss之上传文件的xss,绕过csp,预警机制

    xss1.XSS姿势——文件上传XSS https://wooyun.x10sec.org/static/drops/tips-14915.html总结: 1.1.文件名方式,原理:有些文件名可能反应 ...

  3. list 对像排序

    在C#的List操作中,针对List对象集合的排序我们可以使用OrderBy.OrderByDescending.ThenBy.ThenByDescending等方法按照特定的对象属性进行排序,其中O ...

  4. JavaEE企业面试问题之JavaWeb

    2.Javaweb阶段 2.1 Ajax你以前用过么?简单介绍一下 AJAX = 异步 JavaScript 和 XML.    AJAX 是一种用于创建快速动态网页的技术. 通过在后台与服务器进行少 ...

  5. SQL分表

    一.为什么要水平分表?简而言之,当单表数据量过大时,无法对其进行有效的维护,以及查询速度严重变慢时,我们就需要对其时行水平分表. 二.什么时候需要水平分表?在数据库结构的设计中,需要充分考虑后期数据的 ...

  6. jquery 下载

    jquery下载所有版本(实时更新) 摘自:http://www.jq22.com/jquery-info122 插件描述:jquery下载,实时更新jquery1.2到最新3.2.1所有版本下载 j ...

  7. BER

    BER全称Bit Error Ratio,比特出错概率,是衡量通信系统性能的最根本指标. 采用纠错编码,只要纠前BER小于某个门限值(BER容限点),纠错编码后就能实现纠后误码率为零的传输. 一般情况 ...

  8. 彻底搞清楚setState

    setState最常见的问题是,是异步的还是同步的? setState在React.Component中的函数,是同步函数.但是我们调用的时候,不同的传参和不同的调用位置都会导致不同的结果. 从页面看 ...

  9. 020_统计 13:30 到 14:30 所有访问 apache 服务器的请求有多少个

    统计 13:30 到 14:30 所有访问 apache 服务器的请求有多少个 #!/bin/bash#awk 使用-F 选项指定文件内容的分隔符是/或者:#条件判断$7:$8 大于等于 13:30, ...

  10. seq2seq聊天模型(二)——Scheduled Sampling

    使用典型seq2seq模型,得到的结果欠佳,怎么解决 结果欠佳原因在这里 在训练阶段的decoder,是将目标样本["吃","兰州","拉面" ...