react antd表格中渲染一张或多张图片

使用antd table中显示一张图片,代码如下:

const columns = [  {
    title: "姓名",
    dataIndex: "name",
    width: 100 ,   // table列定宽  可不设
     fixed: "left"   // 固定列的位置
  },
  {
    title: "联系电话",
    width: 150,
    dataIndex: "phone"
  },
  {
  title:"显示一张图片",
  width:150,
  dataIndex:"img"render:(text)=> <img src={text}/>
  },
  {
  title:"显示多张图片",
  width:400,
  dataIndex:"imgs",
  render: text => {
 // text是后端传的多个url,需要逗号分隔再显示图片
      if (text) {
        return (
          <div style={{ display: "flex" }}>
            {text.split(",").map((items, index) => {
              return (
                <div
                  key={index}
                  className="common-img-list"
                  style={{
                    width: "100px",
                    height: "100px",
                    marginLeft: "4px",
                    overflow: "hidden"
                  }}
                >
                  <img
                    style={{ width: "100%" }}
                    src={items}
                    onClick={() => {
                      InitImageViwer();   // 点击放大图片
                    }}
                  />
                </div>
              );
            })}
          </div>
        );
      }
  },
]

// 点击放大图片预览
function InitImageViwer(
  box = 'common-img-list',   // 注意class不要忘记了
  option = {},
  callBack
) {
  setTimeout(() => {
    const viewList = []
    const el = document.querySelectorAll(`.${box}`)
    if (el.length) {
      el.forEach((z, x) => {
        viewList[x] = new ImageViewer(z, option)
      })
      callBack && callBack(viewList)
    }
  }, 1000)
}

// table 使用  scroll  表格滚动
<Table columns={columns}   scroll={{ x: 1500, y: 500 }} />   

实现效果图:
在这里插入图片描述

点击图片放大预览效果:
在这里插入图片描述

评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值