Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

When setting Data from API call then clicking on node name it's resetting all the tree node as select as false #9

Open
ShaileshPrajapati-BTC opened this issue Sep 19, 2018 · 0 comments

Comments

@ShaileshPrajapati-BTC
Copy link

class CategoryTree extends Component {
  constructor(props) {
      super(props);

      this.state = {
          data: {
              core: {

                  data: [{
                          "id": "98",
                          "parent": "#",
                          "text": "[Sample] Product Category",
                          "state": {
                              "opened": true,
                              "selected": true
                          }
                      },
                      {
                          "id": "99",
                          "parent": "98",
                          "text": "[Sample] Sub Category",
                          "state": {
                              "opened": true,
                              "selected": true
                          }
                      },
                      {
                          "id": "100",
                          "parent": "#",
                          "text": "[Sample] Product Category 2",
                          "state": {
                              "opened": true,
                              "selected": true
                          }
                      },
                      {
                          "id": "101",
                          "parent": "100",
                          "text": "[Sample] Sub Category 2",
                          "state": {
                              "opened": true,
                              "selected": true
                          }
                      },
                      {
                          "id": "102",
                          "parent": "100",
                          "text": "[Sample] Sub Category 3",
                          "state": {
                              "opened": true,
                              "selected": true
                          }
                      },
                      {
                          "id": "103",
                          "parent": "100",
                          "text": "[Sample] Sub Category 4",
                          "state": {
                              "opened": true,
                              "selected": true
                          }
                      },
                      {
                          "id": "104",
                          "parent": "100",
                          "text": "[Sample] Sub Category 5",
                          "state": {
                              "opened": true,
                              "selected": true
                          }
                      }
                  ]
              },
              plugins: ['checkbox'],
              checkbox: {
                  three_state: false // to avoid that fact that checking a node also check others
              }
          },
          selected: [],
      };
  }

  handleClick() {
    const newData = this.state.data.core.data[0].children.slice();
    newData.push({ text: 'New child node' });
    this.setState({
      data: {
        core: {
          data: [
            {
              text: 'Root node', children: newData
            }
          ]
        }
      }
    });
  }

  handleChange(e, data) {
    this.setState({
      selected: data.selected,
    })
  }

  render() {
    const { categories } = this.props;
     const data = {
      core: {
        data: categories
      },
      plugins: ['checkbox'],
      checkbox: {
        three_state: !true// to avoid that fact that checking a node also check others
      }
    }
    return (
      <div>
        <button onClick={() => this.handleClick()}>Add node</button>
        <br /><br />
        {(categories.length === 0)? null: 
        <TreeView treeData={data} onChange={(e, data) => this.handleChange(e, data)} />} 
        <br />
        <p>Selected nodes: {this.state.selected.join(', ')}</p>
      </div>
    );
  }
}

export default CategoryTree

When setting the data in local state it's working fine

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant