AppTypeTree.js 3.9 KB
Newer Older
1 2 3 4 5 6
/**
 * 钟是志
 * 2019年3月29日
 * 一个年份加上校区-学院-专业的树
 * */

7
import { Input, Tree } from 'antd';
8 9
import React, { Component, Fragment } from 'react';
import Shell from '@/baseComponent/Shell';
10
import { getAppTypeList, getListByTreeList } from '@/webPublic/Services';
11
import { connect } from 'dva';
12
import { deepCopy } from '@/webPublic/zyd_public/utils/utils';
13
import BlockTitle from '@/baseComponent/BlockTitle';
14 15 16 17

const { TreeNode } = Tree;

export default class AppTypeTree extends Component {
18 19 20 21 22 23 24
	constructor() {
		super();
		this.state = {
			treeListData: [],
			name: '',
		};
	}
25

26 27 28
	handleChange = (newSearch) => {
		this.props.changeSearch(newSearch);
	};
29

30 31 32 33 34 35 36 37 38 39 40 41 42
	pushChildrenData = (treeListData, data, parentId) => {
		for (let item of treeListData) {
			if (item.id === parentId) {
				item.children = data;
				this.setState({
					treeListData: deepCopy(treeListData),
				});
				break;
			} else if (item.children && item.children.length) {
				this.pushChildrenData(item.children, data, parentId);
			}
		}
	};
43

44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
	getLeafData = (parentId) => {
		const { groupId } = this.props;
		if (!groupId) {
			return null;
		}
		getListByTreeList({
			parentId,
			groupId,
			cacheKey: Math.random(),
		}).then((res) => {
			for (let item of res) {
				if (!item.isLeaf) {
					this.getLeafData(item.id);
				}
			}
			if (res.length) {
				this.pushChildrenData(this.state.treeListData, res, parentId);
			}
		});
	};
64

65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
	getList = (name = null) => {
		const { groupId } = this.props;
		const { parentId } = this.state;
		if (!groupId) {
			return null;
		}
		getListByTreeList({
			parentId,
			groupId,
			name,
			cacheKey: Math.random(),
		}).then((treeListData) => {
			if (!treeListData || !treeListData.length) {
				this.setState({
					treeListData: [],
					loading: false,
				});
				this.handleChange({
					selectKey: '',
				});
			} else {
				for (let item of treeListData) {
					if (!item.isLeaf) {
						this.getLeafData(item.id);
					}
				}
				this.handleChange({
					selectKey: treeListData[0].id + '',
				});
				this.setState({
					treeListData,
					loading: false,
				});
			}
		});
		setTimeout(() => {
			if (document.getElementById('list-table-dom')) {
				let tableDom = document.getElementById('list-table-dom');
				this.setState({
					heightSet:
						document.body.clientHeight - 150 > tableDom.offsetHeight + 164
							? document.body.clientHeight - 150
							: tableDom.offsetHeight + 164,
				});
			}
		}, 1000);
	};
112

113 114 115
	componentDidMount() {
		this.getList();
	}
116

117 118 119 120 121
	componentDidUpdate(prevProps, prevState) {
		if (prevProps.groupId !== this.props.groupId) {
			this.getList();
		}
	}
122

123 124 125 126 127 128
	onSelect = (selectedKeys, info) => {
		this.handleChange({
			year: this.props.year,
			selectKey: selectedKeys[0],
		});
	};
129

130 131 132 133 134 135 136 137 138 139
	treeNodeList = (data) => {
		let dom = data.map((item) => {
			return (
				<TreeNode title={item.title} isLeaf={item.isLeaf} key={item.id}>
					{item.children && this.treeNodeList(item.children)}
				</TreeNode>
			);
		});
		return dom;
	};
140

141 142 143 144
	inputList = () => {
		const { name } = this.state;
		return (
			<Input.Search
145
				placeholder={'应用名称搜索'}
146 147 148 149 150 151 152
				onSearch={(value) => {
					this.getList(value);
				}}
				style={{ marginLeft: '10px', width: '150px' }}
			/>
		);
	};
153

154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
	render() {
		const { treeListData, loading, heightSet } = this.state;
		const { selectKey } = this.props;
		return (
			<Fragment>
				<Shell
					styleShell={{
						borderRight: '1px solid rgba(210,210,210,1)',
						minHeight: heightSet || document.body.clientHeight - 150,
						backgroundColor: '#fff',
						overflowX: 'auto',
						marginTop: 0,
					}}>
					<BlockTitle title={'应用'} isSmall={true} dom={this.inputList()} />
					<Tree
						onSelect={this.onSelect}
						selectedKeys={[selectKey]}
						style={{ minHeight: '400px,' }}
						disabled={loading}>
						{this.treeNodeList(treeListData)}
					</Tree>
				</Shell>
			</Fragment>
		);
	}
179
}