2.1.1 • Published 1 month ago

@antdp/edit-table v2.1.1

Weekly downloads
-
License
MIT
Repository
github
Last release
1 month ago

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

参数说明类型默认值
columnsColumnsProps[]-
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-
storeform 存储表单Store-

ColumnsProps

参数说明类型默认值
editable是否编辑boolean-
inputNode自定义 渲染编辑组件(...arg: any[]) => React.ReactNode \| React.ReactNode-
rules规则Rule[]-
itemAttrformItem 表单 其他属性值Omit<FieldProps, 'rules' \| 'label' \| 'name'>-
attrformItem 表单 children 中组件参数Partial<ItemChildAttr<any, any>>-
type组件类型ItemChildType-
tip错误提示(errs: string) => React.ReactNode-
tipAttrTooltip 组件属性TooltipProps-
isList是否是 Listboolean-
listAttrList 组件参数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-
tipAttrTooltip 组件属性TooltipProps-
tip错误提示(errs: string) => React.ReactNode-
children进行覆写 方法时 新增一个 行参数 vReact.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