ant-design / pro-components

🏆 Use Ant Design like a Pro!
https://pro-components.antdigital.dev
MIT License
4.24k stars 1.35k forks source link

关于protable使用antdtable属性的问题 #5141

Closed dashixionghahaha closed 1 year ago

dashixionghahaha commented 2 years ago

What problem does this feature solve?

请问protable如何使用antdTable中的sticky这个属性,弄了半天也没弄好,请大神们赐教

What does the proposed API look like?

希望protable实现sticky这个属性

github-actions[bot] commented 2 years ago

You may look for issues:

  1. 62% ant-design/ant-design#32677

🤖 By issues-similarity-analysis

Lands-1203 commented 2 years ago

protable 中的columns属性中有一个fixed: 'left|right',表格超 <ProTable scroll={{ x: 1600 }} 可以设置表格的宽度

chenshuai2144 commented 1 year ago

TAutomatically replying with ChatGPT can be attempted, but it cannot be guaranteed to be completely accurate and may not fully address all issues. Please feel free to reply if you have any further questions or concerns. 此回复基于 ChatGPT 自动生成,可以尝试下方案,官方人员会在一定时间后继续继续处理。

感谢您对Protable的关注与使用,关于sticky属性的应用,可以通过添加 scroll 属性来实现。

Protable 是基于 Antd Table 封装的,Antd Table 中提供了 sticky 属性来实现固定表头,但该属性需要结合 scroll.x 属性一起使用。那么,我们就可以将这一属性添加到 Protable 的 scroll 中即可生效。

下面是如何添加该属性的示例代码:

import React, { useState } from 'react';
import ProTable from '@ant-design/pro-table';

const columns = [
  {
    title: '交易编号',
    dataIndex: 'id',
    width: 100,
    fixed: 'left', // 该列固定在左侧
  },
  {
    title: '交易金额',
    dataIndex: 'money',
    width: 100,
    sorter: true,
  },
  {
    title: '交易状态',
    dataIndex: 'status',
    width: 100,
    filters: [
      { text: '成功', value: 'success' },
      { text: '失败', value: 'fail' },
    ],
    onFilter: (value, record) => record.status === value,
  },
  {
    title: '交易时间',
    dataIndex: 'createdAt',
    width: 200,
    fixed: 'right', // 该列固定在右侧
  },
];

export default () => {
  const [dataSource, setDataSource] = useState([...Array(50).keys()].map((id) => ({
    id,
    money: Math.floor(Math.random() * 1000),
    status: id % 2 === 0 ? 'success' : 'fail',
    createdAt: new Date(`2020-08-${Math.floor(id / 2) + 1}`),
  })));

  return (
    <ProTable
      columns={columns}
      dataSource={dataSource}
      scroll={{ x: 800, y: 500 }} // 设置表格宽高
      sticky // 开启表头固定属性
      rowKey="id"
      dateFormatter="string"
      headerTitle="高级表格"
      toolBarRender={() => [
        <button
          key="3"
          onClick={() => setDataSource([...dataSource, {
            id: dataSource.length,
            money: Math.floor(Math.random() * 1000),
            status: dataSource.length % 2 === 0 ? 'success' : 'fail',
            createdAt: new Date(),
          }])}
        >
          添加一行
        </button>,
      ]}
    />
  );
};

在上面的代码中,我们通过 scroll 属性设置了表格的宽高,开启了表头固定的属性,然后在 columns 中指定了哪些列要固定,这样就可以实现表格的 sticky 效果了。

另外,具体可以参考文档说明:https://procomponents.ant.design/components/table#scroll