2.1.1 • Published 1 month ago
@antdp/edit-table v2.1.1
EditTable 编辑表格
依赖安装
npm i @antdp/edit-table
基本使用
import React from 'react';
import { Input, Col, InputNumber, Button, Select ,Form} from 'antd';
import EditTable from '@antdp/edit-table';
import 'antd/dist/reset.css';
const originData = [];
for (let i = 0; i < 5; i++) {
originData.push({
key: i.toString(),
name: `Edrward ${i}`,
age: 32,
address: `London Park no. ${i}`,
});
}
const EditableTable =() => {
const [data, setData] = React.useState(originData);
const [tableProps, setTableProps] = React.useState({
isAdd: true,
isOpt: true,
optIsFirst: false,
});
const columns = [
{
title: 'name',
dataIndex: 'name',
width: '20%',
editable: true,
type: 'Input',
},
{
title: 'age',
dataIndex: 'age',
width: '15%',
editable: true,
type: 'Custom',
rules: [{ required: true, message: '请输入' }],
inputNode: <InputNumber />,
},
{
title: 'isList',
dataIndex: 'list',
width: '15%',
editable: true,
type: 'Custom',
isList: true,
render: (text) => {
return (
text &&
(text || [])
.filter((it) => it)
.map((ite) => ite.first)
.join(',')
);
},
inputNode: (fields, { add, remove }, { errors }) => (
<>
{fields.map(({ key, name, fieldKey, ...restField }, index) => (
<div style={{marginBottom:10}} >
<EditTable.Item
key={key}
{...restField}
name={[name, 'first']}
fieldKey={[fieldKey, 'first']}
rules={[
{
required: true,
whitespace: true,
message: 'Missing first name',
},
]}
>
<Input placeholder="First Name" />
</EditTable.Item>
</div>
))}
<Form.Item>
<Button type="dashed" onClick={() => add()}>
Add field
</Button>
<Form.ErrorList errors={errors} />
</Form.Item>
</>
),
},
{
title: 'address',
dataIndex: 'address',
width: '30%',
editable: true,
type: 'Input',
},
];
return (
<div>
<EditTable
initValue={{ address: 2193 }}
onValuesChange={(list) => setData(list)}
rowKey="key"
dataSource={data}
columns={columns}
onSave={(list) => setData(list)}
{...tableProps}
/>
</div>
);
};
export default EditableTable
操作列在第一列
import React from 'react';
import { Input, Col, InputNumber, Button, Select ,Form} from 'antd';
import EditTable from '@antdp/edit-table';
import 'antd/dist/reset.css';
const originData = [];
for (let i = 0; i < 5; i++) {
originData.push({
key: i.toString(),
name: `Edrward ${i}`,
age: 32,
// address: `London Park no. ${i}`,
});
}
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: '20%',
editable: true,
type: 'Custom',
inputNode: (edit) => {
return <Input {...edit} />;
},
},
{
title: 'age',
dataIndex: 'age',
width: '15%',
editable: true,
type: 'Input',
// rules: [{ required: true, message: '请输入' }],
inputNode: (edit) => {
return <Input {...edit} />;
},
},
{
title: 'address',
dataIndex: 'address',
width: '30%',
editable: true,
type: 'Input',
},
];
return (
<div>
<EditTable
initValue={{ address: 2193 }}
onValuesChange={(list) => setData(list)}
rowKey="key"
optIsFirst={true}
dataSource={data}
columns={columns}
onSave={(list) => setData(list)}
isAdd={true}
{...tableProps}
/>
</div>
);
};
export default EditableTable
显示删除按钮
import React from 'react';
import { Input, Col, InputNumber, Button, Select ,Form} from 'antd';
import EditTable from '@antdp/edit-table';
import 'antd/dist/reset.css';
const originData = [];
for (let i = 0; i < 5; i++) {
originData.push({
key: i.toString(),
name: `Edrward ${i}`,
age: 32,
// address: `London Park no. ${i}`,
});
}
const EditableTable =() => {
const [data, setData] = React.useState(originData);
const [tableProps, setTableProps] = React.useState({
isAdd: true,
isOpt: true,
isOptDelete:true,
optIsFirst: false,
});
const columns = [
{
title: 'name',
dataIndex: 'name',
width: '20%',
editable: true,
type: 'Custom',
inputNode: (edit) => {
return <Input {...edit} />;
},
},
{
title: 'age',
dataIndex: 'age',
width: '15%',
editable: true,
type: 'Custom',
rules: [{ required: true, message: '请输入' }],
inputNode: (edit) => {
return <Input {...edit} />;
},
},
{
title: 'address',
dataIndex: 'address',
width: '30%',
editable: true,
type: 'Input',
},
];
return (
<div>
<EditTable
initValue={{ address: 2193}}
onValuesChange={(list) => setData(list)}
rowKey="key"
dataSource={data}
columns={columns}
onSave={(list) => setData(list)}
isAdd={true}
{...tableProps}
/>
</div>
);
};
export default EditableTable
允许同时编辑多行
import React from 'react';
import { Input, Col, InputNumber, Button, Select ,Form} from 'antd';
import EditTable from '@antdp/edit-table';
import 'antd/dist/reset.css';
const originData = [];
for (let i = 0; i < 5; i++) {
originData.push({
key: i.toString(),
name: `Edrward ${i}`,
age: 32,
// address: `London Park no. ${i}`,
});
}
const EditableTable =() => {
const [data, setData] = React.useState(originData);
const [tableProps, setTableProps] = React.useState({
isAdd: true,
isOpt: true,
isOptDelete:true,
optIsFirst: false,
multiple:true
});
const columns = [
{
title: 'name',
dataIndex: 'name',
width: '20%',
editable: true,
type: 'Custom',
inputNode: (edit) => {
return <Input {...edit} />;
},
},
{
title: 'age',
dataIndex: 'age',
width: '15%',
editable: true,
type: 'Custom',
rules: [{ required: true, message: '请输入' }],
inputNode: (edit) => {
return <Input {...edit} />;
},
},
{
title: 'address',
dataIndex: 'address',
width: '30%',
editable: true,
type: 'Input',
},
];
return (
<div>
<EditTable
initValue={{ address: 2193}}
onValuesChange={(list) => setData(list)}
rowKey="key"
dataSource={data}
columns={columns}
onSave={(list) => setData(list)}
isAdd={true}
{...tableProps}
/>
</div>
);
};
export default EditableTable
无操作和新增
import React from 'react';
import { Input, Col, InputNumber, Button, Select ,Form} from 'antd';
import EditTable from '@antdp/edit-table';
import 'antd/dist/reset.css';
const originData = [];
for (let i = 0; i < 5; i++) {
originData.push({
key: i.toString(),
name: `Edrward ${i}`,
age: 32,
// address: `London Park no. ${i}`,
});
}
const EditableTable =() => {
const [data, setData] = React.useState(originData);
const [tableProps, setTableProps] = React.useState({
isOpt: false,
isAdd:false,
});
const columns = [
{
title: 'name',
dataIndex: 'name',
width: '20%',
editable: true,
type: 'Custom',
inputNode: (edit) => {
return <Input {...edit} />;
},
},
{
title: 'age',
dataIndex: 'age',
width: '15%',
editable: true,
type: 'Custom',
rules: [{ required: true, message: '请输入' }],
inputNode: (edit) => {
return <Input {...edit} />;
},
},
{
title: 'address',
dataIndex: 'address',
width: '30%',
editable: true,
type: 'Input',
},
];
return (
<div>
<EditTable
initValue={{ address: 2193}}
onValuesChange={(list) => setData(list)}
rowKey="key"
dataSource={data}
columns={columns}
onSave={(list) => setData(list)}
isAdd={true}
{...tableProps}
/>
</div>
);
};
export default EditableTable
API
参数 | 说明 | 类型 | 默认值 |
---|---|---|---|
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 | - |
isAdd | 是否存在新增按钮 | boolean | - |
initValue | 新增初始值 | any | - |
onBeforeAdd | 新增之前判断 | () => boolean | - |
onErr | 行报错信息 | (err: ValidateErrorEntity<any>) => void | - |
onValuesChange | 表单值更新事件 | (list:any) => void | - |
multiple | 是否可以多行编辑 | boolean | - |
addBtnProps | 新增按钮配置 | ButtonProps | - |
store | form 存储表单 | Store | - |
ColumnsProps
参数 | 说明 | 类型 | 默认值 |
---|---|---|---|
editable | 是否编辑 | boolean | - |
inputNode | 自定义 渲染编辑组件 | (...arg: any[]) => React.ReactNode \| React.ReactNode | - |
rules | 规则 | Rule[] | - |
itemAttr | formItem 表单 其他属性值 | Omit<FieldProps, 'rules' \| 'label' \| 'name'> | - |
attr | formItem 表单 children 中组件参数 | Partial<ItemChildAttr<any, any>> | - |
type | 组件类型 | ItemChildType | - |
tip | 错误提示 | (errs: string) => React.ReactNode | - |
tipAttr | Tooltip 组件属性 | TooltipProps | - |
isList | 是否是 List | boolean | - |
listAttr | List 组件参数 | Omit<ListProps, 'children' \| 'name'> | - |
render | 自定义 渲染(列原始默认的自定义渲染,加了个 other 参数,不是编辑状态下的表格渲染) , other 参数 只有操作列才有 | (value: any,record: any,index: number,other?: OtherProps) => React.ReactNode \| RenderedCell<any> | - |
OtherProps
参数 | 说明 | 类型 | 默认值 |
---|---|---|---|
editingKey | 编辑中字段 | any[] | - |
editable | 编辑中字段 | boolean | - |
newAdd | 当前行 是否编辑 | any[] | - |
isNewAdd | 是否新增的 | boolean | - |
save | 保存 ,key:主键 ,record:行数据 ,index:下标 | (key: string \| number, record: any, indx: number) => void | - |
cancel | 取消 , id:主键 | (id: string \| number) => void | - |
onDelete | 删除 ,id:主键 , rowItem 当前行数据 ,index:下标 | (id: string \| number, rowItem: any, index: number) => void | - |
edit | 编辑 按钮 ,record 当前行数 | (record: any) => void | - |
ref 返回值
参数 | 说明 | 类型 | 默认值 |
---|---|---|---|
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 | 编辑 id | (string \| number)[] | - |
newAdd | 是否编辑 新增的数据 | (string \| number)[] | - |
forms | 收集 所有 表单 | Store | - |
Item 组件参数
参数 | 说明 | 类型 | 默认值 |
---|---|---|---|
preName | 当前行数据存储父级的name list时不用传 | string | - |
itemValue | 当前行的所有数据 | any | - |
tipAttr | Tooltip 组件属性 | TooltipProps | - |
tip | 错误提示 | (errs: string) => React.ReactNode | - |
children | 进行覆写 方法时 新增一个 行参数 v | React.ReactNode | - |
2.1.1
1 month ago
2.1.0
1 month ago
2.0.24
1 month ago
2.0.23
6 months ago
2.0.22
7 months ago
2.0.21
9 months ago
2.0.15
11 months ago
2.0.16
11 months ago
2.0.13
11 months ago
2.0.14
11 months ago
2.0.19
11 months ago
2.0.17
11 months ago
2.0.18
11 months ago
2.0.20
11 months ago
1.9.1
12 months ago
1.9.0
12 months ago
2.0.3
12 months ago
2.0.2
1 year ago
2.0.5
12 months ago
2.0.4
12 months ago
2.0.11
12 months ago
2.0.7
12 months ago
2.0.12
12 months ago
2.0.6
12 months ago
2.0.9
12 months ago
2.0.10
12 months ago
2.0.8
12 months ago
2.0.1
1 year ago
2.0.0
1 year ago
2.0.0-bate6
1 year ago
2.0.0-bate.5
1 year ago
2.0.0-bate.4
1 year ago
2.0.0-bate.4.0
1 year ago
2.0.0-bate.3
1 year ago
2.0.0-bate-2
1 year ago
1.8.25
1 year ago
1.8.26
1 year ago
2.0.0-bate-4.1
1 year ago
1.8.27
1 year ago
2.0.0-bate-1
1 year ago
2.0.0-bate-0
1 year ago
1.8.20
2 years ago
1.8.21
2 years ago
1.8.22
2 years ago
1.8.23
2 years ago
1.8.24
2 years ago
1.8.19
2 years ago
1.8.18
2 years ago
1.8.17
2 years ago