2.2.0 • Published 5 years ago

fk_table v2.2.0

Weekly downloads
-
License
MIT
Repository
-
Last release
5 years ago

中文

🏆 Use Ant Design Table like a Pro!

Demo

codesandbox

API

pro-table is encapsulated in an antd table, supports some presets, and encapsulates some behavior. Only apis different from antd table are listed here.

Table

PropertyDescriptionTypeDefault Value
requesta method to get the dataSource.(params?: {pageSize: number;current: number;[key: string]: any;}) => Promise<RequestData<T>>-
postDataDo some processing on the data obtained through the url.(data: T[]) => T[]-
defaultDataDefault data array.T[]-
actionRefTriggered after the table data is successfully initialized, it will be triggered multiple times.React.MutableRefObject<ActionType> \| ((actionRef: ActionType) => void)[]
toolBarRenderRender toolbar, support for returning a dom array, will automatically increase margin-right.(action: UseFetchDataAction<RequestData<T>>) => React.ReactNode[]-
onLoadTriggered after the data is loaded, it will be triggered multiple times.(dataSource: T[]) => void-
onRequestErrorTriggered when fetching data failed(e: Error) => void-
tableClassNameThe className of the packaged tablestring-
tableStyleThe style of the packaged tableCSSProperties-
optionstable's default operation, set to false to close it{{ fullScreen: boolean \| function, reload: boolean \| function,setting: true }}{{ fullScreen: true, reload:true,setting: true }}
searchWhether to search the form. It can also be a query form config when passing an object.boolean \| { span?: number \| DefaultColConfig,searchText?: string, resetText?: string, collapseRender?: (collapsed: boolean) => React.ReactNode, collapsed:boolean, onCollapse: (collapsed:boolean)=> void }true
dateFormatterformatting moment type"string" \| "number" \| falsestring
beforeSearchSubmitMake some changes before searching(params:T)=>T-
onSizeChangetable size changes(size: 'default' | 'middle' | 'small' | undefined) => void-
columnsStateMapcolumns status{[key: string]: { show:boolean, fixed: "right"|"left"} }-
onColumnsStateChangecolumns status changed(props: {[key: string]: { show:boolean, fixed: "right"|"left"} }) => void-
formsearch From config type="form" and search form's Form config ,the config data like antd FormOmit<FormProps, 'form'>-

Columns

PropertyDescriptionTypeDefault Value
renderTextSimilar to table render, but must return string, if you just want to convert the enumeration, you can use this scheme(text: any,record: T,index: number,action: UseFetchDataAction<RequestData<T>>) => string-
renderLike table's render, the first argument becomes dom, and the fourth argument is added.(text: React.ReactNode,record: T,index: number,action: UseFetchDataAction<RequestData<T>>) => React.ReactNode \| React.ReactNode[]-
renderFormIteminput component for rendering query form(item, props: {value, onChange}) => React.ReactNode-
ellipsisWhether to automatically abbreviateboolean-
copyableWhether to support replicationboolean-
valueEnumThe enumeration of values will automatically convert the value as a key to get the content to be displayed{key: string: React.ReactNode}-
valueTypeType of value'money' \| 'option' \| 'date' \| 'dateTime' \| 'time' \| 'text'\| 'index' \| 'indexBorder''text'
hideInSearchDo not show this in the query formboolean-
hideInTableDo not show this column in Tableboolean-
formItemPropsProps passed into query form item{ [prop: string]: any }-

ActionType

Sometimes we need to trigger the reload of the table and other actions, and actions can help us do this.

interface ActionType {
  reload: () => void;
  fetchMore: () => void;
  reset: () => void;
}

const ref = useRef<ActionType>();

<ProTable actionRef={ref} />;

// Load more
ref.current.reload();

// 加载更多
ref.current.fetchMore();

// Reset to reset value
ref.current.reset();

// clear selected
ref.current.clearSelected();

valueType

TypeDescriptionExamples
moneyConversion value is amount¥ 10,000.26
dateDate2019-11-16
dateTimeDate and Time2019-11-16 12:50:00
timetime12:50:00
optionThe operation item will automatically increase the marginRight. Only one array is supported, and it will be automatically ignored in the form.
textDefault value, no processing-
textareaSame as text, form will be converted to textarea component when converting-
indexserial number-
indexBorderordinal column with border-
progressprogress bar-
digitSimple digit, which will be converted to inputNumber when form is converted-
progressprogress bar-

valueEnums

interface IValueEnum {
  [key: string]:
    | React.ReactNode
    | {
        text: React.ReactNode;
        status: 'Success' | 'Error' | 'Processing' | 'Warning' | 'Default';
      };
}

Usage

npm install @ant-design/pro-table
# or
yarn add @ant-design/pro-table
import React, { useState } from 'react';
import ProTable, { ProColumns } from '@ant-design/pro-table';
import { Input, Button } from 'antd';

const columns: ProColumns[] = [
  {
    title: 'Name',
    dataIndex: 'name',
    copyable: true,
  },
  {
    title: 'Age',
    dataIndex: 'age',
  },
  {
    title: 'date',
    dataIndex: 'date',
    valueType: 'date',
  },
  {
    title: 'option',
    valueType: 'option',
    dataIndex: 'id',
    render: (text, row, index, action) => [
      <a
        onClick={() => {
          window.alert('确认删除?');
          action.reload();
        }}
      >
        delete
      </a>,
      <a
        onClick={() => {
          window.alert('确认刷新?');
          action.reload();
        }}
      >
        reload
      </a>,
    ],
  },
];

export default () => {
  const [keywords, setKeywords] = useState('');
  return (
    <ProTable<{}, { keywords: string }>
      size="small"
      columns={columns}
      request={() => ({
        data: [
          {
            name: 'Jack',
            age: 12,
            date: '2020-01-02',
          },
        ],
        success: true,
      })}
      rowKey="name"
      params={{ keywords }}
      toolBarRender={action => [
        <Input.Search
          style={{
            width: 200,
          }}
          onSearch={value => setKeywords(value)}
        />,
      ]}
      pagination={{
        defaultCurrent: 10,
      }}
    />
  );
};

LICENSE

MIT