frontend-collective / react-sortable-tree

Drag-and-drop sortable component for nested data and hierarchies
https://frontend-collective.github.io/react-sortable-tree/
MIT License
4.91k stars 904 forks source link

loading the child asynchronously from server #211

Open veeramarni opened 6 years ago

veeramarni commented 6 years ago

@fritz-c thanks for the very great component. We want to know how we can load the children's child asynchronously when the expand button is clicked.

Initially, we want to send the UI just the first depth and as the user clicks on the expansion button we want to pull more data. We sending column hasChildren boolean for a placeholder for asynchronous data. So when the expansion is clicked it will pull additional node data from the backend. Let me know what I need to tweak to get there.

       treeData: [
        {
          "title": "development",
          "isDirectory": true,
          "hasChildren": true,
          "children": [
            {
              "title": "examples",
              "isDirectory": true,
              "hasChildren": true,
            },
            {
              "title": "private",
              "isDirectory": true,
              "hasChildren": true,
            },
            {
              "title": "projects",
              "isDirectory": true,
              "hasChildren": true,
            }
          ]
        }
      ],
veeramarni commented 6 years ago

@fritz-c is it something you can tell us it would work then we will have our developers work on it and probably we would create an example project to add to your readme.

fritz-c commented 6 years ago

There is an undocumented and untested bit of functionality to load children asynchronously when they are defined as a function instead of an array. Take a look in the react-sortable-tree.js source file to see something about lazy loading to figure out how it works. If it’s buggy, please submit pull requests to fix it if possible. It would be nice to have as an official feature, but it adds in a few gotchas that I didn’t want to consider early in development.

veeramarni commented 6 years ago

@fritz-c

We have created this PR for lazy loading to work. Please review and let us know.

https://github.com/fritz-c/react-sortable-tree/pull/214

veeramarni commented 6 years ago

@fritz-c Any comments or suggestions on the PR?

megaboich commented 6 years ago

I have created another PR to include example of lazy-loading nodes and also bug fix. Actually it was working quite stable, on React 15 for a while, but now it seems that React 16 is a bit harsh about unhadled exceptions, so it was not stable when you do drag and drop over not yet loaded nodes. So in PR I tried to avoid throwing an exeption in some cases and instead handling undefined result was added.