uniapp开发小程序-有分类和列表时,进入页面默认选中第一个分类

一、效果:

如下图所示,进入该页面后,默认选中第一个分类,以及第一个分类下的列表数据。

二、代码实现:

关键代码:

进入页面时,默认调用分类的接口,在分类接口里做判断:

c 复制代码
if (that.TabIndex == 0) { //默认选中第一个分类下的数据
	that.cate_id = that.cateList[0].id
	that.getListFun(that.cate_id)
}

完整代码:

html 复制代码
<template>
	<view class="">
		//1分类展示
		<scroll-view :scroll-x="true" class="scrollview-box">
			<block v-for="(item,index) in cateList" :key="item">
				<view :class="TabIndex==index?'item active':'item'" @click="tabclick(index,item)">
					{{item.name}}
				</view>
			</block>
		</scroll-view>
		
		//2列表展示
		<view class="invenlist" v-if="storeList.length>0">
			<view class="invenitem" v-for="(item,index) in storeList" :key="index">
				<view>{{item.name}}</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				//分类
				cateList: [],
				cate_id:'',
				TabIndex: 0,//默认第一个分类

				//列表数据
				storeList: [],
			}
		},
		onShow() {
		 	//进入页面,默认调用分类接口
			this.getCateFun() 
		},
		methods: {
			//1.分类接口			
			getCateFun() {
				var that = this
				this.$api.appPlateForm('POST', this.$url.products_cate_list, '', function(res) {
					that.cateList = res.data  //获取分类数据
					
					if (that.TabIndex == 0) { //默认选中第一个分类下的数据
						that.cate_id = that.cateList[0].id
						that.getListFun(that.cate_id)
					}
				})
			},
			
			//2.列表接口	
			getListFun(id) {
				var that = this
				var data = {
					cate_id:id
				}
				this.$api.appPlateForm('POST', this.$url.products_store_list, data, function(res) {
					that.storeList = res.data   //获取列表数据
				})
			},
			// tab切换
			tabclick(index,item) {
				var that = this
				that.TabIndex = index;
				that.cate_id = item.id
				var data = {
					cate_id:that.cate_id
				}
				//获取到id,调用列表接口
				this.$api.appPlateForm('POST', this.$url.products_store_list, data, function(res) {
					that.storeList = res.data   //获取列表数据
				})
				
			},
		}
	}
</script>

<style scoped lang="less">
	.scrollview-box {
		white-space: nowrap;
		/* 滚动必须加的属性 */
		width: 100%;
		padding:0 20rpx;
		box-sizing: border-box;
		.item {
			width: 180rpx;height:50rpx;
			margin-right: 20rpx;
			display: inline-flex;
			/* item的外层定义成行内元素才可进行滚动 inline-block / inline-flex 均可 */
			flex-direction: column;
			align-items: center;
			font-size: 28rpx;
			color: #666666;
			position: relative;
		}
		view::after{
			display: block;
			clear: both;
			content: '';
			position: absolute;
			bottom: 0;
			left: 0;
			right: 0;
			margin: auto;
			height: 4rpx;
			width: 145rpx;
			background-color:  #21CD81;
			display: none;
		}
		
		.active {
			font-weight: bold !important;
			color: #21CD81 !important;
		}
		.active::after{
			display: block;
		}
	
	}
</style>
相关推荐
七个昵称1 小时前
H5扫描识别条形码
uni-app
QuantumLeap丶2 小时前
《uni-app跨平台开发完全指南》- 03 - Vue.js基础入门
前端·vue.js·uni-app
一 乐3 小时前
个人理财系统|基于java+小程序+APP的个人理财系统设计与实现(源码+数据库+文档)
java·前端·数据库·vue.js·后端·小程序
你算哪颗溜溜梅5 小时前
uni.scanCode vs MpaasScan:支付宝扫码识别赢麻了,保姆级教程来咯~
javascript·uni-app
一只小白菜~6 小时前
记录一下微信小程序里使用SSE
微信小程序·小程序·sse·小程序sse·小程序eventsource
小小王app小程序开发6 小时前
家政小程序拓展分析:从工具型产品到全链路服务生态的技术落地与商业破局
小程序
上海云盾安全满满6 小时前
APP小程序被攻击了该如何应对
网络·小程序
不爱说话郭德纲7 小时前
UniappX不会运行到鸿蒙?超超超保姆级鸿蒙开发生成证书以及配置证书步骤
前端·uni-app·harmonyos
2501_915921438 小时前
iOS 虚拟位置设置实战,多工具协同打造精准调试与场景模拟环境
android·ios·小程序·https·uni-app·iphone·webview
2501_916008899 小时前
App 上架需要什么?从开发者账号到开心上架(Appuploader)免 Mac 上传的完整流程指南
macos·ios·小程序·uni-app·objective-c·cocoa·iphone