{"metadata":{"image":[],"title":"","description":""},"api":{"url":"","auth":"required","results":{"codes":[]},"settings":"","params":[]},"next":{"description":"","pages":[]},"title":"Async Data","type":"basic","slug":"async-data-1","excerpt":"","body":"The tree allows to load children asynchronously using `getChildren` option, and `hasChildren` field on the node.\n[block:api-header]\n{\n  \"title\": \"Demo\"\n}\n[/block]\n\n[block:html]\n{\n  \"html\": \"<iframe src=\\\"https://angular-tree-component.azurewebsites.net/#/async\\\" width=\\\"100%\\\" height=\\\"350px\\\"></iframe>\\n\"\n}\n[/block]\n\n[block:html]\n{\n  \"html\": \"<a href=\\\"https://github.com/CirclonGroup/angular-tree-component/blob/master/projects/example-app/src/app/async/async.component.ts\\\" target=\\\"_blank\\\">Source code for this demo</a>\"\n}\n[/block]\n\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"'getChildren' option:\"\n}\n[/block]\nThis options receives a function that has a TreeNode parameter, and returns a value or a promise that resolves to the node's children:\n```\n(node:TreeNode) => TreeNode[] | Promise<TreeNode[]>\n```\n\nThe function will be called whenever a node is expanded, the `hasChildren` field is true, and the `children` field is empty.\nThe result will be loaded into the node's children attribute.\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Example:\"\n}\n[/block]\n\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"options = {\\n  getChildren: (node:TreeNode) => {\\n    return request('/api/children/' + node.id);\\n  }\\n}\\n\\nnodes = [\\n  {\\n    name: 'asyncRoot',\\n    hasChildren: true\\n  },\\n  {\\n    name: 'root2',\\n    children: [\\n      {\\n        name: 'leaf',\\n        hasChildren: false\\n      }\\n    ]\\n  }\\n]\\n\",\n      \"language\": \"javascript\"\n    }\n  ]\n}\n[/block]","updates":[],"order":0,"isReference":false,"hidden":false,"sync_unique":"","link_url":"","link_external":false,"_id":"5c27be9e2b75bd0045dc7466","category":{"sync":{"isSync":false,"url":""},"pages":[],"title":"Guides","slug":"documentation","order":1,"from_sync":false,"reference":false,"_id":"5c27be9e2b75bd0045dc7459","version":"5c27be9e2b75bd0045dc7476","project":"5707c6875309d117006948d4","createdAt":"2016-04-08T14:56:07.661Z","__v":0},"project":"5707c6875309d117006948d4","parentDoc":null,"user":"54d742a80771980d0052f4b5","version":{"version":"8.2.0","version_clean":"8.2.0","codename":"","is_stable":true,"is_beta":false,"is_hidden":false,"is_deprecated":false,"categories":["5c27be9e2b75bd0045dc7458","5c27be9e2b75bd0045dc7459","5c27be9e2b75bd0045dc745a"],"_id":"5c27be9e2b75bd0045dc7476","project":"5707c6875309d117006948d4","__v":0,"forked_from":"5b65b08eb1906e000319f872","createdAt":"2018-03-23T13:06:33.352Z","releaseDate":"2018-03-23T13:06:33.352Z"},"createdAt":"2017-02-15T13:25:34.307Z","githubsync":"","__v":0}
The tree allows to load children asynchronously using `getChildren` option, and `hasChildren` field on the node. [block:api-header] { "title": "Demo" } [/block] [block:html] { "html": "<iframe src=\"https://angular-tree-component.azurewebsites.net/#/async\" width=\"100%\" height=\"350px\"></iframe>\n" } [/block] [block:html] { "html": "<a href=\"https://github.com/CirclonGroup/angular-tree-component/blob/master/projects/example-app/src/app/async/async.component.ts\" target=\"_blank\">Source code for this demo</a>" } [/block] [block:api-header] { "type": "basic", "title": "'getChildren' option:" } [/block] This options receives a function that has a TreeNode parameter, and returns a value or a promise that resolves to the node's children: ``` (node:TreeNode) => TreeNode[] | Promise<TreeNode[]> ``` The function will be called whenever a node is expanded, the `hasChildren` field is true, and the `children` field is empty. The result will be loaded into the node's children attribute. [block:api-header] { "type": "basic", "title": "Example:" } [/block] [block:code] { "codes": [ { "code": "options = {\n getChildren: (node:TreeNode) => {\n return request('/api/children/' + node.id);\n }\n}\n\nnodes = [\n {\n name: 'asyncRoot',\n hasChildren: true\n },\n {\n name: 'root2',\n children: [\n {\n name: 'leaf',\n hasChildren: false\n }\n ]\n }\n]\n", "language": "javascript" } ] } [/block]