UNIAPP实战项目笔记20 搜索结果页面排序功能(单击切换上下箭头和颜色显示)

UNIAPP实战项目笔记20 搜索结果页面排序功能(单击切换上下箭头和颜色显示)

关键代码

shopList.vue 代码

组件代码
顶栏换成动态数据遍历
样式控制箭头位置
通过点击事件获取点击的按钮,更改显示的样式

<template>
    <view class="shop-list">
        <view class="shop-title f-color">
            <view class="shop-item"
                v-for="(item,index) in shopList.data"
                :key="index"
                @tap="changeTab(index)"
            >
                <view :class="shopList.currentIndex==index?'f-active-color':''">{{item.name}}</view>
                <view class="shop-icon">
                    <view class="iconfont icon-shangjiantou up"
                        :class="item.status===1?'f-active-color':''"
                    ></view>
                    <view class="iconfont icon-xiajiantou down"
                        :class="item.status===2?'f-active-color':''"
                    ></view>
                </view>
            </view>
        </view>
        <Lines></Lines>
        <CommodityList :dataList="dataList"></CommodityList>
    </view>
</template>

<script>
    import Lines from '@/components/common/Lines.vue'
    import CommodityList from './CommodityList.vue'
    export default {
        data() {
            return {
                shopList:{
                    currentIndex:0,
                    data:[
                        {name:'价格',status:1},
                        {name:'折扣',status:0},
                        {name:'品牌',status:0}
                    ]
                },
               dataList:[
                   {
                       id:1,
                       imgUrl:"../../static/logo.png",
                       name:"迪奥绒毛大衣,今年必抢,错过瞬时不爽了,爆款疯狂销售",
                       pprice:"299",
                       oprice:"659",
                       discount:"5.2"
                   },
                   {
                       id:2,
                       imgUrl:"../../static/logo.png",
                       name:"迪奥绒毛大衣,今年必抢,错过瞬时不爽了,爆款疯狂销售",
                       pprice:"299",
                       oprice:"659",
                       discount:"5.2"
                   },{
                       id:3,
                       imgUrl:"../../static/logo.png",
                       name:"迪奥绒毛大衣,今年必抢,错过瞬时不爽了,爆款疯狂销售",
                       pprice:"299",
                       oprice:"659",
                       discount:"5.2"
                   },
                   {
                       id:4,
                       imgUrl:"../../static/logo.png",
                       name:"迪奥绒毛大衣,今年必抢,错过瞬时不爽了,爆款疯狂销售",
                       pprice:"299",
                       oprice:"659",
                       discount:"5.2"
                   },
               ] 
            };
        },
        components:{
            Lines,
            CommodityList
        },
        methods:{
            // 点击切换颜色
            changeTab(index){
                
                // 索引值
                let idx = this.shopList.currentIndex;
                // 具体哪一个对象
                let item = this.shopList.data[idx];
                if(idx == index){
                    return item.status = item.status === 1 ? 2 : 1
                }
                
                // 新值
                let newItem = this.shopList.data[index];
                item.status = 0; //旧的回复默认
                this.shopList.currentIndex = index;
                newItem.status = 1; // 新的给颜色
            }
        }
    }
</script>

<style lang="scss">
.shop-title{
    display:flex;
}
.shop-item{
    flex:1;
    display: flex;
    justify-content: center;
    align-items: center;
    height: 80rpx;
}
.shop-icon{
    position: relative;
    margin-left: 10rpx;
}
.iconfont{
    width: 16rpx;
    height: 8rpx;
    position: absolute;
    left: 0;
}
.up{
    top: -25rpx;
}
.down{
    top: -5rpx;
}
</style>
page.json 部分代码
{
            "path" : "pages/search-list/search-list",
            "style" :                                                                                    
            {
                "navigationBarBackgroundColor": "#FFFFFF",
                "app-plus": {
                    "scrollIndicator": "none",
                    "titleNView": {
                        "searchInput": {
                            "placeholder": "nike",
                            "disabled": true,
                            "align": "left",
                            "autoFocus": false,
                            "borderRadius": "15px",
                            "backgroundColor": "#F7F7F7",
                            "placeholderColor": "#B3B3B3"
                        },
                        "buttons": [
                            {
                                "float": "right",
                                "color": "#636263",
                                "text": "筛选",
                                "fontSize": "16px",
                                "width":"60px"
                            }
                        ]
                    }
                }
            }
            
        }

目录结构

前端目录结构
  • manifest.json 配置文件: appid、logo…

  • pages.json 配置文件: 导航、 tabbar、 路由

  • main.js vue初始化入口文件

  • App.vue 全局配置:样式、全局监视

  • static 静态资源:图片、字体图标

  • page 页面

    • index
      • index.vue
    • list
      • list.vue
    • my
      • my.vue
    • search
      • search.vue
    • search-list
      • search-list.vue
    • shopcart
      • shopcart.vue
  • components 组件

    • index
      • Banner.vue
      • Hot.vue
      • Icons.vue
      • indexSwiper.vue
      • Recommend.vue
      • Shop.vue
    • common
      • Card.vue
      • Commondity.vue
      • CommondityList.vue
      • Line.vue
      • ShopList.vue
  • common 公共文件:全局css文件 || 全局js文件

    • api
      • request.js
    • common.css
    • uni.css

案例图片

排序功能案例

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的uniapp商品列表示例: 1. 在pages文件夹下新建一个goods文件夹,在该文件夹下新建一个goods.vue文件。 2. 在goods.vue文件中编写商品列表的HTML结构和样式,如下所示: ``` <template> <div class="goods"> <div class="goods-item" v-for="(item,index) in goodsList" :key="index"> <img :src="item.imgUrl" alt=""> <div class="goods-info"> <p class="goods-name">{{item.name}}</p> <p class="goods-price">¥{{item.price}}</p> </div> </div> </div> </template> <style> .goods { display: flex; flex-wrap: wrap; justify-content: space-between; padding: 20px; } .goods-item { width: 45%; margin-bottom: 20px; border: 1px solid #eee; border-radius: 5px; overflow: hidden; } .goods-item img { width: 100%; height: auto; } .goods-info { padding: 10px; } .goods-name { font-size: 16px; margin-bottom: 10px; } .goods-price { color: red; font-size: 18px; } </style> ``` 3. 在data中定义商品列表数据,如下所示: ``` <script> export default { data() { return { goodsList: [ { name: "商品1", price: 100, imgUrl: require("../assets/goods1.jpg"), }, { name: "商品2", price: 200, imgUrl: require("../assets/goods2.jpg"), }, { name: "商品3", price: 300, imgUrl: require("../assets/goods3.jpg"), }, { name: "商品4", price: 400, imgUrl: require("../assets/goods4.jpg"), }, { name: "商品5", price: 500, imgUrl: require("../assets/goods5.jpg"), }, ], }; }, }; </script> ``` 4. 在main.js中引入goods.vue组件,并注册为全局组件。 ``` import Vue from 'vue' import App from './App' import goods from './pages/goods/goods' Vue.config.productionTip = false Vue.component('goods', goods) App.mpType = 'app' const app = new Vue({ ...App }) app.$mount() ``` 5. 在需要显示商品列表的页面中,引入goods组件并使用即可。 ``` <template> <div> <goods></goods> </div> </template> <script> import goods from "@/pages/goods/goods"; export default { components: { goods, }, }; </script> ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值