gpt4 book ai didi

javascript - Transfer组件两侧都有树形结构

转载 作者:行者123 更新时间:2023-12-01 00:54:44 25 4
gpt4 key购买 nike

我正在使用antd's Transfer成分。使用文档中给出的示例,我能够创建一个类似于以下内容的树传输箱: enter image description here

有没有办法让我在右侧也有一个树结构?目前,当我在 0-1 下选择 0-1-0 时,它在右侧显示为平坦的。

Sandbox example 中也给出了代码,如下:

import React from 'react';
import ReactDOM from 'react-dom';
import 'antd/dist/antd.css';
import './index.css';
import { Transfer, Tree } from 'antd';

const { TreeNode } = Tree;

// Customize Table Transfer
const isChecked = (selectedKeys, eventKey) => {
return selectedKeys.indexOf(eventKey) !== -1;
};

const generateTree = (treeNodes = [], checkedKeys = []) => {
return treeNodes.map(({ children, ...props }) => (
<TreeNode {...props} disabled={checkedKeys.includes(props.key)}>
{generateTree(children, checkedKeys)}
</TreeNode>
));
};

const TreeTransfer = ({ dataSource, targetKeys, ...restProps }) => {
const transferDataSource = [];
function flatten(list = []) {
list.forEach(item => {
transferDataSource.push(item);
flatten(item.children);
});
}
flatten(dataSource);

return (
<Transfer
{...restProps}
targetKeys={targetKeys}
dataSource={transferDataSource}
className="tree-transfer"
render={item => item.title}
showSelectAll={false}
>
{({ direction, onItemSelect, selectedKeys }) => {
if (direction === 'left') {
const checkedKeys = [...selectedKeys, ...targetKeys];
return (
<Tree
blockNode
checkable
checkStrictly
defaultExpandAll
checkedKeys={checkedKeys}
onCheck={(
_,
{
node: {
props: { eventKey },
},
},
) => {
onItemSelect(eventKey, !isChecked(checkedKeys, eventKey));
}}
onSelect={(
_,
{
node: {
props: { eventKey },
},
},
) => {
onItemSelect(eventKey, !isChecked(checkedKeys, eventKey));
}}
>
{generateTree(dataSource, targetKeys)}
</Tree>
);
}
}}
</Transfer>
);
};

const treeData = [
{ key: '0-0', title: '0-0' },
{
key: '0-1',
title: '0-1',
children: [{ key: '0-1-0', title: '0-1-0' }, { key: '0-1-1', title: '0-1-1' }],
},
{ key: '0-2', title: '0-3' },
];

class App extends React.Component {
state = {
targetKeys: [],
};

onChange = targetKeys => {
console.log('Target Keys:', targetKeys);
this.setState({ targetKeys });
};

render() {
const { targetKeys } = this.state;
return (
<div>
<TreeTransfer dataSource={treeData} targetKeys={targetKeys} onChange={this.onChange} />
</div>
);
}
}

ReactDOM.render(<App />, document.getElementById('container'));

我想要的传输方式如下图所示。

  • 我希望能够转移 child
  • 当子表被转移时,左表应在其父表名称下包含剩余的子表,而右表应在其父表名称下包含转移的子表。

enter image description here

enter image description here

最佳答案

看看这个 example其中:

  • TransferTree 是检查节点并显示树路径的示例。
  • AntdTreeTransfer 传输时带有树路径的 Transfer 组件示例。

enter image description here

export const renderTreeNodes = data =>
data.map(item =>
item.children ? (
<Tree.TreeNode title={item.title} key={item.key} dataRef={item}>
{renderTreeNodes(item.children)}
</Tree.TreeNode>
) : (
<Tree.TreeNode {...item} dataRef={item} />
)
);

export const filterTree = (keys, halfKeys, rootNode) =>
rootNode
? rootNode
.filter(node => keys.includes(node.key) || halfKeys.includes(node.key))
.map(nodeRoot => ({
...nodeRoot,
children: filterTree(keys, halfKeys, nodeRoot.children)
}))
: [];

export default function TreeTransfer() {
const [checkedNodes, setCheckedNodes] = useState([]);

const onCheck = (selectedKeys, info) => {
const filteredTree = filterTree(selectedKeys, info.halfCheckedKeys, data);
setCheckedNodes(filteredTree);
};

return (
<FlexBox>
<Row type="flex" gutter={20}>
<Col>
<Card>
<Tree checkable defaultExpandAll onCheck={onCheck}>
{renderTreeNodes(data)}
</Tree>
</Card>
</Col>
<Col>
<Card>
<Tree checkable defaultExpandAll>
{renderTreeNodes(checkedNodes)}
</Tree>
</Card>
</Col>
</Row>
</FlexBox>
);
}

export default function AntdTreeTransfer() {
const [leftCheckedKeys, setLeftCheckedKeys] = useState([]);
const [checkedNodes, setCheckedNodes] = useState([]);
const [targetNodes, setTargetNodes] = useState([]);

return (
<FlexBox>
<Transfer
operations={['', 'Clear']}
onChange={(_, direction) => {
setLeftCheckedKeys([]);

direction === 'right'
? setTargetNodes(checkedNodes)
: setTargetNodes([]);
}}
style={{ width: '50vh' }}
>
{({ direction, onItemSelect, selectedKeys }) =>
direction === 'left' ? (
<Tree
showLine
blockNode
checkable
defaultExpandAll
checkedKeys={leftCheckedKeys}
onCheck={(selectedKeys, info) => {
setLeftCheckedKeys(selectedKeys);
const filteredTree = filterTree(
selectedKeys,
info.halfCheckedKeys,
data
);
setCheckedNodes(filteredTree);

const eventKey = info.node.props.eventKey;
onItemSelect(eventKey, selectedKeys.includes(eventKey));
}}
>
{renderTreeNodes(data)}
</Tree>
) : (
<Tree
autoExpandParent
blockNode
checkable
onCheck={(selectedKeys, info) => {
const eventKey = info.node.props.eventKey;
onItemSelect(eventKey, selectedKeys.includes(eventKey));
}}
>
{renderTreeNodes(targetNodes)}
</Tree>
)
}
</Transfer>
</FlexBox>
);
}

Edit Q-56680158-TreeTransfer

关于javascript - Transfer组件两侧都有树形结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56680158/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com