小程序三级下拉目录菜单

近期需要在微信小程序中实现三级的下拉目录,要求是展开一个收起其他
wxml部分

<view wx:for="{{majorList}}" wx:key="index" class='majorlist'>
    <!-- 一级目录 -->
    <view class='subjectname' data-indexone="{{index}}" bindtap="openTwo">
      <image mode='cover' src='{{libPath}}leftright.png' hidden='{{subtwo == index}}'></image>
      <image class='downimg' mode='cover' src='{{libPath}}down.png' hidden='{{subtwo != index}}'></image>
      <text>{{item.subject}}</text>
    </view>
   <view hidden="{{subtwo != index }}">
  <!--  <view style=" display: {{subtwo == index ? 'block' : 'none' }} "> -->
      <view wx:for="{{item.submajor}}" wx:key="index">
        <!-- 二级目录 -->
        <view class='subname' data-indextwo='{{index}}' bindtap="openThree">
          <image mode='cover' src='{{libPath}}leftright.png' hidden='{{subthree == index}}'></image>
          <image class='downimg' mode='cover' src='{{libPath}}down.png' hidden='{{subthree != index}}'></image>
          <text>{{item.subname}}</text>
        </view>
        <!-- 三级目录 -->
        <view hidden='{{subthree != index}}'>
        <!-- <view style=" display: {{subthree == index ? 'block' : 'none' }} ">-->
          <view wx:for="{{item.major}}" wx:key="index" class='majorname'>{{item}}</view>
        </view>
      </view>
   </view>
</view>

JS 部分

Page({
  /**
   * 页面的初始数据
   */
  data: {
    subtwo: "-1",  // 打开的二级目录
    subthree: "-1",  // 打开的三级目录
    majorList: [
      {
        "subject": "农学",
        "submajor": [
          {
            "subname": "自然",
            "major": [ "植物学", "花草学" ]
          },
          {
            "subname": "社会学",
            "major": ["人文", "历史"]
          }
        ]
      },
      {
        "subject": "医学",
        "submajor": [
          {
            "subname": "临床医学",
            "major": ["动物临床医学", "人体临床医学"]
          }
        ]
      }
    ]
  },
  // 点击一级目录
  openTwo(e) {
    var indexone = e.currentTarget.dataset.indexone
    if (this.data.subtwo == indexone) {
      this.setData({
        subtwo: "-1",
        subthree: "-1"
      })
    } else {
      this.setData({
        subtwo: indexone,
        subthree: "-1"
      })
    }
  },
  // 点击二级目录
  openThree(e) {
    var indextwo = e.currentTarget.dataset.indextwo
    if (this.data.subthree == indextwo) {
      this.setData({
        subthree: "-1"
      })
    } else {
      this.setData({
        subthree: indextwo
      })
    }
  },
})

wxss 部分

.majorlist image {
  width: 12rpx;
  height: 22rpx;
  margin-right: 12rpx;
}
.subjectname {
  height: 116rpx;
  padding-left: 30rpx;
  line-height: 116rpx;
  border-bottom: 1px solid #f6f6f6;
  color: #414141;
  font-size: 34rpx;
}
.subname {
  height: 116rpx;
  padding-left: 60rpx;
  line-height: 116rpx;
  border-bottom: 1px solid #f6f6f6;
  color: #414141;
  font-size: 34rpx;
}
.majorname {
  height: 116rpx;
  padding-left: 60rpx;
  line-height: 116rpx;
  border-bottom: 1px solid #f6f6f6;
  color: #989898;
  font-size: 34rpx;
}
.downimg {
  width: 22rpx!important;
  height: 12rpx!important;
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • create by jsliang on 2018-9-17 17:58:56 Recently revised...
    梁_飘阅读 1,608评论 0 6
  • 1.小程序起步 (1)点击https://mp.weixin.qq.com/wxopen/waregister?a...
    GXW_Lyon阅读 3,438评论 0 0
  • 饭点,有些饿,点了锡纸花甲粉,要了一根烤肠,落座后吃着烤肠,突然感受到对面左手边投来的目光,抬头,发现一个男孩,大...
    ringbowlinlin阅读 198评论 0 0