如何通过 Antd 和 VueJS 在表格单元格中使用 customRender

2024-04-01

我在我的应用程序中使用 antd,并尝试执行 customRender 以在单元格中显示图像。

我的列数组如下所示:

columns: [
        { title: 'Design',
          dataIndex: 'designImage.fileUrl',
          customRender () { // h will be injected
            return '<div id="foo">bar</div>'
          }
        },
]

所以,正如你想象的那样,结果是这样的:

我也尝试过这种方式:

{ title: 'Design',
          dataIndex: 'designImage.fileUrl',
          customRender: (text, record, index) => {
            return {
              children: '<img src="https://via.placeholder.com/300.png/09f/fff">'
            }
          }
        },

不幸的是,结果是这样的:

有人可以帮我弄清楚我做错了什么吗?


您可以利用作用域槽位列中的属性,并使用它来定义范围插槽自定义渲染财产。

这是一个例子:

const columns = [
  {
    title: "Image",
    dataIndex: "image",
    key: "image",
    scopedSlots: { customRender: "image-column" },
  },
];

现在,在您的模板中,您可以使用图像列命名槽,如下所示:

<a-table :columns="columns" :data-source="tableData">
    <template slot="image-column" slot-scope="image">
        <img :src="image" /> <!-- Add your custom elements here -->
    </template>
</a-table>

这是一个组件示例:

<template>
  <a-table :columns="columns" :data-source="tableData">
    <template slot="image-column" slot-scope="image">
      <img :src="image" />
    </template>
  </a-table>
</template>

<script>
const columns = [
  {
    title: "Image",
    dataIndex: "image",
    key: "image",
    scopedSlots: { customRender: "image-column" },
  },
];

const tableData = [
  {
    image: "https://picsum.photos/200",
  },
  {
    image: "https://picsum.photos/200",
  },
];

export default {
  data() {
    return {
      columns,
      tableData,
    };
  },
};
</script>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何通过 Antd 和 VueJS 在表格单元格中使用 customRender 的相关文章

随机推荐