EditTable 编辑表格
依赖安装
npm i @antdp/edit-table
编辑表格 参数
export interface EditableTableProps
extends Omit<TableProps<any>, 'columns' | 'rowKey'> {
columns: ColumnsProps[];
onSave: (data: any[], row: any, record?: any, indx?: number) => void;
onBeforeSave?: (item: any, record: any, index: number) => boolean;
rowKey: string;
optIsFirst?: boolean;
isOpt?: boolean;
optConfig?: ColumnsProps;
isOptDelete?: boolean;
initValue?: any;
isAdd?: boolean;
onBeforeAdd?: () => boolean;
onErr?: (err: ValidateErrorEntity<any>) => void;
onValuesChange?: (
list: any[],
value: any,
allValue: any,
id: string | number,
form: FormInstance,
) => void;
multiple?: boolean;
addBtnProps?: ButtonProps;
store?: Store;
}
表格 列参数
export interface ColumnsProps extends ColumnType<any> {
editable?: boolean;
inputNode?: ((...arg: any[]) => React.ReactNode) | React.ReactNode;
rules?: Rule[];
itemAttr?: Omit<FieldProps, 'rules' | 'label' | 'name'>;
attr?: Partial<ItemChildAttr<any, any>>;
type?: ItemChildType;
tip?: (errs: string) => React.ReactNode;
tipAttr?: TooltipProps;
render?: (
value: any,
record: any,
index: number,
other?: OtherProps,
) => React.ReactNode | RenderedCell<any>;
}
export interface OtherProps {
editingKey: any[];
editable: boolean;
newAdd: any[];
isNewAdd: boolean;
save: (key: string | number, record: any, indx: number) => void;
cancel: (id: string | number) => void;
onDelete: (id: string | number, rowItem: any, index: number) => void;
edit: (record: any) => void;
}
ref 返回值
export interface RefEditTableProps {
save: (key: string, record: any, indx: number) => void;
onDelete: (id: string | number, rowItem: any, index: number) => void;
edit: (record: any) => void;
cancel: (key: string | number) => void;
add: () => void;
isEditing: (record: any) => boolean;
editingKey: (string | number)[];
newAdd: (string | number)[];
forms: Store;
}
案例
import ReactDOM from 'react-dom';
import React from 'react';
import { Input, Col, InputNumber, Button, Select } from 'antd';
import EditTable from '@antdp/edit-table';
import 'antd/dist/antd.css';
const originData = [];
for (let i = 0; i < 5; i++) {
originData.push({
key: i.toString(),
name: `Edrward ${i}`,
age: 32,
});
}
const EditableTable =() => {
const [data, setData] = React.useState(originData);
const [tableProps, setTableProps] = React.useState({
isAdd: true,
isOpt: true,
optIsFirst: true,
});
const columns = [
{
title: 'name',
dataIndex: 'name',
width: '25%',
editable: true,
type: 'Custom',
inputNode: (edit) => {
return <Input {...edit} />;
},
},
{
title: 'age',
dataIndex: 'age',
width: '15%',
editable: true,
type: 'Custom',
rules: [{ required: true, message: '请输入' }],
inputNode: <InputNumber />,
},
{
title: 'address',
dataIndex: 'address',
width: '40%',
editable: true,
type: 'Input',
},
];
return (
<div>
<Button
onClick={() => {
setTableProps({
...tableProps,
isOptDelete: !tableProps.isOptDelete,
});
}}
>
删除按钮
</Button>
<Button
onClick={() => {
setTableProps({ ...tableProps, isAdd: !tableProps.isAdd });
}}
>
新增按钮
</Button>
<Button
onClick={() => {
setTableProps({ ...tableProps, multiple: !tableProps.multiple });
}}
>
多行编辑
</Button>
<Button
onClick={() => {
setTableProps({ ...tableProps, optIsFirst: !tableProps.optIsFirst });
}}
>
操作列前或后
</Button>
<Button
onClick={() => {
setTableProps({ ...tableProps, isOpt: !tableProps.isOpt });
}}
>
无操作列
</Button>
<EditTable
initValue={{ address: 2193 }}
onValuesChange={(list) => setData(list)}
rowKey="key"
optIsFirst={true}
dataSource={data}
columns={columns}
onSave={(list) => setData(list)}
isAdd={true}
{...tableProps}
/>
</div>
);
};
ReactDOM.render(<EditableTable />, _mount_);