关于微信小程序多层嵌套渲染列表以及嵌套列表中数据的获取

原由:意图是想实现相似于安卓viewpager那样的效果,想到了用微信小程序的swiper组件,但若是每一个swiper-item里都只用一个wx:for来渲染数据的话实在是太慢了,就想到多层嵌套数据渲染来提升效率。小程序

直接上代码微信小程序

wxml:数组

<view wx:for="{{one}}" wx:key="id">
  <view>----{{item.name}}----</view>
  <view wx:for="{{item.two}}" wx:for-item="two" wx:key="id">

    <view>----{{two.name}}----</view>
  </view>
  <view wx:for="{{item.three}}" wx:for-item="three" wx:key="id">
    ----{{three.name}}----
  </view>
 -------------------------------------------
</view>

js微信

one: [{
        id: 1,
        name:'第一层',
        two: [{
          'id': 11,
          'name': '第二层第一个数据'
        }, {
          'id': 12,
            'name': '第二层第二个数据'
        }],
        three: [{
          'name': '第三层数据'
        }]
      },
      {
        id: 2,
        name: '第二次渲染第一层',
        two: [{
          'id': 13,
          'name': '第二层第一个数据2'
        }, {
          'id': 14,
            'name': '第二层第二个数据2'
        }],
        three: [{
          'name': '第三层数据'
        }]
      },
      {
        id: 3,
        name: '第三次渲染第一层',
        two: [{
          'id': 15,
          'name': '第二层第一个数据3'
        }, {
          'id': 16,
            'name': '第二层第二个数据3'
        }],
        three: [{
          'name': '第三层数据'
        }]
      }
    ]

实现效果图:this

clipboard.png

若是你想取其中一个数组的某个字段的值,能够用如下写法:spa

var twodata = this.data.one[0].two
    var text = twodata[1].name
    console.log(text)

clipboard.png

相关文章
相关标签/搜索