Antd表头在使用滚动Y和动态列时消失

Posted

技术标签:

【中文标题】Antd表头在使用滚动Y和动态列时消失【英文标题】:Antd table header disapppear when using scroll Y and dynamic columns 【发布时间】:2021-12-30 09:54:20 【问题描述】:

我在为 Antd 表渲染动态列时遇到问题,当我重新渲染列时,表头消失了。

getDefaultColumns()

const getDefaultColumns = () => 
    return [
      
        title: <span className=`table-header`>STT</span>,
        align: "center",
        key: "STT",
        render: (value, record, index) =>
          (currentPage - 1) * pageSize + index + 1,
        width: 50,
        fixed: "left",
      ,
      
        title: <span className=`table-header pl-4`>Name</span>,
        dataIndex: "full_name",
        width: 150,
        fixed: "left",
        align: "left",
        render: (text, record) => (
          <span className="pl-4">record.full_name</span>
        ),
      ,
      
        title: <span className=`table-header pl-4`>Code</span>,
        dataIndex: "code",
        fixed: "left",
        align: "left",
        render: (text, record) => <span className="pl-4">record.code</span>,
      ,
      
        title: <span className=`table-header`>DOB</span>,
        dataIndex: "birthday",
        align: "center",
        render: (value) => <span>moment(value).format(DATE_FORMAT)</span>,
        fixed: "left",
      ,
      
        title: <span className=`table-header`>SUMMARY</span>,
        children: [
          
            title: (
              <span className=`table-header`>
                TB" "
                semester.key !== "whole_year"
                  ? `HK $semester.shortValue`
                  : semester.value
              </span>
            ),
            dataIndex: ["study_summary_info", "average_semester"],
            key: "study_summary_info_average_semester",
            align: "center",
          ,
          
            title: <span className=`table-header`>Rank</span>,
            key: "study_summary_info_ranked_academic",
            align: "center",
            render: (value, record) => 
              return (
                <SelectOption
                  ...
                />
              );
            ,
          ,
          
            title: <span className=`table-header`>Conduct</span>,
            key: "study_summary_info_conduct",
            align: "center",
            render: (value, record) => 
              return (
                <SelectOption
                  ...
                />
              );
            ,
          ,
        ],
      ,
      
        title: <span className=`table-header`>Day off</span>,
        dataIndex: ["study_summary_info", "day_off"],
        key: "study_summary_info_day_off",
        align: "center",
      ,
      
        title: <span className=`table-header`>Title</span>,
        key: "study_summary_info_title",
        align: "center",
        render: (value, record) => 
          return (
            <SelectOption
              ...
            />
          );
        ,
      ,
      
        title: <span className=`table-header`>Result</span>,
        key: "study_summary_info_result",
        align: "center",
        render: (value, record) => 
          return (
            <SelectOption
              ...
            />
          );
        ,
      ,
    ];
  ;

setupColumns()

const setupColumns = () => 
    filterClassHasSubject( class_id: classId, page: 1, size: 999 )
      .then((res) => 
        if (res.data.code === "success") 
          const data = res.data.data;

          let children = data.map((val) => 
            return 
              title: (
                <span className=`table-header`>val.subject_info?.name</span>
              ),
              key: (value, record, index) => `subject_$index`,
              width: 150,
              align: "center",
              ellipsis: true,
              render: (value, record) => 
                const point = record.average_transcript_info.find(
                  (x) =>
                    x.subject_id === val.subject_id &&
                    x.semester === semester.key
                );

                return (
                  <span>point?.value_number</span>
                );
              ,
            ;
          );

          let scoreGroup = 
            title: <span className=`table-header`>DTB</span>,
            children,
          ;

          let temp = getDefaultColumns();
          temp.splice(4, 0, scoreGroup);
          setColumns([...temp]);
        
      );
  ;

Table

<Table
  dataSource=summaryData
  columns=columns
  bordered
  rowClassName=(record, index) =>
    index % 2 === 1
      ? "odd-row attendance-row"
      : "bg-white attendance-row"
  
  pagination=false
  scroll= x: "100vw", y: 580 
/>

当我删除Table 上的滚动 Y 时,不会发生这种情况。 当 semesterclassId 更改时,动态呈现列。

Before change semester

After change semester

【问题讨论】:

【参考方案1】:

就我而言,这是因为列中的属性key。解决方案是使用string,而不是使用函数返回string

【讨论】:

以上是关于Antd表头在使用滚动Y和动态列时消失的主要内容,如果未能解决你的问题,请参考以下文章

antd vue table滚动页面固定表头

如何使用带有垂直滚动的转发器控件修复表头?

使用antd的table组件实现一个交叉表(动态合并单元格)

使用 vb.net 根据 SQL 数据库中存在的记录动态显示/隐藏 DataGrid 按钮列时遇到问题

layui table 表格动态表头

vue-element动态渲染表头(el-table-column)