{"id":21524191,"url":"https://github.com/gxsshallot/general-tree","last_synced_at":"2025-08-22T18:16:14.256Z","repository":{"id":57142586,"uuid":"147654222","full_name":"gxsshallot/general-tree","owner":"gxsshallot","description":"A general tree support tree operation.","archived":false,"fork":false,"pushed_at":"2019-05-22T10:55:12.000Z","size":27,"stargazers_count":1,"open_issues_count":0,"forks_count":2,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-10-23T13:04:34.026Z","etag":null,"topics":["search","traverse","tree"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/gxsshallot.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-09-06T09:59:32.000Z","updated_at":"2020-02-16T07:59:33.000Z","dependencies_parsed_at":"2022-09-12T10:41:25.803Z","dependency_job_id":null,"html_url":"https://github.com/gxsshallot/general-tree","commit_stats":null,"previous_names":["gaoxiaosong/react-native-general-tree","gaoxiaosong/general-tree"],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gxsshallot%2Fgeneral-tree","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gxsshallot%2Fgeneral-tree/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gxsshallot%2Fgeneral-tree/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gxsshallot%2Fgeneral-tree/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/gxsshallot","download_url":"https://codeload.github.com/gxsshallot/general-tree/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244082586,"owners_count":20395299,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["search","traverse","tree"],"created_at":"2024-11-24T01:21:26.583Z","updated_at":"2025-03-17T17:46:11.759Z","avatar_url":"https://github.com/gxsshallot.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# general-tree\n\n[![npm version](https://img.shields.io/npm/v/general-tree.svg?style=flat)](https://www.npmjs.com/package/general-tree)\n[![Build Status](https://travis-ci.org/gaoxiaosong/general-tree.svg?branch=master)](https://travis-ci.org/gaoxiaosong/general-tree)\n\nA general tree support tree operation.\n\n## Install\n\nInstall by Yarn:\n\n```shell\nyarn add general-tree\n```\n\nInstall by NPM:\n\n```shell\nnpm install --save general-tree\n```\n\n## Usage\n\nImport the module in file:\n\n```javascript\nimport Tree from 'general-tree';\n```\n\n### Construct\n\nYou can convert a javascript object to a tree. It will use several key to identify id and children.\n\nConstructor declaration is:\n\n```javascript\nconstructor(\n    root,\n    parent = undefined,\n    childrenKey = 'children',\n    idKey = 'id',\n    onStatusChange = undefined\n)\n```\n\n* root: Object to be converted.\n* parent: A `Tree` object to be parent node.\n* childrenKey: `root[childrenKey]` will be used to generate children `Tree` node. `root[childrenKey] === undefined` means it is a leaf node.\n* idKey: `root[idKey]` is an identifier of node, and will be used when initialization. It will be converted to a string when used.\n* onStatusChange: When a tree node's selection status change, it will be called with current node.\n\nExample:\n\n```javascript\nconst node = {\n    code: 1,\n    label: 'Hello',\n    childs: [\n        {code: 2, label: 'aaa', childs: []}, // not a leaf\n        {code: 3, label: 'bbb'}, // a leaf\n    ]\n};\nconst tree = new Tree(node, null, 'childs', 'code', null);\n```\n\n### Selection Status\n\nA tree node have a selection status.\n\n```javascript\nexport const NotSelect = 0;\nexport const IncompleteSelect = 0.5;\nexport const FullSelect = 1;\n```\n\nA leaf node only have two status: `NotSelect` or `FullSelect`.\n\nA not leaf node have all three status. `IncompleteSelect` means its children is not all selected.\n\nThe selection status will be initialized in `constructor` with `NotSelect`. And you can use `setInitialState` to set a initial selection state of a tree.\n\nThen you can call `update` method to change a tree node selection status automatically.\n\n### Interface\n\n* `isLeaf(): boolean`\n* `isEqual(treeNode: Tree): boolean`\n* `selectStatus(cascade: boolean): 0 | 0.5 | 1`\n* `isFullSelect(cascade: boolean): boolean`\n* `isNotSelect(cascade: boolean): boolean`\n* `isIncompleteSelect(cascade: boolean): boolean`\n* `getLeafCount(): number`\n* `getSelectedLeafCount(): number`\n* `getDeepth(): number`\n* `getInfo(): object`\n* `getId(): any`\n* `getStringId(): string`\n* `getParent(): Tree?`\n* `getChildren(): Tree[]?`\n* `getLeafChildren(): Tree[]`\n* `getLeafChildrenCount(): number`\n* `getSelectedLeafChildrenCount(): number`\n* `getPath(): string`\n* `setInitialState(selectedIds: any[], cascade: boolean): Tree[]`\n* `update(cascade: boolean): void`\n* `search(text: string, keys: string[], multiselect: boolean, exactly: boolean, canSearch: boolean): Tree[]`\n* `hasAncestor(ancestor: Tree): boolean`\n* `findById(childId: any): Tree[]`","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgxsshallot%2Fgeneral-tree","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgxsshallot%2Fgeneral-tree","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgxsshallot%2Fgeneral-tree/lists"}