antD Table表头气泡提示自定义

本文介绍如何在Ant Design的Table组件中实现自定义筛选菜单和提示图标,通过使用filterDropdown和filterIcon属性,结合Tooltip组件的onVisibleChange方法,实现动态提示文案的绑定,避免提示内容固定为“筛选”,并提供服务端动态返回table表头和数据的代码实现。

实现效果:
在这里插入图片描述
用到的属性:

filterDropdown:可以自定义筛选菜单,此函数只负责渲染图层,需要自行编写各种交互;

filterIcon:自定义 filter 图标。结合Tooltip的onVisibleChange方法(显示隐藏的回调)

locale = {filterTitle:悬浮框显示的提示内容}

思路:
1.为提示图标添加Tooltip,利用ToolTip的显示隐藏回调函数(onVisibleChange),为该图标设置对应的提示文案。

	filterIcon: (
          <Tooltip title="默认" onVisibleChange={() => this.onVisibleChange(item.tipMsg)} placement="top">
            <Icon type="question-circle-o" theme="outlined" />
          </Tooltip>
    ),

	onVisibleChange = (tips: string) => {
    	this.setState({tipMsg: tips});
    };

2.在Table的locale对象中,讲tipMsg的值赋值给filterTitle

    const locale = {
      filterTitle: tipMsg || '默认',
    };

注意事项:
要想实现提示内容的自定义,必须要里用onVisibleChange进行提示内容的绑定,否则提示内容会固定为“筛选”。

最后,附上代码:

背景:服务端动态返回table的表头和展示数据。
接口返回数据结构:

  export type TsearchByMonthInfoSource = {
    fontColor: string;
    showValue: string;
  };
  export type TsearchByMonthInfoHead = {
    headName: string;
    orderFile: string; // 有值说明是排序字段
    orderType: number; // 1:正序 0:倒序
    tipMsg: string;
    isFixed:boolean;
  };
  export type TsearchByMonthInfo = {
    dataSource: TsearchByMonthInfoSource[][]; // table展示数据部分
    tableHead: TsearchByMonthInfoHead[]; // 表头
  };

代码实现:

// 第一步:请求接口,获取数据
    searchAttendanceByMonth({
      curPage,
      dptList, //部门Id列表 ,
      orgId,
      orgList, //结构列表 ,
      pageIndex, // 第几页 ,
      pageSize, //每页几条 ,
      userList, //人员LekeUserId列表
      yearMonth: curMonth ? curMonth.format('YYYY-MM') : '', //年月 ,
    }).then(json => {
      let jsonData = json.data;
        this.getColumns(jsonData.dataList[0] && jsonData.dataList[0].tableHead);
        ...
    });
// 第二步:将表头和table数据对应
  getColumns = (tableHead: TsearchByMonthInfoHead[]) => {
     const columns: ColumnProps<Array<any>>[] = tableHead && tableHead.map((item, index) => {
      return {
        title: item.headName,
        align:'center',
        fixed: item.isFixed ? 'left' : false,
        width: 80,
        filterDropdown: item.tipMsg ? true : false,
        filterIcon: (
          <Tooltip title="默认" onVisibleChange={() => this.onVisibleChange(item.tipMsg)} placement="top">
            <Icon type="question-circle-o" theme="outlined" />
          </Tooltip>
        ),
        render: (_: any, record: any) => {
          const value = record[index].showValue;
          return (
            <div
              title={value ? value : ''}
              className={`${prefixCls}-width-limit`}
            >
              {value}
            </div>
          );
        },
      };
    });

    this.setState({
      columns,
    });
  };
<think>好的,我需要帮助用户了解Ant Design(antd)的Table组件表头高度的问题。首先,我应该回忆一下antd Table的相关文档和常见配置。表头高度通常由CSS样式控制,可能涉及到默认值以及如何自定义。 用户可能想知道如何调整表头的高度,或者遇到了表头高度不符合需求的情况。我需要分步骤解释:首先,默认的表头高度是多少;其次,如何通过CSS覆盖默认样式;然后,可能需要提到使用组件提供的API,比如className或style属性;最后,提醒用户注意响应式设计或浏览器兼容性问题。 需要确保回答结构清晰,每一步都有明确的指导。同时,按照用户的要求,所有行内数学表达式用$...$,独立公式用$$...$$,但这里可能更多涉及代码和CSS示例,所以需要正确使用代码块。不过用户没有特别提到代码块的格式,但根据问题类型,代码示例是必要的。 另外,要使用中文回答,避免专业术语错误。可能需要检查antd的最新版本是否有变化,但假设用户使用的是较新版本,保持答案的通用性。最后,确保LaTeX语法正确,尽管在这个问题中可能用不到数学公式,但如果有涉及到计算或尺寸关系的话需要注意。 总结回答的结构:默认高度说明、自定义方法(内联样式、CSS类、全局样式)、注意事项,可能加上示例代码。需要验证每个步骤的正确性,比如正确的CSS类名和属性,例如.ant-table-thead > tr > th。</think>Ant Design(antd)的Table组件表头高度可通过以下方式理解与调整: --- ### 1. **默认表头高度** - antd Table的默认表头高度为 **$54px$**(包含上下各$12px$的padding和$1px$的边框)。 - 计算公式为: $$ \text{高度} = \text{内容高度} + \text{padding-top} + \text{padding-bottom} + \text{border宽度} $$ 即: $$ 54px = 28px + 12px + 12px + 2px $$ --- ### 2. **自定义表头高度** #### 方法1:通过CSS覆盖样式 ```css /* 修改全局表头样式 */ .ant-table-thead > tr > th { height: 60px; /* 自定义高度 */ padding: 8px 16px; /* 调整padding */ } ``` #### 方法2:使用`components`API(antd v4+) ```jsx <Table components={{ header: { cell: ({ children, ...props }) => ( <th {...props} style={{ ...props.style, height: 60 }}> {children} </th> ), }, }} /> ``` #### 方法3:内联样式 ```jsx <Table columns={[ { title: '列名', dataIndex: 'data', onHeaderCell: () => ({ style: { height: 60 } }), }, ]} /> ``` --- ### 3. **注意事项** - **内容溢出**:高度减小时需确保标题文本不换行(可配合`white-space: nowrap`)。 - **全局影响**:直接修改`.ant-table-thead`会影响所有Table组件,建议通过`className`局部作用域控制。 - **版本差异**:antd v4与v5的默认高度一致,但样式细节可能有差异,需检查版本文档。 --- 通过上述方法,可灵活控制表头高度以适应业务需求。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值