react-orgchart icon indicating copy to clipboard operation
react-orgchart copied to clipboard

Level / Depth When First Show

Open erbaafidotama opened this issue 5 years ago • 13 comments

Hayy, thanks to created react version. I have problem, how to set level or depth my org chart? I have 10 level, but i just want to show 3 level then i can expand manual.

Sorry for my bad english. Thanks.

erbaafidotama avatar Sep 14 '20 14:09 erbaafidotama

Can you figure it out?

orcuncelik avatar Sep 28 '20 06:09 orcuncelik

Not yet. I dont know how.

erbaafidotama avatar Sep 28 '20 14:09 erbaafidotama

I guess you can achieve this with some css classes or send some of the node data and when clicked add more data to it. Though I am having difficulties with onClick method inside custom node template

MudulOzan avatar Apr 29 '21 08:04 MudulOzan

For jquery version, can using visibleLevel. How about react version?

erbaafidotama avatar May 11 '21 03:05 erbaafidotama

I had to handle it myself, I send a prop called manager level and when getting managers I added index as manager level

MudulOzan avatar May 11 '21 07:05 MudulOzan

I still cant solving this problem. Any body can help?

erbaafidotama avatar Oct 19 '21 10:10 erbaafidotama

@erbaafidotama I made a recursive tree, my object is like { user: Ozan, Manager: Jonathan 0 } then I query Jonathan and get whose manager Jonathan and set them my associates, if Jonathan has manager, get the manager and so on. When you are getting managers you can count the managerLevel you passed and stop when the level reached. If you can't apply this method, show me your code I'll give u a hand

MudulOzan avatar Oct 19 '21 17:10 MudulOzan

@MudulOzan for example, i just follow demo from this repo. https://codesandbox.io/s/bold-lumiere-tb288?file=/src/App.js

I fetching all my data. In that example, i have 4 levels. But i just want show 3 levels in first show. then when i click expand in node "Hei Hei"s childrens will showing.

erbaafidotama avatar Oct 21 '21 03:10 erbaafidotama

It sounds hard to achieve with full chart for me, let your users have manager object which allows you to build the node tree by your own. Then you can do recursive calls and add your managers or if you are going top to bottom add your underlings. Also you are going to need a custom node which is documented here. Whenever user clicks a node you need to call a method with clicked user's id and reorganize your tree according to that.

This is how I managed to do it, I call getSelectedUser first, then it calls recursive methods. If user clicks a node, it also triggers getSelectedUser. My approach may not be the best but well, it work :P


protected getManager(user: any, tempJson: any, managerLevel?: any) {
    fetch().then((manager: any) => {
        tempJson = {
            ...manager[0],
            children: [
                ...tempJson
            ]
        }
        if (user?.Manager.length > 0 && (user?.User.Id !== manager[0].Manager[0].Id)) {
            if (managerLevel !== null) {
                if (managerLevel <= 1) {
                    this.setOrgData(tempJson)
                    return;
                }
                else {
                    managerLevel--;
                    this.getManager(manager[0], [tempJson], managerLevel)
                }

            }
            else {
                this.getManager(manager[0], [tempJson], managerLevel)
            }
        }
        else {
            this.setOrgData(tempJson)
        }
    })
}

protected getSelectedUser() {
    fetch().then((user: any) => {
        this.getAssociates(user);
    })
}

protected getAssociates(user: any) {
    var tempJson: any;
    var managerLevel = this.props.managerLevel;
    fetch().then((associates: any) => {
        fetch().then((reportees: any) => {
            if (reportees.length > 0) {
                tempJson = [{
                    ...user[0],
                    children: [
                        ...reportees
                    ]
                }]
            }
            else {
                tempJson = associates;
            }
            this.getManager(user[0], [...tempJson], managerLevel)
        })
    })
}

MudulOzan avatar Oct 21 '21 12:10 MudulOzan

I too have the same problem I just need to show 2 levels of the node by default. Can anyone help me?

NandhiniShiva avatar Aug 05 '22 09:08 NandhiniShiva

I too have the same problem I just need to show 2 levels of the node by default. Can anyone help me?

Provided an example above, you can apply that, set manager level to 2, get it from the props, redux, whatever you want

MudulOzan avatar Aug 05 '22 10:08 MudulOzan

I have a problem. I need to collapse all other nodes when I expand one node. I'm a beginner, I don't know how to do this. can anyone help me?

NandhiniShiva avatar Aug 18 '22 05:08 NandhiniShiva

I have a problem. I need to collapse all other nodes when I expand one node. I'm a beginner, I don't know how to do this. can anyone help me?

If you can provide a codesandbox example I can help

MudulOzan avatar Aug 18 '22 15:08 MudulOzan