小程序實現(xiàn)分類頁
分類界頁面中,左邊是一級目錄,右邊是一級目錄對應的二級目錄,根據(jù)這個需求,我們數(shù)據(jù)設計的結構是數(shù)組嵌套數(shù)組,第一個數(shù)組包含一級目錄數(shù)據(jù),嵌套的數(shù)組包含的是二級目錄的數(shù)據(jù)。
主要知識:
1.定義本地json文件
2.本地文件引入
3.小程序列表渲染實現(xiàn)
4.解析本地json
定義本地的json數(shù)據(jù)源
該文件在page下面的data文件下面的categroryData.js中
//模擬json數(shù)據(jù)
var categoryJson=[
{
id: 'guowei',
name: '果味',
isChild: true,
children: [
{
child_id: 1,
name: "果味"
}
]
},
{
id: 'shucai',
name: '蔬菜',
isChild: true,
children: [
{
child_id: 1,
name: "蔬菜"
}
]
},
{
id: 'chaohuo',
name: '炒貨',
isChild: true,
children: [
{
child_id: 1,
name: "炒貨"
}
]
},
{
id: 'dianxin',
name: '點心',
isChild: true,
children: [
{
child_id: 1,
name: "點心"
}
]
},
{
id: 'ganguo',
name: '干果',
isChild: false,
children: []
},
{
id: 'clothes',
name: '衣服',
isChild: false,
children: []
},
{
id: 'bag',
name: '包包',
isChild: false,
children: []
},
{
id: 'woman',
name: '女鞋',
isChild: false,
children: []
},
{
id: 'mansport',
name: '男鞋',
isChild: false,
children: []
},
{
id: 'sports',
name: '運動鞋',
isChild: false,
children: []
},
{
id: 'hzp',
name: '化妝品',
isChild: false,
children: []
},
{
id: 'life',
name: '日常用品',
isChild: false,
children: []
},
{
id: 'computer',
name: '電腦',
isChild: false,
children: []
},
{
id: 'phone',
name: '手機',
isChild: false,
children: []
}
]
//導出數(shù)據(jù)
module.exports={
dataList:categoryJson
}
顯示列表的頁面——categroy.wxml文件
<view class="main">
<view class="categroy-left">
<!-- 當前項的id等于item項的id或者當前的下標等于item的下標時,那個就是當前狀態(tài)- -->
<view wx:for="{{category}}" wx:key="index" data-id="{{item.id}}" data-index="{{index}}"
bindtap="switchTab"
class="cate-list {{curIndex === index?'active':''}}">{{item.name}}</view>
</view>
<scroll-view class="categroy-right" scroll-y="{{}}" scroll-into-view="{{toView}}" scroll-with-animation="true">
<view wx:if="{{category[curIndex].isChild}}">
<block wx:for="{{category[curIndex].children}}" wx:for-index wx:key="idx">
<view id="{{item.id}}" class="cate-box">
<view class="cate-title">
<text>{{item.name}}</text>
</view>
</view>
</block>
</view>
<!-- 若無數(shù)據(jù),則顯示暫無數(shù)據(jù) -->
<view class='nodata' wx:else>該分類暫無數(shù)據(jù)</view>
</scroll-view>
</view>
說明:
curIndex === index?'active':'' ,根據(jù)是否和一級目錄index相同,來判斷是否選中文字。相同執(zhí)行.cate-list.active樣式,不相同則執(zhí)行.cate-list樣式。
將本地數(shù)據(jù)引入到列表中——categroy.js文件
//引入本地的json數(shù)據(jù)
var jsonData=require("../../data/categroryData.js")
Page({
data: {
curIndex: 0,
toView: 'guowei'
},
onLoad(){
this.setData({
//jsonData.dataList獲取data文件中categoryData.js中定義的Json數(shù)據(jù),并賦值給category
category: jsonData.dataList
})
},
switchTab(e){
//將獲取到的item的id和數(shù)組的下表值設為當前的id和下標
this.setData({
toView: e.target.dataset.id,
curIndex: e.target.dataset.index
})
}
})
列表樣式——category.wxss文件
.main{
width:100%;
height: 100%;
}
.categroy-left{
float: left;
width: 150rpx;
height: 100%;
overflow-y: auto;
border-right: 1px solid #ddd;
box-sizing: border-box;
}
.categroy-left .cate-list{
height: 90rpx;
line-height: 90rpx;
text-align: center;
border-left: 3px solid #fff;
}
.categroy-left .cate-list.active{
color: #AB956D;
border-color: #AB956D;
}
.categroy-right{
float: right;
width: 600rpx;
height: 100%;
}
.cate-box{
height: 100%;
padding:40rpx;
box-sizing: border-box;
}
.cate-title{
position: relative;
height: 30rpx;
line-height: 30rpx;
padding:30rpx 0 55rpx;
text-align: center;
color: #AB956D;
font-size: 28rpx;
}
.cate-title::before{
position: absolute;
left: 130rpx;
top: 43rpx;
content: '';
width: 70rpx;
height: 4rpx;
background: #AB956D;
}
.cate-title::after{
position: absolute;
right: 130rpx;
top: 43rpx;
content: '';
width: 70rpx;
height: 4rpx;
background: #AB956D;
}
.nodata{
font-size: 14px;
text-align: center;
color: #AB956D;
margin-top: 100px;
}
效果圖

好啦,大功告成!
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
antd designable平臺的組件拖拽功能實現(xiàn)代碼
這篇文章主要介紹了antd designable平臺的組件拖拽功能實現(xiàn)代碼,本文給大家介紹的非常詳細,感興趣的朋友跟隨小編一起看看吧2024-07-07
JavaScript 實現(xiàn) Tab 點擊切換實例代碼
Tab 選項卡切換效果在現(xiàn)如今的網頁中,運用的也是比較多的,包括點擊切換、滑動切換、延遲切換、自動切換等多種效果,在這篇博文里,我們是通過原生 JavaScript 來實現(xiàn) Tab 點擊切換的效果。2017-03-03
javascript實現(xiàn)狀態(tài)欄中文字動態(tài)顯示的方法
這篇文章主要介紹了javascript實現(xiàn)狀態(tài)欄中文字動態(tài)顯示的方法,涉及JavaScript基于時間函數(shù)動態(tài)操作頁面元素屬性的相關技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-10-10

