小程序?qū)崿F(xiàn)分頁查詢列表的模板
更新時間:2022年08月24日 11:08:17 作者:一際天涯
這篇文章主要為大家詳細(xì)介紹了小程序?qū)崿F(xiàn)分頁查詢列表的模板,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了小程序?qū)崿F(xiàn)分頁查詢列表的模板,供大家參考,具體內(nèi)容如下
list.wxml
<view class="home-main"> ?? ?<!-- 搜索 --> ?? ?<view class="search-bar"> ?? ??? ?<view class="search-bar-form"> ?? ??? ??? ?<image class="search-img" src="/images/search-icon.png"></image> ?? ??? ??? ?<input class="search-input" type="text" placeholder="搜索圖片、文章、視頻" confirm-type="search"></input> ?? ??? ?</view> ?? ?</view> ?? ?<!-- 列表 --> ?? ?<view class="classify-list-all"> ?? ??? ?<view wx:for="{{list}}" wx:key="id" data-item='{{item}}' class="classify-list flex align-center" bindtap="goClassify"> ?? ??? ??? ?<image class="classify-list-image" src="{{item.logo}}"></image> ?? ??? ??? ?<view class="classify-list-main"> ?? ??? ??? ??? ?{{item.name}} ?? ??? ??? ?</view> ?? ??? ?</view> ?? ?</view> </view>
list.js
import Api from "../../../config/api"; import Http from "../../../utils/http"; Page({ ? data: { ? ? formData: { ? ? ? size: 10,//分頁,一頁10條 ? ? ? current: 1,//當(dāng)前頁數(shù) ? ? }, ? ? isLast: false,//是否是最后一頁 ? ? list: [],//列表數(shù)組 ? }, ? onLoad() { ? ? //首次請求 ? ? this.queryListPage(); ? }, ? onPullDownRefresh() { ? ? //下拉刷新 ? ? this.setData({ 'formData.current': 1 }); ? ? this.queryListPage(); ? }, ? onReachBottom() { ? ?? ?//頁面上拉觸底事件的處理函數(shù) ? ? this.queryListPage(); ? }, ? goClassify(e) { ? ? wx.navigateTo({ ? ? ? url: `/pages/home/classify/classify?id=${e.currentTarget.dataset.item.id}`, ? ? }) ? }, ? queryListPage() { ? ?? ?//請求列表 ? ? if (this.data.isLast) { ? ? ? return; ? ? }; ? ? Http.request(Api.productQueryMyPage, this.data.formData, 'GET', true).then(res => { ? ? ? let arr = res.data || []; ? ? ? if (arr && arr.length > 0) { ? ? ? ? arr = arr.map(item => { ? ? ? ? ?//需要處理列表 ? ? ? ? ?item.name = item.name + '處理后數(shù)據(jù)'; ? ? ? ? ? return item; ? ? ? ? }); ? ? ? } else { ? ? ? ? this.setData({ ? ? ? ? ? isLast: true, ? ? ? ? }); ? ? ? } ? ? ? let list = this.data.formData.current === 1 ? arr : this.data.list.concat(arr); ? ? ? let current = this.data.formData.current + 1; ? ? ? this.setData({ ? ? ? ? list, ? ? ? ? 'formData.current': current ? ? ? }); ? ? }); ? }, })
api.js
export default { ? /******* 商品信息 *******/ ? productQueryMyPage: '/product/queryMyPage',//查詢我的商品列表 }
http.js這個簡單的封裝的一下先湊合用,還不太完善
// import Api from "../config/api"; import Config from "../config/config"; function checkCode(res) { ? //401token過期 403表示這個接口是需要登錄的。你沒有權(quán)限訪問 ? if ([401, 403].includes(res.statusCode)) { ? ? wx.removeStorage({ ? ? ? key: 'token', ? ? ? success() {? ? ? ? ? wx.switchTab({ ? ? ? ? ? url: '/pages/my/my-main/my-main' ? ? ? ? }); ? ? ? } ? ? }) ? } } const http = { ? request(url, data, method, needLogin) { ? ? let header = { ? ? ? 'content-type': 'application/json' // 默認(rèn)值 ? ? }; ? ? if (needLogin) { ? ? ? const token = wx.getStorageSync('token'); ? ? ? if (token) { ? ? ? ? header['Authorization'] = 'Bearer ' + token; ? ? ? } ? ? }; ? ? return new Promise((resolve, reject) => { ? ? ? wx.request({ ? ? ? ? url: Config.domain + url, ? ? ? ? data, ? ? ? ? method, ? ? ? ? header, ? ? ? ? success(res) { ? ? ? ? ? console.log(res); ? ? ? ? ? console.log(res.data); ? ? ? ? ? checkCode(res); ? ? ? ? ? resolve(res.data); ? ? ? ? }, ? ? ? ? fail(res) { ? ? ? ? ? reject(res); ? ? ? ? } ? ? ? }) ? ? }) ? }, ? uploadFile(url, filePath, formData, needLogin) { ? ? let header: any = { ? ? ? 'content-type': 'multipart/form-data' // 默認(rèn)值 ? ? }; ? ? if (needLogin) { ? ? ? const token = wx.getStorageSync('token'); ? ? ? if (token) { ? ? ? ? header['Authorization'] = 'Bearer ' + token; ? ? ? } ? ? }; ? ? return new Promise((resolve: any, reject: any) => { ? ? ? wx.uploadFile({ ? ? ? ? url: Config.domain + url, ? ? ? ? filePath, ? ? ? ? name: 'files', ? ? ? ? formData, ? ? ? ? header, ? ? ? ? success(res) { ? ? ? ? ? debugger ? ? ? ? ? console.log(res); ? ? ? ? ? console.log(res.data); ? ? ? ? ? checkCode(res.statusCode); ? ? ? ? ? resolve(JSON.parse(res.data)); ? ? ? ? }, ? ? ? ? fail(res) { ? ? ? ? ? reject(res); ? ? ? ? } ? ? ? }) ? ? }) ? }, }; export default http;
config.js
export default { ? domain: 'http://www.test.com', }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
js字符串截取函數(shù)substr substring slice使用對比
字符串截取函數(shù)有substr、substring以及slice等等,下面將為大家介紹下各自的使用,感興趣的朋友可以了解下2013-11-11學(xué)習(xí)JavaScript設(shè)計模式之享元模式
這篇文章主要為大家介紹了JavaScript設(shè)計模式中的享元模式,對JavaScript設(shè)計模式感興趣的小伙伴們可以參考一下2016-01-01JS去除重復(fù)并統(tǒng)計數(shù)量的實現(xiàn)方法
js去除重復(fù)并統(tǒng)計數(shù)量方法,首先點擊按鈕觸發(fā)事件,然后用class選擇器,迭代要獲取的文本(這里最好用text()方法)加入到Array()集合里。具體操作方法,大家通過本文學(xué)習(xí)下吧2016-12-12