【小程序系列】[02]优购项目 分类 搜索

适应不同型号手机动态绑定可视高度

<scroll-view scroll-y="true"   class="left-scroll-view" :style="{height:wh+'px' }">
		  <block v-for="(item,index) in cateList" :key="index">
        <view :class="['left-scroll-view-item' ,active == index ? 'active' :'']" @click="activeClick(index)">{{item.cat_name}}</view>
      </block>
		</scroll-view>
	data() {
			return {
				wh:0,
        cateList:[],
        active:0
			};
		},



 onLoad() {
     const sysInfo = uni.getSystemInfoSync()
          // 为 wh 窗口可用高度动态赋值
    this.wh = sysInfo.windowHeight
      this.getcateList()
    },

给添加侧边红色边框
在这里插入图片描述

.left-scroll-view-item {
      line-height: 60px;
      background-color: #f7f7f7;
      text-align: center;
      font-size: 12px;

      // 激活项的样式
      &.active {
        background-color: #ffffff;
        position: relative;

        // 渲染激活项左侧的红色指示边线
        &::before {
          content: ' ';
          display: block;
          width: 3px;
          height: 30px;
          background-color: #c00000;
          position: absolute;
          left: 0;
          top: 50%;
          transform: translateY(-50%);
        }
      }
    }

三种class写法

获取2级菜单数据

 async  getcateList(){
      const {data:res} = await uni.$http.get('/api/public/v1/categories')
      if(res.meta.status !== 200) return uni.$showMsg()
      this.cateList = res.message
      
      this.cateLevel2 = res.message[0].children
    },
    activeClick(index){
      this.active = index
      this.cateLevel2 = this.cateList[index].children
    }

切换分类滚动条重置到最顶部

<scroll-view class="right-scroll-view" scroll-y :style="{height: wh + 'px'}" :scroll-top="scrollTop"></scroll-view>


data(){
	 return {
		scrollTop:0
	}
}
 activeClick(index){

      this.scrollTop = this.scrollTop === 0 ? 1 : 0
    }

商品列表详情跳转设置

<view class="cate-lv3-item" v-for="(item3, i3) in item2.children" :key="i3" @click="gotoGoodsList(item3)">
  <image :src="item3.cat_icon"></image>
  <text>{{item3.cat_name}}</text>
</view>
// 点击三级分类项跳转到商品列表页面
gotoGoodsList(item3) {
  uni.navigateTo({
    url: '/subpkg/goods_list/goods_list?cid=' + item3.cat_id
  })
}

创建搜索组件之前,需要创建components
然后创建my-search组件

在这里插入图片描述
然后在需要的地方放入组件

home顶部搜索栏固定效果外面一定要套一个view盒子
在这里插入图片描述
cate分类不需要套盒子
在这里插入图片描述

.search-box {
  // 设置定位效果为“吸顶”
  position: sticky;
  // 吸顶的“位置”
  top: 0;
  // 提高层级,防止被轮播图覆盖
  z-index: 999;
}

在这里插入图片描述

搜索组件@input="input"

 <!-- 使用 uni-ui 提供的搜索组件 -->
	  <uni-search-bar class="search-bar" @input="input" :radius="100" cancelButton="none"></uni-search-bar>
	export default {
		data() {
			return {
				time:null,
        		kw:''
			};
		},
    methods:{
      input(e){
        clearTimeout(this.time)
       this.time =  setTimeout(()=>{
          this.kw = e.value
            console.log(this.kw)
        },500)
      }
    }
	}

搜索建议 发送请求获取数据

<!-- 搜索建议 -->
    <view class="sugg-list">
      <view class="sugg-item" v-for="(item, i) in searchResults" :key="i" @click="gotoDetail(item.goods_id)">
        <view class="goods-name">{{item.goods_name}}</view>
        <uni-icons type="arrowright" size="16"></uni-icons>
      </view>
    </view>

async getSearchList() {
        // 判断搜索关键词是否为空
        if (this.kw.length === 0) {
          this.searchResults = []
          return
        }
        const { data: res } = await uni.$http.get('/api/public/v1/goods/qsearch', {
          query: this.kw
        })
        if (res.meta.status !== 200) return uni.$showMsg()
        this.searchResults = res.message
      },

搜索建议跳转到搜素结果详情页


@click="gotoDetail(item.goods_id)"

 gotoDetail(goods_id){
        uni.navigateTo({
          url:"/subpkg/goods_detail/goods_detail?good_id=" + goods_id
        })
      }
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

不停喝水

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值