当前位置: 首页 > news >正文

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

一、效果:

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

二、代码实现:

关键代码:

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

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

完整代码:

<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 = thisthis.$api.appPlateForm('POST', this.$url.products_cate_list, '', function(res) {that.cateList = res.data  //获取分类数据if (that.TabIndex == 0) { //默认选中第一个分类下的数据that.cate_id = that.cateList[0].idthat.getListFun(that.cate_id)}})},//2.列表接口	getListFun(id) {var that = thisvar 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 = thisthat.TabIndex = index;that.cate_id = item.idvar 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>

在这里插入图片描述

http://www.lryc.cn/news/130722.html

相关文章:

  • 小程序-uni-app:hbuildx uni-app 安装 uni-icons 及使用
  • PHP中in_array()函数用法详解
  • 热电联产在综合能源系统中的选址定容研究(matlab代码)
  • 校园网安全风险分析
  • kafka--kafka的基本概念-topic和partition
  • 【LVS】3、LVS+Keepalived群集
  • 对前端PWA应用的部分理解和基础Demo
  • CSGO饰品价格会一直下跌吗?市场何时止跌回升?
  • 线程池原理
  • 拷贝构造函数
  • 数据库: MySQL安装部署、主从
  • Java IO流(二)IO模型(BIO|NIO|AIO)
  • java版本spring cloud 企业工程系统管理 工程项目管理系统源码em
  • 飞天使-k8s简单搭建
  • java中把一个list转tree的方法
  • QT设置widget背景图片
  • 【ROS】话题通信--从理论介绍到模型实现(C++)
  • 服务器数据恢复-EqualLogic存储RAID5数据恢复案例
  • qsort函数详解
  • C#学习,委托,事件,泛型,匿名方法
  • 2023最新版本~KEIL5使用C++开发STM32
  • 汽车领域专业术语
  • H3C交换机如何配置本地端口镜像并在PC上使用Wireshake抓包
  • 零基础自学:2023 年的今天,请谨慎进入网络安全行业
  • 向gitee推送代码
  • 双指针算法实例1(移动零)
  • C#程序随系统启动例子 - 开源研究系列文章
  • 最全攻略之人工智能顶会论文发表
  • Redis基于内存的key-value结构化NOSQL(非关系型)数据库
  • Spring学习笔记+SpringMvc+SpringBoot学习笔记