最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

關(guān)于antd tree和父子組件之間的傳值問(wèn)題(react 總結(jié))

 更新時(shí)間:2021年06月01日 16:40:23   作者:棠樾  
這篇文章主要介紹了關(guān)于antd tree 和父子組件之間的傳值問(wèn)題,是小編給大家總結(jié)的一些react知識(shí)點(diǎn),本文通過(guò)一個(gè)項(xiàng)目需求實(shí)例代碼詳解給大家介紹的非常詳細(xì),需要的朋友可以參考下

項(xiàng)目需求:點(diǎn)擊產(chǎn)品樹(shù)節(jié)點(diǎn)時(shí)獲取該節(jié)點(diǎn)的所有父節(jié)點(diǎn),同時(shí)回填表格的搜索條件,完成搜索功能,搜索結(jié)果展示在下方的table中。

寫(xiě)了三個(gè)組件:

現(xiàn)在有個(gè)業(yè)務(wù)場(chǎng)景交互:在orderTree組件中點(diǎn)擊樹(shù)節(jié)點(diǎn),獲取當(dāng)前節(jié)點(diǎn)以及所有的父節(jié)點(diǎn)的Id 放入一個(gè)對(duì)象arrKeys中,并在orderForm組件中使用(回填類(lèi)型下拉選擇框,objId對(duì)象作為查詢(xún)接口的入?yún)ⅲ?/p>

現(xiàn)在可以分部解決問(wèn)題:

1.首先獲取點(diǎn)擊的樹(shù)節(jié)點(diǎn)以及所有父節(jié)點(diǎn)的id ---arrKeys

2.在點(diǎn)擊樹(shù)節(jié)點(diǎn)獲取當(dāng)前節(jié)點(diǎn)以及所有父級(jí)節(jié)點(diǎn)之后,通過(guò)this.props.idObject(arrKeys)把 arrKeys傳給父組件。

3.在tree組件和form組件中的componentDidMount生命周期中把整個(gè)組件傳給父組件

4.form組件中的inquery方法:

現(xiàn)附上tree.js代碼

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 = {}; // 當(dāng)前節(jié)點(diǎn)以及所有父節(jié)點(diǎn)的id
let firstParentKey = {}; // 一級(jí)根節(jié)點(diǎn)的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;
};
//搜索用的
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: [], //默認(rèn)展開(kāi)一級(jí)根節(jié)點(diǎn) props.commodity.defaultParentIdList
      searchValue: '',
      autoExpandParent: true,
    };
  }
  componentDidMount() {
    const { dispatch } = this.props;
    this.props.treeRef && this.props.treeRef(this); //掛載時(shí)把整個(gè)tree組件傳給父組件
    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);
      }
    }
  };

  //展開(kāi)/收起節(jié)點(diǎn)時(shí)觸發(fā)
  onExpand = expandedKeys => {
    this.setState({
      expandedKeys,
      autoExpandParent: true,
    });
  };
  //點(diǎn)擊樹(shù)節(jié)點(diǎn)時(shí)觸發(fā)
  onSelect = (selectKeys, e) => {
    const { dispatch } = this.props;
    const {
      commodity: { treeData },
    } = this.props;
    let arrKeys = {};
    //只有節(jié)點(diǎn)選中了才執(zhí)行代碼 dataRef是自定義在TreeNode上添加的屬性,可以獲取當(dāng)前節(jié)點(diǎn)的所有信息
    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);
  };
  // 搜索功能
  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) {
            //不區(qū)分大小寫(xiě)
            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()); //忽略大小寫(xiě)
        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;

父組件index.js代碼:

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);
      }
    );
  };
  //判斷是否點(diǎn)擊重置按鈕
  isReact = ref => {
    const {
      commodity: { defaultParentIdList },
    } = this.props;
    if (ref) {
      this.orderTree.setState({
        expandedKeys: defaultParentIdList,
      });
    }
  };

  render() {
    return (
      <PageHeaderWrapper logo>
        <Card bordered={false} title="商品SPU列表" 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;

以上就是關(guān)于antd tree 和父子組件之間的傳值問(wèn)題(react 總結(jié))的詳細(xì)內(nèi)容,更多關(guān)于antd tree 父子組件傳值的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • react hooks實(shí)現(xiàn)原理解析

    react hooks實(shí)現(xiàn)原理解析

    這篇文章主要介紹了react hooks實(shí)現(xiàn)原理,文中給大家介紹了useState dispatch 函數(shù)如何與其使用的 Function Component 進(jìn)行綁定,節(jié)后實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下
    2022-10-10
  • react-router 路由切換動(dòng)畫(huà)的實(shí)現(xiàn)示例

    react-router 路由切換動(dòng)畫(huà)的實(shí)現(xiàn)示例

    這篇文章主要介紹了react-router 路由切換動(dòng)畫(huà)的實(shí)現(xiàn)示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-12-12
  • ReactNative環(huán)境搭建的教程

    ReactNative環(huán)境搭建的教程

    這篇文章主要介紹了ReactNative環(huán)境搭建的教程,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2025-04-04
  • react搭建環(huán)境時(shí)執(zhí)行npm start報(bào)錯(cuò)start: 'react-scripts start'的解決

    react搭建環(huán)境時(shí)執(zhí)行npm start報(bào)錯(cuò)start: 'react-scripts&

    這篇文章主要介紹了react搭建環(huán)境時(shí)執(zhí)行npm start報(bào)錯(cuò)start: 'react-scripts start'的解決方案,具有很好的參考價(jià)值,希望杜對(duì)大家有所幫助,
    2023-10-10
  • React使用highlight.js Clipboard.js實(shí)現(xiàn)代碼高亮復(fù)制

    React使用highlight.js Clipboard.js實(shí)現(xiàn)代碼高亮復(fù)制

    這篇文章主要為大家介紹了React使用highlight.js Clipboard.js實(shí)現(xiàn)代碼高亮復(fù)制功能示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-04-04
  • React代碼分割的實(shí)現(xiàn)方法介紹

    React代碼分割的實(shí)現(xiàn)方法介紹

    雖然一直有做react相關(guān)的優(yōu)化,按需加載、dll 分離、服務(wù)端渲染,但是從來(lái)沒(méi)有從路由代碼分割這一塊入手過(guò),所以下面這篇文章主要給大家介紹了關(guān)于React中代碼分割的方式,需要的朋友可以參考下
    2022-12-12
  • react表單受控的實(shí)現(xiàn)方案

    react表單受控的實(shí)現(xiàn)方案

    數(shù)據(jù)的受控控制一直是react里的一個(gè)痛點(diǎn),當(dāng)我想要實(shí)現(xiàn)一個(gè)輸入框的受控控制時(shí),我需要定義一個(gè)onChange和value,手動(dòng)去實(shí)現(xiàn)數(shù)據(jù)的綁定,本文小編給大家介紹了react表單受控的實(shí)現(xiàn)方案,需要的朋友可以參考下
    2023-12-12
  • 你知道怎么基于?React?封裝一個(gè)組件嗎

    你知道怎么基于?React?封裝一個(gè)組件嗎

    這篇文章主要為大家詳細(xì)介紹了React如何封裝一個(gè)組件,使用antd的divider組件來(lái)講解,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-01-01
  • React性能優(yōu)化的實(shí)現(xiàn)方法詳解

    React性能優(yōu)化的實(shí)現(xiàn)方法詳解

    react憑借virtual DOM和diff算法擁有高效的性能,除此之外也有很多其他的方法和技巧可以進(jìn)一步提升react性能,在本文中我將列舉出可有效提升react性能的幾種方法,幫助我們改進(jìn)react代碼,提升性能
    2023-01-01
  • React Hook中useState更新延遲問(wèn)題及解決

    React Hook中useState更新延遲問(wèn)題及解決

    這篇文章主要介紹了React Hook中useState更新延遲問(wèn)題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-08-08

最新評(píng)論

炎陵县| 区。| 津南区| 富宁县| 鲜城| 赞皇县| 呼和浩特市| 扬中市| 祁阳县| 南宁市| 萨嘎县| 隆林| 秦安县| 昭觉县| 绥阳县| 孝感市| 宁南县| 邵东县| 焦作市| 章丘市| 新田县| 宁都县| 宜兰县| 桃江县| 涞水县| 嘉鱼县| 昌黎县| 汝城县| 西乌珠穆沁旗| 锦州市| 堆龙德庆县| 祁门县| 孝义市| 孝昌县| 威信县| 偃师市| 台东县| 南康市| 遵义市| 班戈县| 黑河市|