這篇文章將為大家詳細講解有關微信小程序中如何使用動態API把商城項目的商品進行分類,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。
看效果
商品分類.gif
開發計劃
1、商品分類頁面布局
2、調用動態API獲取數據并加載
3、點擊商品分類跳轉相關商品集合
根據商品ID獲取商品詳情API數據模型
訪問:https://100boot.cn/ 選擇微商城案例,如下圖所示:
商品分類API.jpg
下方還有詳細的數據模型可以查看哦!
classify.wxml
<!--主盒子--> <view class="container"> <!--左側欄--> <view class="nav_left"> <block wx:for="{{classifyItems}}"> <!--當前項的id等于item項的id,那個就是當前狀態--> <!--用data-index記錄這個數據在數組的下標位置,使用data-id設置每個item的id值,供打開2級頁面使用--> <view class="nav_left_items {{curNav == item.id ? 'active' : ''}}" bindtap="switchRightTab" data-index="{{index}}" data-id="{{item.id}}"> {{item.name}} </view> </block> </view> <!--右側欄--> <view class="nav_right"> <!--如果有數據,才遍歷項--> <view wx:if="{{classifyItems[curIndex].ishaveChild}}"> <block wx:for="{{classifyItems[curIndex].shopClassifyDtoList}}"> <view class="nav_right_items"> <!--界面跳轉 --> <navigator url="/pages/classifyGoods/classifyGoods?classifyId={{item.id}}"> <image src="{{item.imgUrl}}"></image> <text>{{item.name}}</text> </navigator> </view> </block> </view> <!--如果無數據,則顯示數據--> <view class="nodata_text" wx:else>該分類暫無數據</view> </view> </view>
classify.wxss
/* pages/classify/classify.wxss */ page{ background: #f5f5f5; } /*總體主盒子*/ .container { position: relative; width: 100%; height: 100%; background-color: #fff; color: #939393; } /*左側欄主盒子*/ .nav_left{ /*設置行內塊級元素*/ position: absolute; /* display: inline-block; */ width: 25%; height: 100%; /*主盒子設置背景色為灰色*/ background: #f5f5f5; text-align: center; left: 0; top:0; } /*左側欄list的item*/ .nav_left .nav_left_items{ height: 40px; line-height: 40px; padding: 6px 0; border-bottom: 1px solid #dedede; 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; } .left_cate { display: flex; flex-direction: row; /*每個高30px*/ height: 40px; /*垂直居中*/ line-height: 40px; /*再設上下padding增加高度,總高42px*/ padding: 6px 0; /*只設下邊線*/ border-bottom: 1px solid #dedede; /*文字14px*/ font-size: 14px; background: #fff; color: #f0145a; } .separate { background-color: #f0145a; width: 10rpx; z-index: 10; }
classify.js
const ajax = require('../../utils/ajax.js'); const utils = require('../../utils/util.js'); Page({ /** * 頁面的初始數據 */ data: { classifyItems:[], 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 }) }, /** * 生命周期函數--監聽頁面加載 */ onLoad: function (options) { var that = this; that.classifyShow(); }, classifyShow: function (success) { var that = this; ajax.request({ method: 'GET', url: 'classify/getShopClassifyList?key=' + utils.key, success: data => { that.setData({ classifyItems: data.result }) console.log(data.result) } }) }, })
關于“微信小程序中如何使用動態API把商城項目的商品進行分類”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,使各位可以學到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。