首页 新闻 会员 周边

React+antdui框架实现集合数据独立相互之间不依赖不受影响

0
[待解决问题]

问题场景:
我父组件有两个集合点击父组件中的标签打开子组件给我先第一个集合进行绑定数据
但是我第二个集合明明没有绑定数据但是也显示出来了,怎么让这两个集合进行独立
相互之间不受影响,使用唯一值控制,是不是需要销毁重新加载之类的操作。
拒绝bp,实现就采纳

父组件 部分关键性代码
funtion ListTreeComponets(){

// 创建三级节点 这个是我的节点代码 这个节点是动态新建的
const interfaceNode = {
title: (
<span>
<FacebookOutlined onClick={() => showModallocaDoument(formData,formData.SolutionNo)} style={{ marginLeft: '15px', marginRight: '5px' }} />

        </span>
      ),
    };


//这个方法就是打开子组件的
const showModallocaDoument=(data)=>{ 
  const internewData = {
    ...data,
    OptionControl: data.OptionControl,
    Optionlable:data.Optionlable,
  };
  const finalData = {
    ...internewData,
  };
  setisopendocmentdw(true);
  }

<IndocmentComponets
isopen={isopendocmentdw}
onClose={handleCloseModaldo}
/>

}export default ListTreeComponets

子组件
function IndocmentComponets(props){
const {
  isopen,
    onClose,
}
=props;

const columntwo = [
{
title: '表名',
dataIndex: 'table_name',
key: 'table_name',
render: (text) => (
<Tooltip title={text}>
<span>{text}</span>
</Tooltip>
),
},
{
title: '属性',
dataIndex: 'attributes',
key: 'attributes',
render: (text) => (
<Tooltip title={text}>
<span>{text}</span>
</Tooltip>
),
},
{
title: '英文名',
dataIndex: 'en_name',
key: 'en_name',
render: (text) => (
<Tooltip title={text}>
<span>{text}</span>
</Tooltip>
),
},
{
title: '中文名',
dataIndex: 'zh_name',
key: 'zh_name',
render: (text) => (
<Tooltip title={text}>
<span>{text}</span>
</Tooltip>
),
},
{
title: '字段',
dataIndex: 'fields',
key: 'fields',
render: (text) => (
<Tooltip title={text}>
<span>{text}</span>
</Tooltip>
),

    },
    {
      title: '长度',
      dataIndex: 'length',
      key: 'length',
    },
    {
      title: '名称',
      dataIndex: 'name',
      key: 'name',
      render: (text) => (
        <Tooltip title={text}>
          <span>{text}</span>
        </Tooltip>
      ),
    },
    {
      title: '类型',
      dataIndex: 'type',
      key: 'type',
      render: (text) => (
        <Tooltip title={text}>
          <span>{text}</span>
        </Tooltip>
      ),
    },,
  ];


//读取api接口数据部分 这里就是获取数据的方法

const docMetaSave = (formData) => {
const updatedFormData = formData.map((data) => ({
...data,
SolutionNo: formattribute.SolutionNo
}));
setBasicMeta(updatedFormData);
setbasicsource(updatedFormData);
console.log('追加后的from数据', updatedFormData);
};

//给table赋值
useEffect(() => {
debugger
if (!basicMeta) {
setColumntwoData([]);
return;
}
debugger
setColumntwoData(
basicMeta.map((row, index) => ({
key: index.toString(),
table_name: row.table_name,
attributes: row.attributes,
en_name: row.en_name,
zh_name: row.zh_name,
fields: row.fields,
length: row.length,
name: row.name,
type: row.type,
}))
);
debugger
console.log("读取到的数据源信息:",columntwoData)
}, [basicMeta]);

 <Table
        dataSource={columntwoData}
        columns={columntwo}
        />

}export default IndocmentComponets

实现效果:
假设我新建了多个集合并给每个集合绑定或者不绑定数据,点击不同的集合展示的效果不同。
假设集合一绑定了数据我点开集合一table中就是有数据的
假设集合二没有绑定数据我点开集合二table中就是空的
注意:集合之间不影响,我操作了集合2就不会对集合一造成影响。
我个人感觉是加载绑定数据哪里的问题,只要给他区分开来或者价格标记就可以了,具体的实现我不是清楚。
我现在绑定每次都是加载的时候给这个table赋值而这个table就一个。都是同一个tabel。




昱昇的主页 昱昇 | 初学一级 | 园豆:160
提问于:2023-07-20 15:40
< >
分享
所有回答(1)
0

根据你的描述,我理解你想实现的效果是点击父组件中的标签,打开子组件并展示不同的集合数据,每个集合数据是独立的,互不影响。子组件中的表格应该根据不同的集合数据进行展示。

你的代码中有一些问题,导致子组件中的表格数据在切换集合时没有正确展示。问题主要出现在columntwoData的设置和数据源更新上。

下面我给你一个修改后的子组件代码示例,希望能解决你的问题:

jsx
Copy code
import React, { useState, useEffect } from "react";
import { Table, Tooltip } from "antd";

function IndocmentComponets(props) {
const { isopen, onClose, data } = props;

const [columntwoData, setColumntwoData] = useState([]);

const columntwo = [
// 列定义,与你的代码相同
];

// 根据传入的data设置表格数据
useEffect(() => {
if (data) {
const updatedData = data.map((row, index) => ({
key: index.toString(),
table_name: row.table_name,
attributes: row.attributes,
en_name: row.en_name,
zh_name: row.zh_name,
fields: row.fields,
length: row.length,
name: row.name,
type: row.type,
}));
setColumntwoData(updatedData);
} else {
setColumntwoData([]);
}
}, [data]);

return (
<div>
{/* 子组件内容,包括展示表格 */}
<Table dataSource={columntwoData} columns={columntwo} />
</div>
);
}

export default IndocmentComponets;
父组件中的代码:

jsx
Copy code
import React, { useState } from "react";
import IndocmentComponets from "./IndocmentComponets";

function ListTreeComponets() {
// 假设你有两个集合数据
const collection1Data = [
// 第一个集合的数据
// ...
];

const collection2Data = [
// 第二个集合的数据
// ...
];

const [selectedCollection, setSelectedCollection] = useState(null);

// 打开子组件并设置选中的集合数据
const showModallocaDoument = (data) => {
setSelectedCollection(data);
};

// 关闭子组件
const handleCloseModaldo = () => {
setSelectedCollection(null);
};

return (
<div>
{/* 在这里渲染父组件中的标签,并点击时调用showModallocaDoument /}
{/
... */}

  {/* 渲染子组件并传入选中的集合数据 */}
  <IndocmentComponets
    isopen={Boolean(selectedCollection)}
    onClose={handleCloseModaldo}
    data={selectedCollection}
  />
</div>

);
}

export default ListTreeComponets;
上述修改后的代码中,子组件根据传入的data属性设置表格数据,父组件中的selectedCollection状态用来记录选中的集合数据,并通过showModallocaDoument方法来更新selectedCollection。每次点击不同的集合标签时,子组件的表格数据都会根据选中的集合数据进行更新,实现了不同集合之间的独立展示,互不影响。

请根据你的具体情况将示例代码调整到你的项目中,并检查是否正确实现了你想要的效果。希望这能帮助到你!

Technologyforgood | 园豆:5992 (大侠五级) | 2023-07-23 23:22
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册