天天看點

微信小程式分類更高效實作(實戰)

想看效果的去這裡,效果都是一樣的 

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
    })
  }
})      

wxml

<!--主盒子-->
<view class="container">
  <!--左側欄-->
  <view class="nav_left">
    <block wx:for="{{cateItems}}">
      <!--目前項的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}}">
        <view class="nav_right_items">
        <!--界面跳轉 -->
          <navigator url="../../detail/detail}}">
            <image src="{{item.image}}"></image>
            <text>{{item.name}}</text>
          </navigator>
        </view>
      </block>
    </view>
    <!--如果無資料,則顯示資料-->
    <view class="nodata_text" wx:else>該分類暫無資料</view>
  </view>
</view>      

wxss

page{  
  background: #f5f5f5;  
}  
/*總體主盒子*/  
.container {  
  position: relative;  
  width: 100%;  
  height: 100%;  
  background-color: #fff;  
  color: #939393;  
}
 /*左側欄主盒子*/  
.nav_left{  
  /*設定行内塊級元素(沒使用定位)*/  
  display: inline-block;  
  width: 25%;  
  height: 100%;  
  /*主盒子設定背景色為灰色*/  
  background: #f5f5f5;  
  text-align: center;  
}  
/*左側欄list的item*/  
.nav_left .nav_left_items{  
  /*每個高30px*/  
  height: 40px;  
  /*垂直居中*/  
  line-height: 40px;  
  /*再設上下padding增加高度,總高42px*/  
  padding: 6px 0;  
  /*隻設下邊線*/  
  border-bottom: 1px solid #dedede;  
  /*文字14px*/  
  font-size: 14px; 
}  
/*左側欄list的item被選中時*/  
.nav_left .nav_left_items.active{  
  /*背景色變成白色*/  
  background: #fff;  
  color: #f0145a; 
}  
/*右側欄主盒子*/  
.nav_right{  
  /*右側盒子使用了絕對定位*/  
  position: absolute;  
  top: 0;  
  right: 0;  
  flex: 1;  
  /*寬度75%,高度占滿,并使用百分比布局*/  
  width: 75%;  
  height: 1000px;  
  padding: 10px;  
  box-sizing: border-box;  
  background: #fff;  
}  
/*右側欄list的item*/  
.nav_right .nav_right_items{  
  /*浮動向左*/  
  float: left;  
  /*每個item設定寬度是33.33%*/  
  width: 33.33%;  
  height: 120px;  
  text-align: center;  
}  
.nav_right .nav_right_items image{  
  /*被圖檔設定寬高*/  
  width: 60px;  
  height: 60px;  
  margin-top: 15px;  
}  
.nav_right .nav_right_items text{  
  /*給text設成塊級元素*/  
  display: block;  
  margin-top: 15px;  
  font-size: 14px;  
  color: black;
  /*設定文字溢出部分為...*/  
  overflow: hidden;  
  white-space: nowrap;  
  text-overflow: ellipsis;  
} 
.nodata_text
{
  color: black;
  font-size: 14px;  
  text-align: center;  
}