antd的a-table表格中合并且使用插槽(使用customRender合并出现问题解决方案)

本文档介绍了在Ant Design的a-table组件中如何使用customRender进行单元格合并,并结合scopedSlots实现复杂操作。示例代码详细展示了在`Name`、`Age`、`Homephone`等列的合并方法,以及在`operation`列中使用插槽进行跨行合并组件的挑战和解决方案。同时,提到了`customCell`属性的使用,但文档资料不足,作者通过研究源码找到实际应用案例。文章适合Ant Design表格高级使用的开发者参考。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

antd的a-table表格中合并且使用插槽问题

1. customRender合并单元格

在antd官方文档中,是由使用customRender方式将单元格合并的方法

data() {
    const columns = [
      {
        title: 'Name',
        dataIndex: 'name',
        customRender: (text, row, index) => {
          if (index < 4) {
            return <a href="javascript:;">{text}</a>;
          }
          return {
            children: <a href="javascript:;">{text}</a>,
            attrs: {
              colSpan: 5,
            },
          };
        },
      },
      {
        title: 'Age',
        dataIndex: 'age',
        customRender: renderContent,
      },
      {
        title: 'Home phone',
        colSpan: 2,
        dataIndex: 'tel',
        customRender: (value, row, index) => {
          const obj = {
            children: value,
            attrs: {},
          };
          if (index === 2) {
            obj.attrs.rowSpan = 2;
          }
          // These two are merged into above cell
          if (index === 3) {
            obj.attrs.rowSpan = 0;
          }
          if (index === 4) {
            obj.attrs.colSpan = 0;
          }
          return obj;
        },
      },
      {
        title: 'Phone',
        colSpan: 0,
        dataIndex: 'phone',
        customRender: renderContent,
      },
      {
        title: 'Address',
        dataIndex: 'address',
        customRender: renderContent,
      },
    ];
    return {
      data,
      columns,
    };
  }

效果如下
在这里插入图片描述

使用customRender方式确实可以对表格进行特殊处理,像此处就进行单元格合并操作,但很多操作必须在单元个中插入组件的操作,举例,如下图所示
  {
    title: '详细操作',
    dataIndex: 'content',
    scopedSlots: {customRender: 'operation'},
    align: 'center',
    customRender: (text, row, index) => {
      return { // 否则以独占4列的文本形式被渲染
        children: text,
        attrs: {
          rowSpan: row.row
        }
      }
    }
  }
<a-table :columns="columnsRuleInfoChild" :data-source="dataRuleChild" class="info-rule">
<template slot="operation" slot-scope="text, record">
            <a-button @click="onShowChildRuleInfo(record.AbnormalWarnInfo)" type="link"
                      style="background-color: rgba(0, 0, 0, 0);display: inline-block;float: none">
              <a-icon
                  type="diff"/>
              子集条件
            </a-button>
            <a-button @click="onSelectChooseWarnRule(record)" type="link"
                      style="background-color: rgba(0, 0, 0, 0);display: inline-block;float: none">
              <a-icon
                  type="select"/>
              确认
            </a-button>
</a-table>
如果用过的人可以知道,此处的表头详细操作确实跨行合并了,但是组件内容却并没有重写

再此处可以发现这个地方的插槽 scopedSlots: {customRender: ‘operation’} 方法没有加载组件
因为 customRender: (text, row, index) => {
return {
children: text,
attrs: {
rowSpan: row.row
}
}
}

重载了 scopedSlots: {customRender: ‘operation’}

所以处理方式只能在children的属性中写入,具体如下

customRender: (text, row, index) => {
	·let setTime = setTimeout(()=>{
        let button = document.getElementById("queryChildList");
      	let submit = document.getElementById("submit")
      	button.onclick = function(){
        //此处省略
        }
      	submit.onclick = function(){
        //此处省略
        } 
      },100)
      return {
        children: (<a href="#" id='queryChildList'> 子条件集合</a>  <a href="#" id="submit">确认</a>),
        attrs: {
          rowSpan: row.row
        }
      }
    }

但是此操作无法加载vue的组件,只能使用原生的js操作

2. customCell合并

antd在原文档中有这个属性
在这里插入图片描述
在文档中的示例却只有如下所示

//customRow 用法 #
//适用于 customRow customHeaderRow customCell customHeaderCell。遵循Vue jsx语法。
<Table
  customRow={(record) => {
    return {
      props: {
        xxx... //属性
      },
      on: { // 事件
        click: (event) => {},       // 点击行
        dblclick: (event) => {},
        contextmenu: (event) => {},
        mouseenter: (event) => {},  // 鼠标移入行
        mouseleave: (event) => {}
      },

    };
  )}
  customHeaderRow={(column) => {
    return {
      on: {
        click: () => {},        // 点击表头行
      }
    };
  )}
/>

再没有多余阐述中,你没有看错,没有其他说明了,当时就想问候写文档人的亲戚了。。。

在我深入底层源码中,我找到了这个属性对应适用的字段
在这里插入图片描述
示例

{
    title: '选额内容',
    align: 'center',
    dataIndex: 'row',
    scopedSlots: {customRender: 'row'},
    customCell: ((record, rowIndex) => {
      return {
        style: {display: record.row === 0 ? 'none' : undefined},
        attrs: {
          rowSpan: record.row,
        }
      }
    })
  }
	<a-radio-group v-model="selectedRowKeys" style="width: 100%">
          <a-table :columns="columnsCheckConfig" :data-source="dataCheckData" class="info"
                   rowKey="contentId"
          >
            <template slot="row" slot-scope="text, record">
              <a-radio :value="record.id"></a-radio>
            </template>
          </a-table>
        </a-radio-group>

这样就可在operation这个列中使用它从插槽

效果如下图所示
在这里插入图片描述

初出茅庐,有错误还请纠正

博图v16是一种常用的仿真软件,用于帮助工程师模拟PLC(可编程逻辑控制器)的行为和功能。加载PLC出错可能有以下几个可能的原因: 1. PLC程序错误:在加载PLC时,如果程序中存在错误或者逻辑错误,可能会导致加载出错。需要仔细检查PLC程序中的语法错误或逻辑错误,修复错误后重新加载。 2. 软件兼容性问题:博图v16仿真加载PLC可能与某些PLC型号不兼容,导致加载失败。需要检查博图v16与所使用的PLC型号之间的兼容性,如果不兼容,则需要尝试使用适的仿真软件或者更新博图v16软件版本。 3. 硬件连接问题:如果PLC与计算机或仿真软件之间的连接存在问题,可能导致加载出错。需要检查PLC与计算机的连接方式和设置,确保连接正常。如果连接采用通信接口(例如串行口或以太网口),还需要检查通信接口的设置和参数。 4. 计算机性能问题:如果计算机的性能不足,可能导致加载PLC时出错。特别是在加载较为复杂的PLC程序时,计算机的处理能力可能不够,导致加载失败。需要确保计算机硬件性能足够,如果计算机配置较低,可以尝试使用更高性能的计算机。 总之,博图v16仿真加载PLC出错可能由于PLC程序错误、软件兼容性问题、硬件连接问题或计算机性能问题等原因引起。根据具体情况进行排查和处理,可以解决加载出错的问题
评论 13
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值