Project requirements: When you click a product tree node, get all the parent nodes of the node, and fill in the search conditions in the table to complete the search function. The search results are displayed in the table below. Three components were written: Now there is a business scenario interaction: click on the tree node in the orderTree component, get the ID of the current node and all parent nodes, put them into an object arrKeys, and use it in the orderForm component (backfill the type drop-down selection box, and use the objId object as the input parameter of the query interface) Now we can solve the problem in parts: 1. First get the ids of the clicked tree node and all parent nodes ---arrKeys 2. After clicking the tree node to get the current node and all parent nodes, pass arrKeys to the parent component through this.props.idObject(arrKeys). 3. Pass the entire component to the parent component in the componentDidMount lifecycle in the tree component and form component 4. The inquery method in the form component: The tree.js code is now attached import React, { Component } from 'react'; import { connect } from 'dva'; import { Divider, Modal, Table, message, Tag, Spin } from 'antd'; import router from 'umi/router'; import style from '../style.less'; import { Tree, Input } from 'antd'; const { confirm } = Modal; const { TreeNode } = Tree; const { Search } = Input; let dataList = []; let keysObj = {}; // The id of the current node and all parent nodes let firstParentKey = {}; // first-level root node id const intetorFun = (data, key, string) => { if (string) { firstParentKey = { [data.param]: data.paramId, }; } if (data.children && data.children.length !== 0) { data.children.forEach(item => { if (item.id === key[0]) { keysObj = { [data.param]: data.paramId, [item.param]: item.paramId, ...firstParentKey, }; } else { intetorFun(item, key); } }); } return keysObj; }; const getParentKey = (key, tree) => { let parentKey = []; for (let i = 0; i < tree.length; i++) { const node = tree[i]; parentKey = intetorFun(node, key, 'firstTime'); } return parentKey; }; //Search const getSearchKey = (key, tree) => { let parentKey; for (let i = 0; i < tree.length; i++) { const node = tree[i]; if (node.children) { if (node.children.some(item => item.id === key)) { parentKey = node.id; } else if (getSearchKey(key, node.children)) { parentKey = getSearchKey(key, node.children); } } else { if (node.id === key) { parentKey = node.id; } } } return parentKey; }; @connect(({ commodity, loading, menu }) => ({ commodity, loading: loading.effects['commodity/getTree'], menu, })) class OrderTree extends Component { constructor(props) { super(props); this.state = { expandedKeys: [], //Default expansion of the first-level root node props.commodity.defaultParentIdList searchValue: '', autoExpandParent: true, }; } componentDidMount() { const { dispatch } = this.props; this.props.treeRef && this.props.treeRef(this); //Pass the entire tree component to the parent component when mounting dispatch({ type: 'commodity/getTree', callback: res => { this.generateList(res.data); const defaultParentIdList = res.data.map(item => item.id); this.setState({ expandedKeys: defaultParentIdList, }); }, }); } generateList = data => { const { dispatch } = this.props; for (let i = 0; i < data.length; i++) { const node = data[i]; const { id, name } = node; dataList.push({ id, name }); dispatch({ type: 'commodity/save', payload: { dataList, }, }); if (node.children) { this.generateList(node.children); } } }; //Triggered when expanding/collapsed nodes onExpand = expandedKeys => { this.setState({ expandedKeys, autoExpandParent: true, }); }; //Triggered when clicking a tree node onSelect = (selectKeys, e) => { const { dispatch } = this.props; const { commodity: { treeData }, } = this.props; let arrKeys = {}; //The code is executed only when the node is selected dataRef is a custom attribute added to TreeNode, which can get all the information of the current node if (e.selected && e.node.props.dataRef.param !== 'categoryId') { keysObj = {}; firstParentKey = {}; arrKeys = getParentKey(selectKeys, treeData); } else if (e.selected && e.node.props.dataRef.param === 'categoryId') { keysObj = {}; firstParentKey = {}; arrKeys = { categoryId: e.node.props.dataRef.paramId, }; } else if (!e.selected) { return false; } this.props.idObject(arrKeys); }; // Search function onChange = e => { const { value } = e.target; const { commodity: { treeData, dataList, defaultParentIdList }, } = this.props; let expandedKeys = []; if (value) { expandedKeys = dataList .map(item => { if (item.name.toLowerCase().indexOf(value.toLowerCase()) > -1) { //Case insensitive return getSearchKey(item.id, treeData); } return null; }) .filter((item, i, self) => item && self.indexOf(item) === i); this.setState({ expandedKeys, searchValue: value, autoExpandParent: true, }); } else { this.setState({ expandedKeys: defaultParentIdList, searchValue: '', autoExpandParent: true, }); } }; render() { const { searchValue, expandedKeys, autoExpandParent } = this.state; const { commodity: { treeData }, loading, } = this.props; const loop = data => data.map(item => { const index = item.name.toLowerCase().indexOf(searchValue.toLowerCase()); //Ignore case const beforeStr = item.name.substr(0, index); const afterStr = item.name.substr(index + searchValue.length); const centerStr = item.name.substr(index, searchValue.length); const title = index > -1 ? ( <span title={item.name}> {beforeStr} <span style={{ color: '#f50' }}>{centerStr}</span> {afterStr} </span> ) : ( <span title={item.name}>{item.name}</span> ); if (item.children) { return ( <TreeNode key={item.id} title={title} dataRef={item}> {loop(item.children)} </TreeNode> ); } return <TreeNode key={item.id} title={title} dataRef={item} />; }); return ( <Spin spinning={loading}> <div> <Search style={{ marginBottom: 8 }} placeholder="Search" onChange={this.onChange} /> <Tree onExpand={this.onExpand} onSelect={this.onSelect} expandedKeys={expandedKeys} autoExpandParent={autoExpandParent} > {loop(treeData)} </Tree> </div> </Spin> ); } } export default OrderTree; Parent component index.js code: import React, { Component } from 'react'; import { connect } from 'dva'; import { formatMessage, FormattedMessage } from 'umi/locale'; import { Card, Spin } from 'antd'; import PageHeaderWrapper from '@/components/PageHeaderWrapper'; import OrderForm from './components/form'; import OrderTable from './components/table'; import OrderTree from './components/tree'; import style from './style.less'; import { consoleTestResultHandler } from 'tslint/lib/test'; // let dataList = []; @connect(({ commodity, loading, menu }) => ({ commodity, loading: loading.effects['commodity/getTree'], menu, })) class OrderPage extends Component { constructor() { super(); this.state = { idObject: {}, reactFlag: false, }; } componentDidMount() { const { dispatch } = this.props; dispatch({ type: 'commodity/getGoodsCategory', }); } onRef = ref => { this.orderForm = ref; }; treeRef = ref => { this.orderTree = ref; }; getIdObject = data => { this.setState( { idObject: data, }, () => { this.orderForm.props.form.setFieldsValue({ categoryIds: [String(data.categoryId)], }); this.orderForm.inquery(data); } ); }; //Judge whether the reset button is clicked isReact = ref => { const { commodity: { defaultParentIdList }, } = this.props; if (ref) { this.orderTree.setState({ expandedKeys: defaultParentIdList, }); } }; render() { return ( <PageHeaderWrapper logo> <Card bordered={false} title="Product SPU List" className={style.antCardBox}> <div style={{ width: '350px', marginRight: '30px', boxShadow: '3px -3px 6px 0px #ccc6' }} className={style.antTreeBox} > <OrderTree idObject={this.getIdObject} treeRef={this.treeRef} /> </div> <div style={{ flex: '1' }}> <OrderForm onRef={this.onRef} isReact={this.isReact} /> <OrderTable /> </div> </Card> </PageHeaderWrapper> ); } } export default OrderPage; The above is the details about the value transfer problem between antd tree and parent-child components (react summary). For more information about the value transfer between antd tree parent-child components, please pay attention to other related articles on 123WORDPRESS.COM! You may also be interested in:
|
<<: Sample code for the test script for indexes and locks at RR and RC isolation levels
>>: Sample code for installing ASPNET.Core3.0 runtime in Linux
There are many seemingly true "rumors" ...
The problems and solutions encountered when insta...
Achieve results Implementation Code html <div ...
background Sometimes we need to get the creation ...
The overall architecture of MySQL is divided into...
This article shares with you how to use bootstrap...
The problem is as follows: I entered the command ...
The crontab command is used by Unix and Linux to ...
HTML5 is the next version of the HTML standard. M...
Disadvantages of Tables 1. Table takes up more byt...
Preface Nginx (pronounced "engine X") i...
Table of contents 1. Install axios 2. Use of axio...
Vue+js realizes the fade in and fade out of the v...
Table of contents What is a partition table Parti...
<body> <div id="root"> <...