import logo from "./logo.svg";
import "./Layout.less";
import {Button} from "antd";
import React, {useState, useEffect} from "react";
import {Table, Input, InputNumber, Popconfirm, Form, Typography} from "antd";
import {FormInstance} from "antd/lib/form";
import {
UserOutlined,
LaptopOutlined,
NotificationOutlined,
} from "@ant-design/icons";
interface Item {
key: string;
name: string;
age: number;
address: string;
enthusiasmLevel?: number;
}
const originData: Item[] = [];
for (let i = 0; i < 3; i++) {
originData.push({
key: i.toString(),
name: `Edrward ${i}`,
age: 32,
address: `London Park no. ${i}`,
});
}
interface EditableCellProps extends React.HTMLAttributes<HTMLElement> {
editing: boolean;
dataIndex: string;
title: any;
inputType: "number" | "text";
record: Item;
index: number;
children: React.ReactNode;
}
const EditableCell: React.FC<EditableCellProps> = ({
editing,
dataIndex,
title,
inputType,
record,
index,
children,
...restProps
}) => {
const inputNode = inputType === "number" ? <InputNumber /> : <Input />;
return (
<td {...restProps}>
{editing ? (
<Form.Item
name={dataIndex}
style={{margin: 0}}
rules={[
{
required: true,
message: `Please Input ${title}!`,
},
]}
>
{inputNode}
</Form.Item>
) : (
children
)}
</td>
);
};
const EditableTable = () => {
const [form] = Form.useForm();
const [data, setData] = useState(originData);
const [editingKey, setEditingKey] = useState("");
// useEffect(() => {
// }, [data]);
const isEditing = (record: Item) => record.key === editingKey;
const edit = (record: Partial<Item> & {key: React.Key}) => {
form.setFieldsValue({name: "", age: "", address: "", ...record});
setEditingKey(record.key);
};
const cancel = () => {
setEditingKey("");
};
const save = async (key: React.Key) => {
try {
const row = (await form.validateFields()) as Item;
const newData = [...data];
const index = newData.findIndex((item) => key === item.key);
if (index > -1) {
const item = newData[index];
newData.splice(index, 1, {
...item,
...row,
});
setData(newData);
setEditingKey("");
} else {
newData.push(row);
setData(newData);
setEditingKey("");
}
} catch (errInfo) {
console.log("Validate Failed:", errInfo);
}
};
const handleDelete = (key: React.Key) => {
console.log("🚀 ~ file: Table.tsx ~ line 115 ~ handleDelete ~ key", key);
console.log("🚀 ~ data", data);
const newData = data;
// newData = newData.filter((item:any) => {item.key !== key});
const index = newData.findIndex((item: any) => key === item.key);
newData.splice(index, 1);
setData([...newData]);
};
const columns = [
{
title: "name",
dataIndex: "name",
width: "25%",
editable: true,
},
{
title: "age",
dataIndex: "age",
width: "15%",
editable: true,
},
{
title: "address",
dataIndex: "address",
width: "40%",
editable: true,
},
{
title: "operation",
dataIndex: "operation",
render: (_: any, record: Item) => {
const editable = isEditing(record);
return editable ? (
<span>
<a
href="javascript:;"
onClick={() => save(record.key)}
style={{marginRight: 8}}
>
保存
</a>
<Popconfirm title="确认取消吗?" onConfirm={cancel}>
<a>取消</a>
</Popconfirm>
</span>
) : (
<>
<Typography.Link
disabled={editingKey !== ""}
onClick={() => edit(record)}
>
编辑
</Typography.Link>
{console.log('---------',data.length)}
{data.length >= 2 ? ( // 注意此
<Popconfirm
title="确认删除吗?"
onConfirm={() => {
handleDelete(record.key);
}}
okText="确定"
cancelText="取消"
>
<a href="javascript:;">删除</a>
</Popconfirm>
) : null}
</>
);
},
},
];
const mergedColumns = columns.map((col) => {
if (!col.editable) {
return col;
}
return {
...col,
onCell: (record: Item) => ({
record,
inputType: col.dataIndex === "age" ? "number" : "text",
dataIndex: col.dataIndex,
title: col.title,
editing: isEditing(record),
}),
};
});
return (
<Form form={form} component={false}>
<Table
components={{
// 覆盖默认的 table 元素
body: {
cell: EditableCell,
},
}}
bordered // 是否展示外边框和列边框
dataSource={data}
columns={mergedColumns}
rowClassName="editable-row"
pagination={{
onChange: cancel,
}}
/>
</Form>
);
};
export default EditableTable;
table 删除操作
2021/11/17 11:39:38