相对上篇改动不大,实现选中追加,删除右边树结构,模拟数据在可以看上篇
import React, {
Component } from 'react';
import {
Tree, Input, Button,Card } from 'antd';
// import requset from '@/utils/request';
import {
cloneDeep } from 'lodash';
const {
Search } = Input;
//扁平后数据
let arr = [];
//右边扁平后数据
let arrRightKey = [];
class TransferTree extends Component {
constructor(props) {
super(props);
this.state = {
treeData: [],
expandedKeys: [],
searchValue: '',
autoExpandParent: true,
loading: false,
rightTree: DATAR,
rightTreeCopy: [],
checkedKeysLeft: [],
leftBtnDisabled: true,
rightBtnDisabled: true
};
}
componentDidMount() {
//请求树结构
//如果没有给父节点key,那就给每个子节点设置父节点key
const newDATA = cloneDeep(DATA);
const newDATAR = cloneDeep(DATAR);
this.setParent(newDATA);
// console.log(DATA);
//扁平数据
this.setnode(newDATA);
//扁平数据
this.setnodeR(newDATAR);
console.log(arr, arrRightKey);
}
//1设置父节点id
setParent = treeData => {
treeData.map(t => {
if (t.children && t.children.length) {
t.children.map(c => {
c.parentKey = t.key;
});
this.setParent(t.children);
} else {
return;
}
});
};
//2扁平数据
setnode = c => {
c.map(item => {
arr.push(item);
item.children && this.setnode(item.children);
});
};
//2扁平数据
setnodeR = c => {
c.map(item => {
arrRightKey.push(item.key);
item.children