使用elementui中el-collapse折叠面板 实现下拉菜单

<template> 
        <div class="left-factor-library-name">
          <div class="left-tree">
            <div class="tree-top">表名称</div>
            <div class="sub-input">
              <el-input clearable
                        placeholder="搜索"
                        @input="factorNameSearch()"
                        v-model="factorName"
                        size="small"
                        clear="el-inp" />
            </div>
            <div class="tree-content"
                 v-if="dataTree&&dataTree.length>0">
              <!-- <el-collapse v-model="activeName"
                           accordion
                           v-for="(item,index) in dataTree"
                           :key="index">
                <el-tooltip class="item"
                            effect="dark"
                            :content="item.name"
                            placement="right">
                  <div @click="menuClick(item)"
                       :class="[activeIndex === item.id ? 'bottom-tabs-active' : '','collapse-items']"
                       style="line-height:30px;">
                    {{ item.name }}
                  </div>
                </el-tooltip>
              </el-collapse> -->
              <el-collapse v-model="activeName"
                           accordion
                           v-for="(item,index) in factorList"
                           :key="index">
                <el-collapse-item :title="item.typeName"
                                  :name="item.type"
                                  v-if="item.data&&item.data.length>0">
                  <div v-for="(i,inx) in item.data"
                       :key="inx">
                    <el-tooltip class="item"
                                effect="dark"
                                :content="i.name"
                                placement="right">
                      <div @click="menuClick(i)"
                           :class="[activeIndex === i.id ? 'bottom-tabs-active' : '','collapse-items']"
                           style="line-height:30px;">
                        {{ i.name }}
                      </div>
                    </el-tooltip>
                  </div>
                </el-collapse-item>
              </el-collapse>
            </div>
          </div>
        </div>
</template>

<script>
export default {
  data: function () {
    return {
      dataTree: [],
      activeIndex: '', // 数据名称
      activeName: ''
    }
  },
  mounted () {
    this.getMainUnit()
  },
  methods: {
    // 获取左侧的菜单
    getMainUnit () {
      this.$http.get(``).then(({ data: res }) => {
        if (res.code !== 0) {
          return this.$message.error(res.msg)
        }
        // console.log(res.data)
        // this.dataTree = res.data
        this.activeIndex = res.data[0].id
 
        // res.data返回的是数组对象形式,需要进行修改。根据不同type划分新的数组对象
        if (res.data && res.data.length > 0) {
          // res.data.map((i) => {
          //   console.log(i)
          // })
          // 遍历数组
          for (var i = 0; i < res.data.length; i++) {
            // 判断新定义的数组有没有值  没有值则减第一个类给他赋值上去
            if (this.dataTree.length === 0) {
              this.dataTree.push({
                type: res.data[i].type,
                typeName: res.data[i].typeName,
                data: [res.data[i]]
              })
            } else { // 第一个完后 后面的有值了 使用findindex()方法进行比较 存在返回下标index 不存在则返回-1
              var index = this.dataTree.findIndex(item => {
                return item.type === res.data[i].type
              })
              // 判断index是否大于0 大于0则在该类的data push进该类别的数据
              if (index >= 0) {
                this.dataTree[index].data.push(res.data[i])
              } else { // 不存在则新建一个对象  push 进arr2 里面 作为第二个类
                this.dataTree.push({
                  type: res.data[i].type,
                  typeName: res.data[i].typeName,
                  data: [res.data[i]]
                })
              }
            }
          }
          // console.log(this.dataTree)
        }

      })
    },
    // 点击获取分页
    menuClick (id) {
      // console.log(id, typeof id)
      this.activeIndex = id // 改变样式
      // console.log(this.activeIndex, this.activeName)
    }
  }
}
</script>

<style lang="scss" scoped>

  .left-factor-library-name {
    width: 20%;

    .left-tree {
      width: 100%;
      height: 100vh;
      border: 1px solid rgb(222, 220, 220);
      // margin-right: 20px;
      border-radius: 4px;
      .tree-top {
        padding: 10px 20px;
        border-bottom: 1px solid rgb(222, 220, 220);
      }

      .sub-input {
        // background-color: #f0f0f0;
        height: 46px;
        display: flex;
        justify-content: space-around;
        align-items: center;
        /deep/ .el-input {
          width: 90%;
        }
        // /deep/ .el-button{
        //   width: 28%;
        // }
      }
      .tree-content {
        // overflow-y: scroll;
        // padding: 10px 20px;
        .el-collapse {
          padding: 0px 14px;
        }
        .el-collapse-item__content {
          padding-bottom: 0;
        }
        .bottom-tabs-active {
          line-height: 30px;
          background-color: #e7f7f1;
          color: #21bb7e;
        }

        .collapse-items {
          cursor: pointer;
          line-height: 30px;
          height: 30px;
          border-bottom: 1px solid rgb(222, 220, 220);

          // 单行文字超出换行
          overflow: hidden;
          text-overflow: ellipsis;
          white-space: nowrap;
        }
      }
    }
  }
</style>

 关于数据的处理

后端返的是

需要根据type分组 改为

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值