ant-design框架table中插入图片(已解决)

文章讲述了在AntDesign的table组件中,如何通过使用slot而不是render方法来插入和显示图片。通过指定rowKey和columns中的customRender属性,并结合slot-scope,可以在表格的特定列(这里是picture列)中展示图片,图片的源来自数据对象(如record.picture)。示例代码展示了具体的实现方式。
摘要由CSDN通过智能技术生成

ant-design框架table中插入图片

使用render未达到预期效果,使用slot则可以实现。

  • 组件部分:
<a-table size="default"
  :rowKey="record => record.key"
  :columns='columns'
  :data-source='tableData'
  rowKey="adId">
  <span slot='picture' slot-scope="text, record">
    <template>
      <img
        slot="picture"
        width='150'
        height='100'
        style="cursor: pointer"
        :src='record.picture'
        alt='缩略图'>
      </template>
  </span>
</a-table>
  • columns部分:
columns: [
        {
          dataIndex: 'picture',
          title: '广告图片',
          scopedSlots: { customRender: 'picture' },
          width: '150',
          height: '100',
          align: "center",
        }
  //其他列未展示
  ]
  • data部分:
tableData: [
        {
          title: '好丽友',
          activityBelong: '任意活动',
          picture: 'https://video.acwing.com/image/default/09D8D059E05D4D00B4A39A8FDAC4D0D4-6-2.jpg?auth_key=1691020800-ac2796wing-0-0fec1d8d06a90033422c106b614c4ea9',
          activityUrl: 'https://csdnnews.blog.csdn.net',
          publishTime: '2023-07-25',
          isShow: true,
          backgroundColor: '红色',
          operation: '',
          sort: 1,
          adId: '001'
        }
  ]
  • 效果展示:
    在这里插入图片描述
  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
要在 Ant DesignTable 显示图片,可以使用 `render` 函数来渲染每一行的数据,并在其添加 `img` 标签来显示图片。具体步骤如下: 1. 在 Table ,使用 `render` 函数来渲染每一行的数据,其包括需要显示图片的列。 2. 在 `render` 函数,使用 `img` 标签来显示图片。可以使用 `record` 参数来获取当前行的数据,并从获取图片的链接。 下面是一个简单的示例代码: ```vue <template> <a-table :columns="columns" :data-source="data" /> </template> <script> export default { data() { return { data: [ { name: "John Brown", age: 32, avatarUrl: "https://example.com/avatar.jpg", }, { name: "Jim Green", age: 42, avatarUrl: "https://example.com/avatar.jpg", }, ], }; }, computed: { columns() { return [ { title: "Name", dataIndex: "name", }, { title: "Age", dataIndex: "age", }, { title: "Avatar", dataIndex: "avatarUrl", scopedSlots: { customRender: "avatar" }, }, ]; }, }, methods: { renderAvatar(url) { return <img src={url} style="width: 50px" />; }, }, }; </script> ``` 在这个示例,我们使用 `render` 函数来渲染每一行的数据,并在其添加 `img` 标签来显示图片。在 `columns` ,我们将 `avatarUrl` 列的 `scopedSlots` 属性设置为 `customRender: 'avatar'`,表示使用名为 `avatar` 的 `slot` 来渲染这一列。在 `methods` ,我们定义了一个名为 `renderAvatar` 的函数,用于渲染图片。最终,我们将 `renderAvatar` 函数作为 `avatar` `slot` 的内容,从而显示图片。 希望这可以帮助到你!
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

辞寒oo

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

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

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

打赏作者

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

抵扣说明:

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

余额充值