微信小程序--列表展示

小知识:

wx:for="{{list}}"用来循环数组。

wx:for-item=‘变量名(随便起)’   它是指定循环数据当前的变量名,可以通过  {{变量名.属性}} 展示数组的元素。

wx:for-index=‘变量名(随便起)’,它是指向当前元素的下标名,可以在其他事件中定义自定义事件(data-xxx='{{变量名}}',,该自定义属性可以在参数e下面打印出来)获取该下标。

 思考:二维甚至多维数组应该如何遍历?

例题: 

列表数据如图所示: 

微信小程序--列表展示_第1张图片 

例题采用了scroll-view和swiper组件。代码如下:

new_canteen.js:

// pages/canteen/new_canteen.js
const app = getApp();
Page({
    /** 页面的初始数据*/
    data: {
        navigationSetting:{
            title:'食堂',
            height: app.globalData.navBarHeight,
            paddingTop: app.globalData.statusBarHeight,
            // backgroundColor:'red',
            size:'default'
        },
        tabList: ['一粟堂', '三清园', '七品居','九华厅'],
        tabNow: 0, //当前选中的tab标签索引
        search: "",
        tradeList: [['从前说','千千万万','千千万万','千千万万','千千万万'],['我记得']], //对应tab显示的商家列表
        list:[ //搜索后的list内容列表
            {Name:"一粟堂",arr:[
              {title: '烧卤家族', sale: 358, rating: 4.9, consumption: 15},
              {title: '烧卤家族', sale: 518, rating: 4.9, consumption: 35},
              {title: '烧卤家族', sale: 138, rating: 4.9, consumption: 23},
            ]},
            {Name:"三清园",arr:[
              {title: '汉味小吃', sale: 156, rating: 4.9, consumption: 25},
              {title: '汉味小吃', sale: 324, rating: 4.9, consumption: 17},
            ]},
            {Name:"七品居",arr:[
                {title: '古茗', sale: 156, rating: 4.9, consumption: 25},
                {title: '古茗', sale: 351, rating: 4.9, consumption: 19},
                {title: '古茗', sale: 324, rating: 4.9, consumption: 17},
            ]},
            {Name:"九华厅",arr:[
                {title: '汉味小吃', sale: 156, rating: 4.9, consumption: 25},
            ]}
        ],
    },
    selectTab (e) { //切换不同的tab
        this.setData({
            tabNow: e.currentTarget.dataset.id
        })
    },
    handleSwiperChange(e) { //对应改变tab
        this.setData({
          tabNow: e.detail.current
        })
    },
    getContent(e) {
        console.log("主页:",e)
        this.setData({
          search: e.detail
        })
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})

new_canteen.json:

{
  "usingComponents": {
    "navigation":"../../components/navbar/navbar",
    "search": "/components/search/search"
  },
  "navigationStyle": "custom",
  "navigationBarTextStyle": "black"
}

new_canteen.wxml:







    
        
         
             
                 {{ item }} 
                 
            
        
    
    
    
    
        
            
                
                    
                        
                            
                            
{{map.title}}
{{map.rating}} 月售:{{map.sale}} 人均消费:{{map.consumption}}元/人

new_canteen.wxss:

/* pages/canteen/new_canteen.wxss */
page {
    height: 100%;/*页面高度为屏幕的高度*/
}
.head {
  position: fixed;
    height: 13%; 
    width: 100%;
    color: #333;
    font-size: 30rpx;
    font-weight: bold;
    padding-bottom: 10rpx;
    box-sizing: border-box;
}
.head-title {
    position: relative;
    display: inline-block;
    height: 100%;
}
.head-title::after {
    content: '';
    position: absolute;
    z-index: 99;
    width: 15px;
    height: 15px;
    margin-left: -15rpx;
    border-top: 3px solid #333;
    border-right: 3px solid #333;
    border-top-right-radius: 100%;
    transform: rotate(-225deg);
    left: 50%;
    bottom: 3px;
}
.container {
    width: 100%;
    background-color: #fff;
    overflow: hidden;
    /* border-radius: 30rpx 30rpx 0 0; */
    padding: 0 0;
}
.container-head {
    width: 100%;
    box-sizing: border-box;
  }
.scroll_view {
    height: 80rpx;
    border-radius: 50rpx;
    /* background-color: #eeece4; */
    white-space: nowrap;/*不换行*/
  }
  
.scroll_view .tab {
    /* padding: 0 10rpx; */
    line-height: 65rpx;
    display: inline-block;
    text-align: center;
    height: 80rpx;
    vertical-align: top;/*防止高度塌陷*/
}
/*激活当前tab样式*/
.scroll_view .select {
    /* color: #1067D1; */
    font-size: larger;
    font-weight: bold;
    border-bottom: 18px solid #F6CB48;
} 
.tab .tab_item {
    height: 70rpx;
    display: inline-block;
    line-height: 90rpx;
    margin: 0 26rpx;
    padding: 0 20rpx;
    flex: 1;
}
.container-swiper {
    height: 100%;
    display: flex;
    flex-direction: column;
    flex-flow: column;
}
.container-swiper-sc {
    height: 100%;
}
.container-swiper-sc .items {
    padding: 0 2%;
    width: 100%;
    box-sizing: border-box;
  }
  .container-swiper-sc .items .item-img {
    width: 30vw;
    height: 30vw;
    margin-right: 2.8%;
    margin-bottom: 10rpx;
    flex-shrink: 0;
  }
  .container-swiper-sc .items .item-img:nth-child(3n+3) {
    margin-right: 0;
  }

/* 商家列表展示 */
/*列表展示*/
.mendian-list .mendian-info {
  display: flex;
  padding: 8px;
  margin-top: 16rpx;
  border-radius: 20px;
  /* border-bottom: 1px #dedede dashed; */
  border: 2px solid #A9A9A9;
  /* background-color: #D7EEFC; */
}
.mendian-list .mendian-info:last-child {
  /* border-bottom: none; */
  margin-bottom: 3px;
}
.mendian-list .thumb {
  width: 70px;
  height: 70px;
  flex-shrink: 0;
  margin-right: 15px;
  border-radius: 10px;
  background-color: blue;
}
.mendian-list .mendian-text {
  flex-grow: 1;
  flex-shrink: 1;
  display: flex;
  justify-content: space-between;
  flex-flow: column;
}
.mendian-list .title {
  font-size: 18px;
  font-weight: 600;
  /* text-align: center; */
  color: #484848;
  margin-top: 5px;
}

小图片:

实现效果: 

效果展示

 

你可能感兴趣的:(微信小程序,微信小程序,前端)