其他分享
首页 > 其他分享> > 简易实现-vuejs数据超出单行显示更多,点击展开剩余数据

简易实现-vuejs数据超出单行显示更多,点击展开剩余数据

作者:互联网

## 默认展示一行,点击箭头展开更多,设置最大高度,超出显示滚动条

字段解释:dataList为后台接口请求的选项数据,showAll是否展示全部(类型数组,长度跟dataList相等,属性默认false)

<i>标签收起展开按钮,点击事件传入行的index,标记点击了哪一行进行展开,showAll需要整体替换才能触发vue的数据响应式

##样式部分

字体行高36,默认数据行高36,overflow:hidden隐藏,点击展开设为height:auto

<template>
  <div>
    <el-row v-for="(item,index) in dataList" :key="index">
      <el-col :span="1">{{item.name}}</el-col>
      <el-col :span="21" class="items" :class="[showAll[index]?'show':'hide']">
        <span v-for="(subItem,index1) in item.contents" :key="index1" class="item">{{subItem}}</span>
      </el-col>
      <el-col :span="1">多选</el-col>
      <el-col :span="1">
        <i class="el-icon-arrow-down" @click="showItem(index)"></i>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import {mallGoodsAttributeList} from '@/api/goods'
export default {
  name: 'GoodsFilter',

  data () {
    return {
      dataList: [],
      showAll: []
    }
  },

  mounted () {
    this.getList()
  },

  methods: {
    getList () {
      mallGoodsAttributeList().then(res => {
        res.data.forEach(item => {
          item.contents = item.content.split(',')
          this.showAll.push(false)
        })
        this.dataList = res.data
        console.log(this.dataList)
      })
    },
    showItem (index) {
      this.showAll.splice(index, 1, !this.showAll[index])
    }
  }
}
</script>

<style lang="scss" scoped>
.el-col{
  line-height: 36px;
}
.items{
  display: flex;
  flex-wrap: wrap;
  max-height: 260px;
  &.hide{
    height: 36px;
    overflow: hidden;
  }
  &.show{
    height: auto;
    overflow-y: scroll;
  }
}
.item{
  margin-right: 10px;
}
</style>

  

 

标签:index,item,vuejs,dataList,height,单行,点击,showAll,数据
来源: https://www.cnblogs.com/liangtang/p/16288596.html