我从子组件中读取数据到父组件,需要在父组件中的table表格中动态的进行展示
以下是父组件中的部分代码,我在展示的时候遇到了问题无法进行展示
变量定义
const [basicMeta,setBasicMeta]=useState(null);
const [columntwoData, setColumntwoData] = useState([]);
列定义
const [columntwo, setcolumntwo] = useState([
{
title: '表名',
dataIndex: 'tablename',
key: 'tablename',
},
{
title: '属性',
dataIndex: 'attributes',
key: 'attributes',
},
{
title: '英文名',
dataIndex: 'en_name',
key: 'en_name',
},
{
title: '中文名',
dataIndex: 'zh_name',
key: 'zh_name',
},
{
title: '字段',
dataIndex: 'fields',
key: 'fields',
},
{
title: '长度',
dataIndex: 'length',
key: 'length',
},
{
title: '名称',
dataIndex: 'name',
key: 'name',
},
{
title: '类型',
dataIndex: 'type',
key: 'type',
},,
]);
//读取父组件api接口数据并赋值给定义的变量
const docMetaSave=(formData,selectedCollection)=>{
debugger
console.log(selectedCollection);
setBasicMeta({...formData});
}
读取到的数据格式如下:
basicMeta数据格式:
selectedRows
: Array(11)
0: {key: 1, value: '字段'}
1: {key: 2, value: ' PushLine'}
2: {key: 3, value: ' name'}
3: {key: 4, value: ' PushName'}
4: {key: 5, value: ' PushName'}
5: {key: 6, value: ' 09f21006-a440-48fe-d5f1-08db68911080'}
6: {key: 7, value: ' 300'}
7: {key: 8, value: ' 名称'}
8: {key: 9, value: ' nvarchar'}
9: {key: 10, value: ' 名称'}
10: {key: 11, value: 'PushLine'}
读取数据赋值给定义的变量动态生成行
useEffect(() => {
if (!basicMeta || !basicMeta.selectedRows) {
setColumntwoData([]);
return;
}
debugger
const selectedTable = basicMeta.selectedRows.find(row => row.value === 'table_name');
const titleMap = {
'table_name': '表名',
'attributes': '属性',
'en_name': '英文名',
'zh_name': '中文名',
'fields': '字段',
'length': '长度',
'name': '名称',
'type': '类型',
};
const newColumns = basicMeta.selectedRows.map(row => ({
title: titleMap[row.value],
dataIndex: row.value,
key: row.key
}));
const newTableData = {};
debugger
basicMeta.selectedRows.forEach(row => {
basicMeta.selectedRows.forEach(row => {
newTableData[row.value] = row.value;
});
});
const newColumntwoData = [{
...newTableData,
key: 'columntwo'
}];
setcolumntwo(newColumns);
setColumntwoData(selectedTable ? newColumntwoData : []);
}, [basicMeta]);
界面渲染
<Table
dataSource={columntwoData}
columns={columntwo}
/>