如何使用 Antd 和 VueJS 在 table 单元格中使用 customRender
How to use customRender in a table cell with Antd and VueJS
我在我的应用程序中使用 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">'
}
}
},
不幸的是,结果是这样的:
谁能帮我弄清楚我做错了什么?
您可以利用列中的 scopedSlots 属性,并使用它为 customRender 属性.
这是一个例子:
const columns = [
{
title: "Image",
dataIndex: "image",
key: "image",
scopedSlots: { customRender: "image-column" },
},
];
现在,在您的模板中,您可以使用 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>
我在我的应用程序中使用 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">'
}
}
},
不幸的是,结果是这样的:
谁能帮我弄清楚我做错了什么?
您可以利用列中的 scopedSlots 属性,并使用它为 customRender 属性.
这是一个例子:
const columns = [
{
title: "Image",
dataIndex: "image",
key: "image",
scopedSlots: { customRender: "image-column" },
},
];
现在,在您的模板中,您可以使用 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>