当前位置:   article > 正文

vue2使用element UI中Descriptions组件的遍历问题_el-descriptions-item

el-descriptions-item

需求描述:展示信息时其中部门区域是未知数量的,需要通过遍历进行展示。如下图举例,其中地址和备注是一一对应关系,需遵循该样式。

 问题描述:起初我在el-descriptions中直接使用v-for进行遍历地址和备注两个el-descriptions-item,发现页面毫无反应,不会渲染这部分。

  1. <div v-for="item in arr" :key="item.id">
  2. <el-descriptions-item>
  3. <template slot="label">
  4. <i class="el-icon-location-outline" />地址
  5. </template>
  6. {{item.city}}
  7. </el-descriptions-item>
  8. <el-descriptions-item>
  9. <template slot="label">
  10. <i class="el-icon-tickets" />备注
  11. </template>
  12. <el-tag size="small">{{item.remarks}}</el-tag>
  13. </el-descriptions-item>
  14. </div>

导致原因:打开控制台发现,这个组件是将数据渲染成了一个表格形式,放在里面的div是没有被识别出来。所以更不会遍历渲染。

 处理办法:使用template进行包裹遍历(注意:key要放在真实dom上)

  1. <template v-for="(item,ind) in arr">
  2. <el-descriptions-item :key="ind">
  3. <template slot="label">
  4. <i class="el-icon-location-outline" />地址
  5. </template>
  6. {{item.city}}
  7. </el-descriptions-item>
  8. <el-descriptions-item :key="ind">
  9. <template slot="label">
  10. <i class="el-icon-tickets" />备注
  11. </template>
  12. <el-tag size="small">{{item.remarks}}</el-tag>
  13. </el-descriptions-item>
  14. </template>

 数据: arr: [{ city: '苏州', remarks: '学校' }, { city: '扬州', remarks: '老家' }]


以下为其他思考解决方式(不推荐)

  • 1.     处理数据,不可以通过div遍历但是可以在el-descriptions-item中遍历自身。可以将传过来的数组进行拆分重新组装,之后遍历该数据。

例:[{city:'苏州',remarks:'学校'},{city:'扬州',remarks:'老家'}]   =>   ['苏州','学校','扬州','老家']

  1. //遍历展示
  2. <el-descriptions-item v-for="(item,ind) in arr" :key="ind">
  3. <template slot="label">
  4. <i class="el-icon-location-outline" />
  5. {{ind%2==0?'地址':'备注'}}
  6. </template>
  7. {{item}}
  8. </el-descriptions-item>
  •  2.     拆分展示图表。分成三部分,中间遍历部分用div手写样式。(该样式需要根据需求自行调整)
    1. <div class="departList">
    2. <div v-for="(item,ind) in jointDeparts" :key="ind" class="departItem">
    3. <div class="depart">
    4. <div class="left">地址</div>
    5. <div class="right">{{item.name}}</div>
    6. </div>
    7. <div class="type">
    8. <div class="left">备注</div>
    9. <div class="right">{{item.type}}</div>
    10. </div>
    11. </div>
    12. </div>
    1. .departList {
    2. margin: 5px 0;
    3. width: 98% !important;
    4. margin-left: 2% !important;
    5. border: 1px solid #ebeef5;
    6. .departItem {
    7. display: flex;
    8. div {
    9. display: flex;
    10. width: 50%;
    11. }
    12. .left {
    13. padding: 12px 10px;
    14. color: rgba(0, 0, 0, 0.85);
    15. border-right: 1px solid #e5e6eb;
    16. border-bottom: 1px solid #e5e6eb;
    17. background: #f2f3f5;
    18. width: 30.3%;
    19. font-size: 14px;
    20. }
    21. .right {
    22. padding: 12px 10px;
    23. color: #606266;
    24. border-bottom: 1px solid #e5e6eb;
    25. font-size: 14px;
    26. width: 70%;
    27. }
    28. .type {
    29. .left {
    30. border-left: 1px solid #e5e6eb;
    31. }
    32. }
    33. }

本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/434728
推荐阅读
相关标签
  

闽ICP备14008679号