layui的表格渲染图片

templet 格式化表格数据

方法一

在需要图片展示的 td 代码渲染的最后,添加 templet : '#id'

用此方法,在某些特殊接口,可能会拿不到数据,建议第二种方法

js - html模板:

    <script type="text/html" id="img">
        <div><img src= url style="width: 30px; height: 30px;"></div>
    </script>

js  -  layui -  table:

cols: [
            [
                 
                {field:'categoryId', title:'ID', width:80, fixed: 'left', unresize: true, sort: true}
                ,{field:'categoryName', width:120, title: '名称'}
                ,{field:'categoryIcon', width:120, title: '图标',templet:'#img'}
                ,{field:'createTime', width:120, title: '时间'}
                ,{fixed: 'right', title:'操作', toolbar: '#barDemo', width:150}
            ]
       ]

方法二

在需要图片展示的 td 代码渲染的最后,添加 templet : 函数名  

用函数的方法,比用script模板申明要灵活很多

js  -  layui -  table:

cols: [
            [
                 
                {field:'categoryId', title:'ID', width:80, fixed: 'left', unresize: true, sort: true}
                ,{field:'categoryName', width:120, title: '名称'}
                ,{field:'categoryIcon', width:120, title: '图标',templet:img}
                ,{field:'createTime', width:120, title: '时间'}
                ,{fixed: 'right', title:'操作', toolbar: '#barDemo', width:150}
            ]
       ]

js - function

// 表格格式化  -  img 渲染
      function img(cellvalue) {
          console.log(cellvalue);
          return "<img src='" + cellvalue.categoryIcon + "' height=\"30\" width=\"30\" alt='icon'/>";
      }

欢迎关注我的公众号:码上学习鸭

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值