Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
612 views
in Technique[技术] by (71.8m points)

javascript - How to collapse and expand antd TreeSelect items on click of a button?

I am using antd library for developing my website. I am using TreeSelect component to show some nested options and successfully i have implemented.

But I am facing an issue below: I am trying to expand and collapse TreeSelect items on click of a button. Can anyone help me on this?

Below code i am using for button event

expandCollapseOpions = () => {
    this.setState({
      isExpandCollpase: !this.state.isExpandCollpase
    });
}; 

Below attribute I am using, which is not correct I feel.

treeDefaultExpandAll={this.state.isExpandCollpase}

Complete Code:

import React from "react";
import ReactDOM from "react-dom";
import "antd/dist/antd.css";
import { TreeSelect } from "antd";

const { TreeNode } = TreeSelect;

class Demo extends React.Component {
  state = {
    value: undefined,
    isExpandCollpase: true
  };

  onChange = value => {
    console.log(value);
    this.setState({ value });
  };

  expandCollapseOpions = () => {
    this.setState({
      isExpandCollpase: !this.state.isExpandCollpase
    });
  };

  render() {
    return (
      <div>
        <button onClick={() => this.expandCollapseOpions()}>
          Expand/Collapse Options
        </button>
        <br />
        <br />
        <TreeSelect
          showSearch
          multiple
          style={{ width: "100%" }}
          value={this.state.value}
          dropdownStyle={{ maxHeight: 400, overflow: "auto" }}
          placeholder="Please select"
          allowClear
          treeDefaultExpandAll={this.state.isExpandCollpase}
          onChange={this.onChange}
        >
          <TreeNode value="parent 1" title="parent 1" selectable={false}>
            <TreeNode value="A" title="A" selectable={false}>
              <TreeNode value="leaf1" title="my leaf" />
              <TreeNode value="leaf2" title="your leaf" />
            </TreeNode>
          </TreeNode>
          <TreeNode value="parent 2" title="parent 2" selectable={false}>
            <TreeNode value="B" title="B" selectable={false}>
              <TreeNode
                value="sss"
                title={<b style={{ color: "#08c" }}>sss</b>}
              />
            </TreeNode>
          </TreeNode>
        </TreeSelect>
      </div>
    );
  }
}

ReactDOM.render(<Demo />, document.getElementById("container"));

stackblitz link

App URL

I am trying to implement below:

On 1st click of Expand/Collapse Options button

enter image description here

On 2nd click of Expand/Collapse Options button

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I found the solution for this issue.

So need to remove the component and again need to add the component.

 expandCollapseOpions = async () => {
    await this.setState({
      isExpandCollpase: !this.state.isExpandCollpase
    });
    await this.setState({
      isExpandCollpase: !this.state.isExpandCollpase
    });
  };

DOM condition

{this.state.isExpandCollpase ? (
          <TreeSelect
            showSearch
            multiple
            style={{ width: "100%" }}
            value={this.state.value}
            dropdownStyle={{ maxHeight: 400, overflow: "auto" }}
            placeholder="Please select"
            allowClear
            onChange={this.onChange}
            labelInValue={true}
          >
            <TreeNode value="parent 1" title="parent 1" selectable={false}>
              <TreeNode value="A" title="A" selectable={false}>
                <TreeNode value="leaf1" title="my leaf" />
                <TreeNode value="leaf2" title="your leaf" />
              </TreeNode>
            </TreeNode>
            <TreeNode value="parent 2" title="parent 2" selectable={false}>
              <TreeNode value="B" title="B" selectable={false}>
                <TreeNode
                  value="sss"
                  title={<b style={{ color: "#08c" }}>sss</b>}
                />
              </TreeNode>
            </TreeNode>
          </TreeSelect>
        ) : null}

stackblitz link


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.7k users

...