iView Table组件中嵌入超链接、图片、标签、div盒子、图标


前言

在具体的业务开发中,有时需要在表格组件中嵌入图片、标签等组件来展示数据,应用第三方UI组件库时,通常有略微差异,如element和iView(element直接在元素中嵌入组件即可实现,而iVew需要在Script里设置实现)。element只需要采用作用域插槽即可实现这有效果(element可参考这篇文章),本篇文章为大家介绍iView实现的方式。


一、iView Table组件基本使用

Table组件概述:主要用于展示大量结构化数据,支持排序、筛选、分页、自定义操作、导出 csv 等复杂功能。

以下是Table组件基本使用的代码:

<template>
    <Table :columns="columns1" :data="data1"></Table>
    // columns:表格列的配置描述,具体项见后文
    // data:显示的结构化数据,其中,字段 cellClassName 用于设置任意单元格的样式名称
</template>
<script>
    export default {
        data () {
            return {
                columns1: [
                    {
                        title: 'Name',
                        key: 'name'
                    }
                ],
                data1: [
                    {
                        name: 'John Brown',
                        age: 18,
                        address: 'New York No. 1 Lake Park',
                        date: '2016-10-03'
                    }
                ]
            }
        }
    }
</script>

二、iView Table组件中嵌入超链接、图片、div盒子、标签

效果图如下:
在这里插入图片描述

以下是Table组件中嵌入超链接的代码:

<template>
	<Table border :columns="bgmColumns" :data="bgmList" :loading="loading" height='500'>
	// 使用作用域插槽将每一条数据ID与其索引相互关联
        <template slot-scope="{ row }" slot="id">
          <strong>{{ row.id }}</strong>
        </template>
        // 添加操作栏
        <template slot-scope="{ row, index }" slot="action">
          <Button type="primary" size="small" style="margin-right: 5px" @click="showEditModel(row.id)">编辑</Button>
          <Button type="error" size="small" @click="removeBgmById(row.id)">删除</Button>
        </template>
	</Table>
</template>

<script>
  export default {
    data() {
      return {
        //  columns:表格列的配置描述,此处即为歌曲列表每一列的表头信息以及展示内容的相关配置
        bgmColumns: [
        // 此处为表格最前面的索引值,长度50px,数据居中
        {
            type: 'index',
            width: '50',
            align: 'center'
          },
          {
            title: '歌曲地址',
            key: 'url',
            align: 'center',
            render: (h, params) => {
            // 设置为<a>标签
              return h('a', {
              // 此处为歌曲地址的超链接样式设置(添加下划线)
                style: {
                  textDecoration: 'underline'
                },
                attrs: {
                  href: params.row.url, target: '_blank'
                }
              }, params.row.url)
            }
          },
          {
            title: '唱片图片',
            width: '140',
            key: 'cover',
            align: 'center',
            render: (h, params) => {
            // 设置为<img>
              return h('img', {
              // 此处为歌曲图片样式配置
                style: {
                  'width': '100px',
                  'height': '100px',
                  'border-radius': '50%'
                },
                attrs: {
                  src: params.row.cover
                }
              })
            }
          },
          {
            title: '播放时主题色',
            key: 'theme',
            align: 'center',
            render: (h, params) => {
            // 此处为div盒子样式配置(background的属性值从后台数据中取回,需使用字符串拼接的方式实现)
              return h('div', {
                style: 'width: 100%;height: 100px;' + 'background: ' + params.row.theme,
              })
            }
          },
          {
            title: '标签名称',
            key: 'labelName',
            align: 'center',
            render: (h, params) => {
              let arr = (params.row.labelName || "").split("\n")
              return h('div', arr.map(function(item, index) {
              // 设置为Tag标签
                return h('Tag', {
                  domProps: {
                    innerHTML: item || "无"
                  }
                })
              }))
            }
          },
          // 通过if else ,根据数据的不同,用不同的图标展示性别
          {
            title: '性别',
            key: 'gender',
            align: 'center',
            render: (h, params) => {
              if (params.row.gender == '男') {
                return h('div', [
                  h(
                    'Icon', {
                      props: {
                        type: 'ios-male'
                      }
                    })
                ])
              } else {
                return h('div', [
                  h(
                    'Icon', {
                      props: {
                        type: 'ios-female'
                      }
                    })
                ])
              }
            }
          }
        ]
      }
    }
  }
  </script>

在这里插入图片描述

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

我也想做全栈一霸!

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值