|
@@ -30,22 +30,15 @@ function DIYTable(props: IProps) {
|
|
|
|
|
|
// table数据
|
|
// table数据
|
|
const [tableData, setTableData] = useState<TableDataType[]>([{ index: 1 }]);
|
|
const [tableData, setTableData] = useState<TableDataType[]>([{ index: 1 }]);
|
|
- // table 列配置
|
|
|
|
- const [tableColumnDef, setTableColumnDef] = useState<ColumnsType<any>>([
|
|
|
|
|
|
+
|
|
|
|
+ // table列配置
|
|
|
|
+ const tableColumnDef = [
|
|
{
|
|
{
|
|
title: '序号',
|
|
title: '序号',
|
|
dataIndex: 'index',
|
|
dataIndex: 'index',
|
|
className: 'hidden',
|
|
className: 'hidden',
|
|
},
|
|
},
|
|
- ]);
|
|
|
|
-
|
|
|
|
- // const tableColumnDef: ColumnsType<any> = [
|
|
|
|
- // {
|
|
|
|
- // title: '序号',
|
|
|
|
- // dataIndex: 'index',
|
|
|
|
- // className: 'hidden',
|
|
|
|
- // },
|
|
|
|
- // ];
|
|
|
|
|
|
+ ];
|
|
|
|
|
|
// 表单填写时的表格生成
|
|
// 表单填写时的表格生成
|
|
const handleGenerateTable = () => {
|
|
const handleGenerateTable = () => {
|
|
@@ -146,39 +139,21 @@ function DIYTable(props: IProps) {
|
|
};
|
|
};
|
|
|
|
|
|
// 当仅用作展示时
|
|
// 当仅用作展示时
|
|
- const handleDisplayOnly = () => {
|
|
|
|
|
|
+ const displayColumnDef = () => {
|
|
const rows = columns;
|
|
const rows = columns;
|
|
- const newTableData = [];
|
|
|
|
- const newTableColumnDef = [
|
|
|
|
- {
|
|
|
|
- title: '序号',
|
|
|
|
- dataIndex: 'index',
|
|
|
|
- className: 'hidden',
|
|
|
|
- },
|
|
|
|
- ];
|
|
|
|
- if (rows && columns.length) {
|
|
|
|
|
|
+ if (rows && rows.length) {
|
|
for (let index = 0; index < rows.length; index++) {
|
|
for (let index = 0; index < rows.length; index++) {
|
|
// 把每一行的数据提出来到一个对象里
|
|
// 把每一行的数据提出来到一个对象里
|
|
if (rows) {
|
|
if (rows) {
|
|
const row = rows[index];
|
|
const row = rows[index];
|
|
-
|
|
|
|
if (index === 0) {
|
|
if (index === 0) {
|
|
// 列配置
|
|
// 列配置
|
|
row.forEach((col: any) => {
|
|
row.forEach((col: any) => {
|
|
- newTableColumnDef.push({ title: col.name, dataIndex: col.type, className: '' });
|
|
|
|
|
|
+ tableColumnDef.push({ title: col.name, dataIndex: col.type, className: '' });
|
|
});
|
|
});
|
|
}
|
|
}
|
|
- const rowData: any = {};
|
|
|
|
- row.forEach((col: any) => {
|
|
|
|
- rowData.index = index + 1;
|
|
|
|
- rowData[col.type] = col.value[0];
|
|
|
|
- rowData.key = col.id + index;
|
|
|
|
- });
|
|
|
|
- newTableData.push(rowData);
|
|
|
|
}
|
|
}
|
|
}
|
|
}
|
|
- setTableColumnDef(newTableColumnDef);
|
|
|
|
- setTableData(newTableData);
|
|
|
|
}
|
|
}
|
|
};
|
|
};
|
|
|
|
|
|
@@ -188,14 +163,40 @@ function DIYTable(props: IProps) {
|
|
|
|
|
|
if (!displayOnly) {
|
|
if (!displayOnly) {
|
|
handleGenerateTable();
|
|
handleGenerateTable();
|
|
|
|
+ } else {
|
|
|
|
+ displayColumnDef();
|
|
}
|
|
}
|
|
|
|
|
|
useEffect(() => {
|
|
useEffect(() => {
|
|
- if (!displayOnly) {
|
|
|
|
|
|
+ if (columns.length === 0) {
|
|
return;
|
|
return;
|
|
}
|
|
}
|
|
- handleDisplayOnly();
|
|
|
|
- }, []);
|
|
|
|
|
|
+ const rows = columns;
|
|
|
|
+ const newTableData = [];
|
|
|
|
+ if (rows && rows.length) {
|
|
|
|
+ if (displayOnly) {
|
|
|
|
+ for (let index = 0; index < rows.length; index++) {
|
|
|
|
+ // 把每一行的数据提出来到一个对象里
|
|
|
|
+ const row = rows[index];
|
|
|
|
+ const rowData: any = {};
|
|
|
|
+ if (displayOnly) {
|
|
|
|
+ row.forEach((col: any) => {
|
|
|
|
+ rowData.index = index + 1;
|
|
|
|
+ // eslint-disable-next-line prefer-destructuring
|
|
|
|
+ rowData[col.type] = col.value[0];
|
|
|
|
+ rowData.key = col.id + index;
|
|
|
|
+ });
|
|
|
|
+ newTableData.push(rowData);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ } else {
|
|
|
|
+ newTableData.push({
|
|
|
|
+ index: 1,
|
|
|
|
+ });
|
|
|
|
+ }
|
|
|
|
+ setTableData(newTableData);
|
|
|
|
+ }
|
|
|
|
+ }, [columns]);
|
|
|
|
|
|
return (
|
|
return (
|
|
<>
|
|
<>
|