zoukankan      html  css  js  c++  java
  • 微信小程序 左右分类列表

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

    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: 0;right: 0; flex: 1; 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 二级目录的数据

  • 相关阅读:
    快速排序算法
    HTTP中的重定向和请求转发的区别
    单链表的逆置(头插法和就地逆置)
    水仙花数学习
    构建n位元的格雷码
    算法学习day01 栈和队列
    数据结构学习总结 线性表之双向链表
    设计模式
    Nginx 初步认识
    数据结构学习总结(2) 线性表之单链表
  • 原文地址:https://www.cnblogs.com/joe235/p/11454114.html
Copyright © 2011-2022 走看看