小程序三級下拉目錄菜單

近期需要在微信小程序中實現(xiàn)三級的下拉目錄,要求是展開一個收起其他
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({
  /**
   * 頁面的初始數(shù)據(jù)
   */
  data: {
    subtwo: "-1",  // 打開的二級目錄
    subthree: "-1",  // 打開的三級目錄
    majorList: [
      {
        "subject": "農(nóng)學(xué)",
        "submajor": [
          {
            "subname": "自然",
            "major": [ "植物學(xué)", "花草學(xué)" ]
          },
          {
            "subname": "社會學(xué)",
            "major": ["人文", "歷史"]
          }
        ]
      },
      {
        "subject": "醫(yī)學(xué)",
        "submajor": [
          {
            "subname": "臨床醫(yī)學(xué)",
            "major": ["動物臨床醫(yī)學(xué)", "人體臨床醫(yī)學(xué)"]
          }
        ]
      }
    ]
  },
  // 點擊一級目錄
  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;
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • create by jsliang on 2018-9-17 17:58:56 Recently revised...
    梁_飄閱讀 1,679評論 0 6
  • 1.小程序起步 (1)點擊https://mp.weixin.qq.com/wxopen/waregister?a...
    GXW_Lyon閱讀 3,591評論 0 0
  • 飯點,有些餓,點了錫紙花甲粉,要了一根烤腸,落座后吃著烤腸,突然感受到對面左手邊投來的目光,抬頭,發(fā)現(xiàn)一個男孩,大...
    ringbowlinlin閱讀 214評論 0 0

友情鏈接更多精彩內(nèi)容