<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: sdalal</title>
    <description>The latest articles on Forem by sdalal (@sdalal).</description>
    <link>https://forem.com/sdalal</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F847091%2Fec48ff5d-8d4f-4977-b96c-54bbf0df35c5.png</url>
      <title>Forem: sdalal</title>
      <link>https://forem.com/sdalal</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/sdalal"/>
    <language>en</language>
    <item>
      <title>React Help: On Link Click, how to expand all and collapse all tree nodes using React?</title>
      <dc:creator>sdalal</dc:creator>
      <pubDate>Thu, 14 Apr 2022 00:05:34 +0000</pubDate>
      <link>https://forem.com/sdalal/react-help-on-link-click-how-to-expand-all-and-collapse-all-tree-nodes-using-react-54pp</link>
      <guid>https://forem.com/sdalal/react-help-on-link-click-how-to-expand-all-and-collapse-all-tree-nodes-using-react-54pp</guid>
      <description>&lt;p&gt;I generate a tree from xml file using react. I have two links Expand All and Collapse All. How to expand all tree nodes if tree is in Collapse Mode when Expand All link will click? How to Collapse all tree nodes if tree is in Expand Mode when Collapse All link will be clicked?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;App.js
import React, { Component } from 'react';
import { Switch, Route, Link } from "react-router-dom";
import { BrowserRouter } from 'react-router-dom';
import Home from './Home';

export class App extends Component {
  constructor(props) {
    super(props);
  }

render() {
    return (
        &amp;lt;BrowserRouter&amp;gt;
            &amp;lt;Switch&amp;gt;
{/* Otherwise, render the Landing component */}
                &amp;lt;Route component={Home} /&amp;gt;
            &amp;lt;/Switch&amp;gt;
        &amp;lt;/BrowserRouter&amp;gt;
    );
}
}

export default App;
-------------------------------

Home.js

import React, { Component } from 'react';
import ProductsTree from './ProductsTreeView';

class Home extends React.Component {
    constructor(props) {
        super(props);
this.state =
        {
currentNode: {},
        };
        this.setCurrentNode = this.setCurrentNode.bind(this);
    }

    setCurrentNode(node) {
        this.setState({ currentNode: node });
    }

    render() {
        return (
&amp;lt;div&amp;gt;
                    &amp;lt;table width="100%" border="0" cellSpacing="0" cellPadding="0"&amp;gt;
                        &amp;lt;tbody&amp;gt;
                            &amp;lt;tr width="100%"&amp;gt;
                                &amp;lt;td align="left" nowrap="true"&amp;gt;
&amp;lt;b&amp;gt;&amp;lt;a href=’’&amp;gt;Expand All&amp;lt;/a&amp;gt;&amp;lt;/b&amp;gt;
&amp;lt;b&amp;gt;&amp;lt;a href=’’&amp;gt;Collapse All&amp;lt;/a&amp;gt;&amp;lt;/b&amp;gt;
    &amp;lt;/td&amp;gt;
                            &amp;lt;/tr&amp;gt;
                        &amp;lt;/tbody&amp;gt;
                    &amp;lt;/table&amp;gt;
                    &amp;lt;br /&amp;gt;
                    &amp;lt;ProductsTree setCurrentNode={this.setCurrentNode} /&amp;gt;
&amp;lt;/div&amp;gt;

);
    }
}

export default Home;

-------------------------------

ProductsTreeView.js

import React, { Component } from 'react';
import axios from 'axios';
import XMLParser from 'react-xml-parser';
import tree from './tree.xml'


class ProductsTreeView extends Component {
    render() {
        return (
            &amp;lt;div id="TreeView"&amp;gt;
                &amp;lt;TreeView setCurrentNode={this.props.setCurrentNode} /&amp;gt;
            &amp;lt;/div&amp;gt;
        );
    }
}

class Node {
    description = 'n/a';
    id = -1;
    key_id = -1;
    linkpagename = '';
    isActive = false;
    nodes = [];

    constructor(description, id, key_id, linkpagename) {
        this.description = description;
        this.id = id;
        this.key_id = key_id;
        this.linkpagename = linkpagename;
    }

    static nodesFromXml(xml) {
        const map = (entity, nodes) =&amp;gt; {
            const id = entity.attributes['id'];
            const key_id = entity.attributes['key-id'];
            const descriptionText =
                entity.children[
                    entity.children.findIndex((child) =&amp;gt; child.name === 'description')
                ].value;
            const entities = entity.children.filter(
                (child) =&amp;gt; child.name === 'entity'
            );
            var linkPageName = entity.attributes['link-page-name'];
     const node = new Node(descriptionText, id, key_id, linkPageName);
            nodes.push(node);
            entities.forEach((entity) =&amp;gt; map(entity, node.nodes));
        };

        const parsedData = new XMLParser().parseFromString(xml);

        const entities = parsedData.children.filter(
            (child) =&amp;gt; child.name === 'entity'
        );

        const nodes = [];
        entities.forEach((entity) =&amp;gt; map(entity, nodes));
        return nodes;
    }
}

class TreeView extends React.Component {
    constructor(props) {
        super(props);
        this.state = { nodes: [] };
        this.toggleNode = this.toggleNode.bind(this);
    }

    componentDidMount() {
        axios
            .get(tree, { 'Content-Type': 'application/xml; charset=utf-8' })
            .then((response) =&amp;gt;
                this.setState({ nodes: Node.nodesFromXml(response.data) })
            );
    }

    render() {
        const nodes = this.state.nodes;

        return (
            &amp;lt;ul&amp;gt;
                {nodes.map((node) =&amp;gt; (
                    &amp;lt;TreeNode
                        id={node.id}
                        key={node.key_id}
                        node={node}
                        onToggle={this.toggleNode}
                        setCurrentNode={this.props.setCurrentNode}
                    /&amp;gt;
                ))}
            &amp;lt;/ul&amp;gt;
        );
    }

    toggleNode(node) {
        if (node.nodes.length === 0) return;
        this.props.setCurrentNode(node);

        function _toggleNode(currentNode, node) {
            if (currentNode.id === node.id) {
                currentNode.isActive = !currentNode.isActive;
            } else {
                currentNode.nodes.forEach((childNode) =&amp;gt; _toggleNode(childNode, node));
            }
        }

        const nodes = this.state.nodes;
        nodes.forEach((currentNode) =&amp;gt; _toggleNode(currentNode, node));
        this.setState((state) =&amp;gt; (state.nodes = nodes));
    }
}

class TreeNode extends React.Component {
    render() {
        const node = this.props.node;
        const onToggle = this.props.onToggle;
        let acitveChildren = null;
        if (node.isActive &amp;amp;&amp;amp; node.nodes.length &amp;gt; 0) {
            acitveChildren = (
                &amp;lt;ul&amp;gt;
                    {node.nodes.map((node) =&amp;gt; (
                        &amp;lt;TreeNode
                            id={node.id}
                            key={node.key_id}
                            node={node}
                            onToggle={onToggle}
                        /&amp;gt;
                    ))}
                &amp;lt;/ul&amp;gt;
            );
        }

        return (
            &amp;lt;li
                id={node.id} linkpagename={node.linkpagename}
                key={node.key_id}
                onClick={(event) =&amp;gt; {
                    event.stopPropagation();
                    onToggle(node);
                }}
            &amp;gt;
                {node.description} - {node.key_id} - {node.linkpagename}
                {acitveChildren}
            &amp;lt;/li&amp;gt;
        );
    }
}

export default ProductsTreeView;

---------------------------------------
tree.xml
&amp;lt;?xml version="1.0" standalone="yes"?&amp;gt;
&amp;lt;tree&amp;gt;
    &amp;lt;entity id="e11" key-id="1" link-page-name="Add_Item"&amp;gt;
        &amp;lt;description&amp;gt;Service&amp;lt;/description&amp;gt;
        &amp;lt;image&amp;gt;images/plus.gif&amp;lt;/image&amp;gt;
        &amp;lt;imageNode&amp;gt;images/de.gif&amp;lt;/imageNode&amp;gt;
        &amp;lt;imageOpen&amp;gt;images/minus.gif&amp;lt;/imageOpen&amp;gt;
        &amp;lt;entity id="e248" key-id="48" link-page-name="Add_SubItem"&amp;gt;
            &amp;lt;description&amp;gt;A_test1_test1&amp;lt;/description&amp;gt;
            &amp;lt;image&amp;gt;images/plus.gif&amp;lt;/image&amp;gt;
            &amp;lt;imageNode&amp;gt;images/de.gif&amp;lt;/imageNode&amp;gt;
            &amp;lt;imageOpen&amp;gt;images/minus.gif&amp;lt;/imageOpen&amp;gt;
            &amp;lt;entity id="e3717" key-id="717" link-page-name="Edit_SubItem"&amp;gt;
                &amp;lt;description&amp;gt;A_SubItem1&amp;lt;/description&amp;gt;
                &amp;lt;image&amp;gt;images/plus.gif&amp;lt;/image&amp;gt;
                &amp;lt;imageNode&amp;gt;images/de.gif&amp;lt;/imageNode&amp;gt;
                &amp;lt;imageOpen&amp;gt;images/minus.gif&amp;lt;/imageOpen&amp;gt;
                &amp;lt;entity id="e45546" key-id="5546" link-page-name="Edit_ItemTemplate"&amp;gt;
                    &amp;lt;description&amp;gt;A_Test_Template&amp;lt;/description&amp;gt;
                    &amp;lt;image&amp;gt;images/paper.gif&amp;lt;/image&amp;gt;
                    &amp;lt;imageOpen&amp;gt;images/paper.gif&amp;lt;/imageOpen&amp;gt;
                &amp;lt;/entity&amp;gt;
            &amp;lt;/entity&amp;gt;
        &amp;lt;/entity&amp;gt;
        &amp;lt;entity id="e21" key-id="1" link-page-name="Add_SubItem"&amp;gt;
            &amp;lt;description&amp;gt;Checking&amp;lt;/description&amp;gt;
            &amp;lt;image&amp;gt;images/plus.gif&amp;lt;/image&amp;gt;
            &amp;lt;imageNode&amp;gt;images/de.gif&amp;lt;/imageNode&amp;gt;
            &amp;lt;imageOpen&amp;gt;images/minus.gif&amp;lt;/imageOpen&amp;gt;
            &amp;lt;entity id="e3606" key-id="606" link-page-name="Edit_SubItem"&amp;gt;
                &amp;lt;description&amp;gt;Categories&amp;lt;/description&amp;gt;
                &amp;lt;image&amp;gt;images/plus.gif&amp;lt;/image&amp;gt;
                &amp;lt;imageNode&amp;gt;images/de.gif&amp;lt;/imageNode&amp;gt;
                &amp;lt;imageOpen&amp;gt;images/minus.gif&amp;lt;/imageOpen&amp;gt;
                &amp;lt;entity id="e45112" key-id="5112" link-page-name="Edit_ItemTemplate"&amp;gt;
                    &amp;lt;description&amp;gt;All states&amp;lt;/description&amp;gt;
                    &amp;lt;image&amp;gt;images/paper.gif&amp;lt;/image&amp;gt;
                    &amp;lt;imageOpen&amp;gt;images/paper.gif&amp;lt;/imageOpen&amp;gt;
                &amp;lt;/entity&amp;gt;
            &amp;lt;/entity&amp;gt;
            &amp;lt;entity id="e3382" key-id="382" link-page-name="Edit_SubItem"&amp;gt;
                &amp;lt;description&amp;gt;Advan&amp;lt;/description&amp;gt;
                &amp;lt;image&amp;gt;images/plus.gif&amp;lt;/image&amp;gt;
                &amp;lt;imageNode&amp;gt;images/de.gif&amp;lt;/imageNode&amp;gt;
                &amp;lt;imageOpen&amp;gt;images/minus.gif&amp;lt;/imageOpen&amp;gt;
                &amp;lt;entity id="e43157" key-id="3157" link-page-name="Edit_ItemTemplate"&amp;gt;
                    &amp;lt;description&amp;gt;States&amp;lt;/description&amp;gt;
                    &amp;lt;image&amp;gt;images/paper.gif&amp;lt;/image&amp;gt;
                    &amp;lt;imageOpen&amp;gt;images/paper.gif&amp;lt;/imageOpen&amp;gt;
                &amp;lt;/entity&amp;gt;
            &amp;lt;/entity&amp;gt;
            &amp;lt;entity id="e247" key-id="47" link-page-name="Add_SubItem"&amp;gt;
                &amp;lt;description&amp;gt;A_test6&amp;lt;/description&amp;gt;
                &amp;lt;image&amp;gt;images/plus.gif&amp;lt;/image&amp;gt;
                &amp;lt;imageNode&amp;gt;images/de.gif&amp;lt;/imageNode&amp;gt;
                &amp;lt;imageOpen&amp;gt;images/minus.gif&amp;lt;/imageOpen&amp;gt;
                &amp;lt;entity id="e3716" key-id="716" link-page-name="Edit_SubItem"&amp;gt;
                    &amp;lt;description&amp;gt;A_Item&amp;lt;/description&amp;gt;
                    &amp;lt;image&amp;gt;images/plus.gif&amp;lt;/image&amp;gt;
                    &amp;lt;imageNode&amp;gt;images/de.gif&amp;lt;/imageNode&amp;gt;
                    &amp;lt;imageOpen&amp;gt;images/minus.gif&amp;lt;/imageOpen&amp;gt;
                    &amp;lt;entity id="e45545" key-id="5545" link-page-name="Edit_ItemTemplate"&amp;gt;
                        &amp;lt;description&amp;gt;temp1&amp;lt;/description&amp;gt;
                        &amp;lt;image&amp;gt;images/paper.gif&amp;lt;/image&amp;gt;
                        &amp;lt;imageOpen&amp;gt;images/paper.gif&amp;lt;/imageOpen&amp;gt;
                    &amp;lt;/entity&amp;gt;
                &amp;lt;/entity&amp;gt;
            &amp;lt;/entity&amp;gt;

        &amp;lt;/entity&amp;gt;
    &amp;lt;/entity&amp;gt;
    &amp;lt;entity id="e12" key-id="2" link-page-name="Add_Item"&amp;gt;
        &amp;lt;description&amp;gt;Sales&amp;lt;/description&amp;gt;
        &amp;lt;image&amp;gt;images/plus.gif&amp;lt;/image&amp;gt;
        &amp;lt;imageNode&amp;gt;images/de.gif&amp;lt;/imageNode&amp;gt;
        &amp;lt;imageOpen&amp;gt;images/minus.gif&amp;lt;/imageOpen&amp;gt;
        &amp;lt;entity id="e230" key-id="30" link-page-name="Add_SubItem"&amp;gt;
            &amp;lt;description&amp;gt;Gift Cards&amp;lt;/description&amp;gt;
            &amp;lt;image&amp;gt;images/plus.gif&amp;lt;/image&amp;gt;
            &amp;lt;imageNode&amp;gt;images/de.gif&amp;lt;/imageNode&amp;gt;
            &amp;lt;imageOpen&amp;gt;images/minus.gif&amp;lt;/imageOpen&amp;gt;
            &amp;lt;entity id="e3421" key-id="421" link-page-name="Edit_SubItem"&amp;gt;
                &amp;lt;description&amp;gt;Sample Card&amp;lt;/description&amp;gt;
                &amp;lt;image&amp;gt;images/plus.gif&amp;lt;/image&amp;gt;
                &amp;lt;imageNode&amp;gt;images/de.gif&amp;lt;/imageNode&amp;gt;
                &amp;lt;imageOpen&amp;gt;images/minus.gif&amp;lt;/imageOpen&amp;gt;
                &amp;lt;entity id="e43308" key-id="3308" link-page-name="Edit_ItemTemplate"&amp;gt;
                    &amp;lt;description&amp;gt;greeting temp&amp;lt;/description&amp;gt;
                    &amp;lt;image&amp;gt;images/paper.gif&amp;lt;/image&amp;gt;
                    &amp;lt;imageOpen&amp;gt;images/paper.gif&amp;lt;/imageOpen&amp;gt;
                &amp;lt;/entity&amp;gt;
            &amp;lt;/entity&amp;gt;
            &amp;lt;entity id="e3422" key-id="422" link-page-name="Edit_SubItem"&amp;gt;
                &amp;lt;description&amp;gt;De Card&amp;lt;/description&amp;gt;
                &amp;lt;image&amp;gt;images/plus.gif&amp;lt;/image&amp;gt;
                &amp;lt;imageNode&amp;gt;images/de.gif&amp;lt;/imageNode&amp;gt;
                &amp;lt;imageOpen&amp;gt;images/minus.gif&amp;lt;/imageOpen&amp;gt;
                &amp;lt;entity id="e43309" key-id="3309" link-page-name="Edit_ItemTemplate"&amp;gt;
                    &amp;lt;description&amp;gt;NS Temp&amp;lt;/description&amp;gt;
                    &amp;lt;image&amp;gt;images/paper.gif&amp;lt;/image&amp;gt;
                    &amp;lt;imageOpen&amp;gt;images/paper.gif&amp;lt;/imageOpen&amp;gt;
                &amp;lt;/entity&amp;gt;
            &amp;lt;/entity&amp;gt;

        &amp;lt;/entity&amp;gt;
        &amp;lt;entity id="e215" key-id="15" link-page-name="Add_SubItem"&amp;gt;
            &amp;lt;description&amp;gt;Chck&amp;lt;/description&amp;gt;
            &amp;lt;image&amp;gt;images/plus.gif&amp;lt;/image&amp;gt;
            &amp;lt;imageNode&amp;gt;images/de.gif&amp;lt;/imageNode&amp;gt;
            &amp;lt;imageOpen&amp;gt;images/minus.gif&amp;lt;/imageOpen&amp;gt;
            &amp;lt;entity id="e3671" key-id="671" link-page-name="Edit_SubItem"&amp;gt;
                &amp;lt;description&amp;gt;Add item&amp;lt;/description&amp;gt;
                &amp;lt;image&amp;gt;images/plus.gif&amp;lt;/image&amp;gt;
                &amp;lt;imageNode&amp;gt;images/de.gif&amp;lt;/imageNode&amp;gt;
                &amp;lt;imageOpen&amp;gt;images/minus.gif&amp;lt;/imageOpen&amp;gt;
                &amp;lt;entity id="e45438" key-id="5438" link-page-name="Edit_ItemTemplate"&amp;gt;
                    &amp;lt;description&amp;gt;Ahhhh&amp;lt;/description&amp;gt;
                    &amp;lt;image&amp;gt;images/paper.gif&amp;lt;/image&amp;gt;
                    &amp;lt;imageOpen&amp;gt;images/paper.gif&amp;lt;/imageOpen&amp;gt;
                &amp;lt;/entity&amp;gt;
            &amp;lt;/entity&amp;gt;
            &amp;lt;entity id="e3450" key-id="450" link-page-name="Edit_SubItem"&amp;gt;
                &amp;lt;description&amp;gt;Advtttt&amp;lt;/description&amp;gt;
                &amp;lt;image&amp;gt;images/plus.gif&amp;lt;/image&amp;gt;
                &amp;lt;imageNode&amp;gt;images/de.gif&amp;lt;/imageNode&amp;gt;
                &amp;lt;imageOpen&amp;gt;images/minus.gif&amp;lt;/imageOpen&amp;gt;
                &amp;lt;entity id="e43577" key-id="3577" link-page-name="Edit_ItemTemplate"&amp;gt;
                    &amp;lt;description&amp;gt;gggggg&amp;lt;/description&amp;gt;
                    &amp;lt;image&amp;gt;images/paper.gif&amp;lt;/image&amp;gt;
                    &amp;lt;imageOpen&amp;gt;images/paper.gif&amp;lt;/imageOpen&amp;gt;
                &amp;lt;/entity&amp;gt;
        &amp;lt;/entity&amp;gt;
        &amp;lt;/entity&amp;gt;       

    &amp;lt;/entity&amp;gt;
&amp;lt;/tree&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>react</category>
    </item>
  </channel>
</rss>
