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

What is the best way to render Tree using custom Tree Object JSON #53

Closed
anil1712 opened this issue Feb 14, 2017 · 2 comments
Closed

What is the best way to render Tree using custom Tree Object JSON #53

anil1712 opened this issue Feb 14, 2017 · 2 comments
Labels

Comments

@anil1712
Copy link

Hi,
I am having the Tree Data in this format. (eg. {name: ..., users: [ ... ]})

[ { 'id': '1', 'name': 'Value1', 'users': [ { 'name': 'Value2',  }, { 'name': 'Value3', 'users': [ { 'name': 'Value4',  } ] }, { 'name': 'Value4', 'users': [ { 'name': 'Value5',  } ] }, { 'name': 'Value5',  }, { 'name': 'Value6', 'users': [ { 'name': 'Value7',  } ] } ] } ]

What is the best way to convert it into required TreeData format?
(like: {title: ..., children:[ ... ]}))

Can we use nodeRenderer to achieve this?

Thanks
Anil

@anil1712 anil1712 changed the title What is the best way to render custom TreeData What is the best way to render Tree using custom Tree Object JSON Feb 14, 2017
@fritz-c
Copy link
Member

fritz-c commented Feb 15, 2017

As all of the helper functions also use children as the key for children, I would suggest you convert it temporarily. Try this function:

const convertChildrenKeys = (children, prevKey, nextKey) => {
    return children.map(child => {
        const modifiedChild = {
            name: child.name,
            // carry over whatever other props you want
        };

        // If this child has children, add them using the nextKey
        if (child[prevKey] && child[prevKey].length > 0) {
            modifiedChild[nextKey] = convertChildrenKeys(child[prevKey], prevKey, nextKey);
        }

        return modifiedChild;
    });
};

// Convert your data to a form that can be handled by react-sortable-tree
const compatible = convertChildrenKeys(myTreeData, 'users', 'children');

// ... 

// After you're done using the library,
// convert the data back to its original format (if you need to)
const original = convertChildrenKeys(compatible, 'children', 'users');

By the way, nodeRenderer is entirely for visual customization. For example, if you wanted to render rows without borders, add buttons anywhere, etc.

@anil1712
Copy link
Author

Thanks @fritz-c
convertChildrenKeys is what I was expecting.

But I am facing one more difficulty. How can we sort the children array (title) in ascending order?

@fritz-c fritz-c closed this as completed Oct 27, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants