|
@@ -30,14 +30,76 @@ function DIYTable(props: IProps) {
|
|
const { table, columns, displayOnly, onChange } = props;
|
|
const { table, columns, displayOnly, onChange } = props;
|
|
// table数据
|
|
// table数据
|
|
const [tableData, setTableData] = useState<TableDataType[]>([{ index: 1 }]);
|
|
const [tableData, setTableData] = useState<TableDataType[]>([{ index: 1 }]);
|
|
- // table 列配置
|
|
|
|
- const [tableColumnDef, setTableColumnDef] = useState<ColumnsType<any>>([
|
|
|
|
|
|
+
|
|
|
|
+ // 列配置
|
|
|
|
+ const tableColumnDef: ColumnsType<any> = [
|
|
{
|
|
{
|
|
title: '序号',
|
|
title: '序号',
|
|
dataIndex: 'index',
|
|
dataIndex: 'index',
|
|
className: 'hidden',
|
|
className: 'hidden',
|
|
},
|
|
},
|
|
- ]);
|
|
|
|
|
|
+ ];
|
|
|
|
+
|
|
|
|
+ const [commitValue, setCommitValue] = useState<any[]>([]);
|
|
|
|
+
|
|
|
|
+ const handleValueChange = (value: any, id: string, label: string) => {
|
|
|
|
+ let ids = id.split(';');
|
|
|
|
+ let [rowIndex, colIndex] = ids[0].split(',').map((item) => Number(item));
|
|
|
|
+ let [columnID, tableID] = ids[1].split('>');
|
|
|
|
+ let [columnLabel, tableLabel] = label.split('>');
|
|
|
|
+
|
|
|
|
+ const cell = {
|
|
|
|
+ name: columnLabel,
|
|
|
|
+ id: columnID,
|
|
|
|
+ type: columnID.split('_')[0],
|
|
|
|
+ value: [value],
|
|
|
|
+ };
|
|
|
|
+ // 组装可能用到的数据
|
|
|
|
+ const cols = [];
|
|
|
|
+ cols[colIndex] = cell;
|
|
|
|
+ const rows = [];
|
|
|
|
+ rows[rowIndex] = cols;
|
|
|
|
+
|
|
|
|
+ const newValues = JSON.parse(JSON.stringify(commitValue)) || [];
|
|
|
|
+
|
|
|
|
+ if (newValues.length !== 0 && newValues[rowIndex] !== undefined) {
|
|
|
|
+ newValues.forEach((row: any, rindex: number) => {
|
|
|
|
+ if (rindex === rowIndex) {
|
|
|
|
+ if (row[colIndex] !== undefined) {
|
|
|
|
+ row.forEach((_col: any, cindex: number) => {
|
|
|
|
+ if (cindex === colIndex) {
|
|
|
|
+ row[colIndex] = cell;
|
|
|
|
+ }
|
|
|
|
+ });
|
|
|
|
+ } else {
|
|
|
|
+ row[colIndex] = cell;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ });
|
|
|
|
+ } else {
|
|
|
|
+ newValues[rowIndex] = cols;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ const textNode = columns?.find((item) => item.componentName === 'TextNote');
|
|
|
|
+ if (textNode) {
|
|
|
|
+ const textNodeIndex = columns?.findIndex(
|
|
|
|
+ (item) => item.componentName === 'TextNote',
|
|
|
|
+ );
|
|
|
|
+ const textCell = {
|
|
|
|
+ name: textNode.props.label,
|
|
|
|
+ id: textNode.props.id,
|
|
|
|
+ type: textNode.componentName,
|
|
|
|
+ value: [textNode.props.placeholder],
|
|
|
|
+ };
|
|
|
|
+ newValues.forEach((item) => {
|
|
|
|
+ if (textNodeIndex !== -1) {
|
|
|
|
+ item[textNodeIndex] = textCell;
|
|
|
|
+ }
|
|
|
|
+ });
|
|
|
|
+ }
|
|
|
|
+ setCommitValue(newValues);
|
|
|
|
+ onChange(newValues);
|
|
|
|
+ };
|
|
|
|
|
|
// 表单填写时的表格生成
|
|
// 表单填写时的表格生成
|
|
const handleGenerateTable = () => {
|
|
const handleGenerateTable = () => {
|
|
@@ -63,7 +125,13 @@ function DIYTable(props: IProps) {
|
|
style={{ padding: '0', margin: '0' }}
|
|
style={{ padding: '0', margin: '0' }}
|
|
options={column.props.options}
|
|
options={column.props.options}
|
|
disabled={column.props.disabled}
|
|
disabled={column.props.disabled}
|
|
- onChange={onChange}
|
|
|
|
|
|
+ onChange={(value: any) => {
|
|
|
|
+ handleValueChange(
|
|
|
|
+ value,
|
|
|
|
+ id,
|
|
|
|
+ columnLabel + '>' + table.props.label,
|
|
|
|
+ );
|
|
|
|
+ }}
|
|
/>
|
|
/>
|
|
);
|
|
);
|
|
};
|
|
};
|
|
@@ -81,7 +149,13 @@ function DIYTable(props: IProps) {
|
|
placeholder={column.props.placeholder}
|
|
placeholder={column.props.placeholder}
|
|
format={column.props.format}
|
|
format={column.props.format}
|
|
disabled={column.props.disabled}
|
|
disabled={column.props.disabled}
|
|
- onChange={onChange}
|
|
|
|
|
|
+ onChange={(value: any) => {
|
|
|
|
+ handleValueChange(
|
|
|
|
+ value,
|
|
|
|
+ id,
|
|
|
|
+ columnLabel + '>' + table.props.label,
|
|
|
|
+ );
|
|
|
|
+ }}
|
|
/>
|
|
/>
|
|
);
|
|
);
|
|
};
|
|
};
|
|
@@ -99,7 +173,13 @@ function DIYTable(props: IProps) {
|
|
style={{ padding: '4px 11px' }}
|
|
style={{ padding: '4px 11px' }}
|
|
disabled={column.props.disabled}
|
|
disabled={column.props.disabled}
|
|
unit={column.props.unit}
|
|
unit={column.props.unit}
|
|
- onChange={onChange}
|
|
|
|
|
|
+ onChange={(value: any) => {
|
|
|
|
+ handleValueChange(
|
|
|
|
+ value,
|
|
|
|
+ id,
|
|
|
|
+ columnLabel + '>' + table.props.label,
|
|
|
|
+ );
|
|
|
|
+ }}
|
|
/>
|
|
/>
|
|
);
|
|
);
|
|
};
|
|
};
|
|
@@ -112,13 +192,13 @@ function DIYTable(props: IProps) {
|
|
<Input
|
|
<Input
|
|
disabled={column.props.disabled}
|
|
disabled={column.props.disabled}
|
|
placeholder={column.props.placeholder}
|
|
placeholder={column.props.placeholder}
|
|
- onChange={(e) =>
|
|
|
|
- onChange?.(
|
|
|
|
|
|
+ onChange={(e: React.ChangeEvent<HTMLInputElement>) => {
|
|
|
|
+ handleValueChange(
|
|
e.target.value,
|
|
e.target.value,
|
|
id,
|
|
id,
|
|
columnLabel + '>' + table.props.label,
|
|
columnLabel + '>' + table.props.label,
|
|
- )
|
|
|
|
- }
|
|
|
|
|
|
+ );
|
|
|
|
+ }}
|
|
/>
|
|
/>
|
|
);
|
|
);
|
|
};
|
|
};
|
|
@@ -146,30 +226,25 @@ function DIYTable(props: IProps) {
|
|
// 当仅用作展示时
|
|
// 当仅用作展示时
|
|
const handleDisplayOnly = () => {
|
|
const handleDisplayOnly = () => {
|
|
const rows = columns;
|
|
const rows = columns;
|
|
- const newTableData = [];
|
|
|
|
- const newColumnDef: ColumnsType<any> = [];
|
|
|
|
- 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) => {
|
|
- newColumnDef.push({ title: col.name, dataIndex: col.type });
|
|
|
|
|
|
+ if (col) {
|
|
|
|
+ tableColumnDef.push({
|
|
|
|
+ title: col?.name || '',
|
|
|
|
+ dataIndex: col?.type || '',
|
|
|
|
+ className: '',
|
|
|
|
+ });
|
|
|
|
+ }
|
|
});
|
|
});
|
|
}
|
|
}
|
|
- let rowData: any = {};
|
|
|
|
- row.forEach((col: any) => {
|
|
|
|
- rowData[col.type] = col.value[0];
|
|
|
|
- rowData.key = col.id + index;
|
|
|
|
- });
|
|
|
|
- newTableData.push(rowData);
|
|
|
|
}
|
|
}
|
|
}
|
|
}
|
|
- setTableColumnDef(newColumnDef);
|
|
|
|
- setTableData(newTableData);
|
|
|
|
}
|
|
}
|
|
};
|
|
};
|
|
|
|
|
|
@@ -177,17 +252,48 @@ function DIYTable(props: IProps) {
|
|
setTableData([...tableData, { index: tableData.length }]);
|
|
setTableData([...tableData, { index: tableData.length }]);
|
|
};
|
|
};
|
|
|
|
|
|
|
|
+ if (displayOnly) {
|
|
|
|
+ handleDisplayOnly();
|
|
|
|
+ } else {
|
|
|
|
+ handleGenerateTable();
|
|
|
|
+ }
|
|
|
|
+
|
|
useEffect(() => {
|
|
useEffect(() => {
|
|
- if (displayOnly) {
|
|
|
|
- handleDisplayOnly();
|
|
|
|
- } else {
|
|
|
|
- handleGenerateTable();
|
|
|
|
|
|
+ if (columns?.length === 0) {
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+ 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) => {
|
|
|
|
+ if (col) {
|
|
|
|
+ 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 (
|
|
<>
|
|
<>
|
|
- {table.name ? table.name : table.props.label}
|
|
|
|
|
|
+ {/* {table.name ? table.name : table.props.label} */}
|
|
<Table
|
|
<Table
|
|
style={displayOnly ? { margin: '10px 24px 10px 0' } : {}}
|
|
style={displayOnly ? { margin: '10px 24px 10px 0' } : {}}
|
|
columns={tableColumnDef}
|
|
columns={tableColumnDef}
|