Antd表格customRender与scopedSlots同时使用

7 篇文章 0 订阅

行列合并与插槽同时使用
普通的table插槽是在column对象中配置scopedSlots: { customRender: ‘xxx’ },并在标签中写相应的代码实现的,与此处关系不大,不做过多的介绍。
行列合并时要对customRender做配置,相应单元格的重写则需要写在customRender返回的children中,使用的是类似React的jsx语法:
例子如下,我希望在实现行合并的同时操作按钮有二次确认事件

{
          title: '操作',
          dataIndex: 'operation',
          // scopedSlots: { customRender: 'operation' },
          fixed: 'right',
          align: 'center',
          width: 150,
          customRender: (text, record, index) => {
            var childrenVal
            // record.auditStatus返回得是0/1   此处已经转换状态
            if (record.auditStatus == '已审核') {
              childrenVal = (
                <div>
                  <a-button size="small" disabled style="margin-right:10px">
                    已审核
                  </a-button>
                  <a-popconfirm title="确定重置吗?" onConfirm={() => this.leaveCong(record)} placement="topRight">
                    <a-button size="small" style="background: #32cd32; border-color: #32cd32; color: #fff;">
                      重置
                    </a-button>
                  </a-popconfirm>
                </div>
              )
            } else {
              childrenVal = (
                <div>
                  <a-popconfirm title="确定审核吗?" onConfirm={() => this.leaveShen(record)} placement="topRight">
                    <a-button size="small" style="background: #faad14; border-color: #faad14; color: #fff;margin-right:10px">
                      未审核
                    </a-button>
                  </a-popconfirm>
                  <a-button size="small" disabled>
                    重置
                  </a-button>
                </div>
              )
            }
            const obj = {
              children: childrenVal,
              attrs: {
                rowSpan: record.rowNum,
              },
            }
            return obj
          },
        },

效果如图:
在这里插入图片描述
如果需要对插槽中的元素添加事件:

onConfirm={() => this.leaveCong(record)}  / onClick={() => this.leaveCong(record)}
  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Antd 表格组件的 `customRender` 和 `scopedSlots` 是用于自定义表格单元格内容的两种不同方式。 `customRender` 是一个函数,用于自定义表格单元格的渲染内容。你可以在函数中根据传入的参数来返回自定义的渲染结果。 `scopedSlots` 是一个对象,用于自定义表格单元格的内容。你可以在对象中定义不同的插槽名称,并将其对应的内容作为表格单元格的内容。 当 `customRender` 和 `scopedSlots` 同时存在时,Antd 表格组件会优先使用 `customRender` 来渲染单元格内容。这意味着,如果你同时使用了 `customRender` 和 `scopedSlots`,`scopedSlots` 中的插槽将会失效。 如果你想要使用 `scopedSlots` 来自定义表格单元格的内容,可以将 `customRender` 的值设为 `undefined` 或 null,这样 Antd 表格组件就会使用 `scopedSlots` 中定义的插槽来渲染单元格内容。 以下是一个示例代码: ```vue <template> <a-table :columns="columns" :data-source="data"> <template v-slot:title> Custom Table </template> <template v-slot:name="{ text }"> <span style="color: red">{{ text }}</span> </template> </a-table> </template> <script> export default { data() { return { columns: [ { title: 'Name', dataIndex: 'name', customRender: undefined, // or null scopedSlots: { customRender: 'name' } }, { title: 'Age', dataIndex: 'age' }, { title: 'Address', dataIndex: 'address' } ], data: [ { name: 'John Doe', age: 30, address: 'New York' }, // ... ] }; } }; </script> ``` 在上面的示例中,我们将 `customRender` 的值设为 `undefined`,并在 `scopedSlots` 中使用 `name` 插槽来自定义 `Name` 列的内容。这样,`scopedSlots` 中的插槽就会生效,并且 `Name` 列的单元格内容会渲染为红色字体。 希望以上解答能对你有帮助!如有任何疑问,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值