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

wxml代码:

<view class="page">

  <!--左侧栏-->
<view class="nav_left">
<block wx:for="{{cateItems}}" wx:key="unique">
<!--当前项的id等于item项的id,那个就是当前状态-->
<!--用data-index记录这个数据在数组的下标位置,使用data-id设置每个item的id值,供打开2级页面使用-->
<view class="nav_left_items {{curNav == item.cate_id ? 'active' : ''}}" bindtap="switchRightTab" data-index="{{index}}" data-id="{{item.cate_id}}">{{item.cate_name}}</view>
</block>
</view>
<!--右侧栏-->
<view class="nav_right">
<!--如果有数据,才遍历项-->
<view wx:if="{{cateItems[curIndex].ishaveChild}}">
<block wx:for="{{cateItems[curIndex].children}}" wx:key="unique">
<view class="nav_right_items" data-id="{{item.DetailId}}" bindtap="isDetail">
<image src="{{item.image}}"></image>
<text>{{item.name}}</text>
</view>
</block>
</view>
<!--如果无数据,则显示数据-->
<view class="nodata_text" wx:else>该分类暂无数据</view>
</view> </view>

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

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

wxss:

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

js:

Page({

  /**
* 页面的初始数据
*/
data: {
cateItems: [
{
cate_id: 1,
cate_name: "护肤",
ishaveChild: true,
children:
[
{
child_id: 1,
name: '洁面皂',
image: "http://mz.djmall.xmisp.cn/files/logo/20161208/148117972563.jpg"
},
{
child_id: 2,
name: '卸妆',
image: "http://mz.djmall.xmisp.cn/files/logo/20161207/148110444480.jpg"
},
{
child_id: 3,
name: '洁面乳',
image: "http://mz.djmall.xmisp.cn/files/logo/20161208/148117973270.jpg"
},
{
child_id: 4,
name: '面部祛角质',
image: "http://mz.djmall.xmisp.cn/files/logo/20161208/148117981591.jpg"
}
]
},
{
cate_id: 2,
cate_name: "彩妆",
ishaveChild: true,
children:
[
{
child_id: 1,
name: '气垫bb',
image: "http://mz.djmall.xmisp.cn/files/logo/20161212/14815381301.jpg"
},
{
child_id: 2,
name: '修容/高光',
image: "http://mz.djmall.xmisp.cn/files/logo/20161212/14815381411.jpg"
},
{
child_id: 3,
name: '遮瑕',
image: "http://mz.djmall.xmisp.cn/files/logo/20161212/148153815181.jpg"
},
{
child_id: 4,
name: '腮红',
image: "http://mz.djmall.xmisp.cn/files/logo/20161212/148153815759.jpg"
},
{
child_id: 5,
name: '粉饼',
image: "http://mz.djmall.xmisp.cn/files/logo/20161212/148153816983.jpg"
},
{
child_id: 6,
name: '粉底',
image: "http://mz.djmall.xmisp.cn/files/logo/20161212/148153817721.jpg"
},
{
child_id: 7,
name: '蜜粉/散粉',
image: "http://mz.djmall.xmisp.cn/files/logo/20161212/148153819354.jpg"
},
{
child_id: 8,
name: '隔离霜',
image: "http://mz.djmall.xmisp.cn/files/logo/20161215/148179053369.jpg"
}
]
},
{
cate_id: 3,
cate_name: "香水/香氛",
ishaveChild: true,
children:
[
{
child_id: 1,
name: '淡香水EDT',
image: "http://mz.djmall.xmisp.cn/files/logo/20161213/14815978910.jpg"
},
{
child_id: 2,
name: '浓香水EDP',
image: "http://mz.djmall.xmisp.cn/files/logo/20161213/148159789883.jpg"
},
{
child_id: 3,
name: '香体走珠',
image: "http://mz.djmall.xmisp.cn/files/logo/20161213/14815979307.jpg"
},
{
child_id: 4,
name: '古龙香水男士的最爱',
image: "http://mz.djmall.xmisp.cn/files/logo/20161213/148159765589.jpg"
}
]
},
{
cate_id: 4,
cate_name: "个人护理",
ishaveChild: false,
children: []
}
],
curNav: 1,
curIndex: 0
}, //事件处理函数
switchRightTab: function (e) {
// 获取item项的id,和数组的下标值
let id = e.target.dataset.id,
index = parseInt(e.target.dataset.index);
// 把点击到的某一项,设为当前index
this.setData({
curNav: id,
curIndex: index
})
}, isDetail:function(e){
console.log(e.currentTarget.dataset.id);
wx.navigateTo({
url: "../Detail/Detail?child_id=" + e.currentTarget.dataset.id
})
}, /**
* 生命周期函数--监听页面加载
*/
onLoad: function (options) { }, })

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. 关于反编译pyc的一点技巧

    现在最流行的是用 https://github.com/rocky/python-uncompyle6 但是有些python小版本不一样,比如2.7.6的某版本,开头的magic number在这个项 ...

  2. nginx中ngx_http_core_module模块

    http核⼼心模块指令:套接字相关的配置3.1 server{ }设置虚拟服务器器的配置Syntax: server { ... }Default: —Context: httpserver {lis ...

  3. MNIST 数据集介绍

    在学习机器学习的时候,首要的任务的就是准备一份通用的数据集,方便与其他的算法进行比较. MNIST数据集是一个手写数字数据集,每一张图片都是0到9中的单个数字,比如下面几个:     MNIST数据库 ...

  4. About IndexDB

    http://blog.csdn.net/bd_zengxinxin/article/details/7758317 HTML5 - Storage 客户端存储 http://html5demos.t ...

  5. 鬼子进村 fhq-treap

    鬼子进村 fhq-treap 题面 观察题目发现可用平衡树做:每次鬼子拆家即从平衡树中加入被拆的节点:每次村民修房子都向平衡树中删除该节点:每次查询时,只需要求出其后驱与前驱,易知nxt-pre-1为 ...

  6. Asp.Net跨平台 Jexus 5.8.1 独立版

    在Linux上运行ASP.NET网站或WebApi的传统步骤是,先安装libgdiplus,再安装mono,然后安装Jexus.在这个过程中,虽然安装Jexus是挺简便的一件事,但是安装mono就相对 ...

  7. 省市县数据(txt)

    下载地址:https://pan.baidu.com/s/1x1uR9KxzlxAbn5bgN6ZfrQ 提取码:ky8z

  8. postgre-插入数据时的单引号问题

    场景: 将一个HTML页面存储到数据库中 问题: HTML页面中既包含单引号也包含双引号 解决办法: 双单引号 INSERT INTO table VALUES ('<html><s ...

  9. 从TCP到Socket,彻底理解网络编程是怎么回事

    进行程序开发的同学,无论Web前端开发.Web后端开发,还是搜索引擎和大数据,几乎所有的开发领域都会涉及到网络编程.比如我们进行Web服务端开发,除了Web协议本身依赖网络外,通常还需要连接数据库,而 ...

  10. Java实现单例的5种方式

    1. 什么是单例模式 单例模式指的是在应用整个生命周期内只能存在一个实例.单例模式是一种被广泛使用的设计模式.他有很多好处,能够避免实例对象的重复创建,减少创建实例的系统开销,节省内存. 2. 单例模 ...