【问题标题】:How to use customRender in a table cell with Antd and VueJS如何使用 Antd 和 VueJS 在表格单元格中使用 customRender
【发布时间】:2021-06-21 23:47:35
【问题描述】:

我在我的应用程序中使用 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">'
            }
          }
        },

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

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

【问题讨论】:

    标签: vue.js antd


    【解决方案1】:

    您可以利用列中的 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>
    

    【讨论】:

      猜你喜欢
      • 2020-02-22
      • 2020-05-05
      • 2021-07-03
      • 2021-09-24
      • 1970-01-01
      • 2021-12-27
      • 2013-12-23
      • 2019-04-28
      • 2015-12-31
      相关资源
      最近更新 更多