NESTED SETS TREE
It's a small module for getting elements of Nested Sets Tree. It works both in browsers and NodeJS. Includes types definition for Typescript.
Installation
npm install nested-sets-tree
Initialization (create NestedSets instance)
When initialized, you must define the keys used in your nested sets tree array:
Javascript:
const NestedSets = ; const tree = id: 'id' lvl: 'depth' parentId: 'parent_id' lft: 'lft' rgt: 'rgt' hide: 'hide';
In typescript + es6 import + nodejs you should use "esModuleInterop": true
option in your tsconfig.json.
Typescript:
; ; ;
Load tree
Javascript:
let array = id: 1 lvl: 0 parent_id: 0 //... //...; tree;
Typescript:
; tree.loadTreearray, ;
You can set the options' object together with your tree array:
validate true/false - if true your tree is validated. By default is false.
createIndexes true/false - if true the indexes for quick binary search are created (recommended for huge amount of operations). By default false.
indexes {} - object includes ready-made indexes. Set it if you already have got sorted tree.
tree;
Search
treeids; treeresults; treeresutls; treeresults; treeresults;
Hidden elements
If you want to exclude some element's in search results, you should use hide: true
flag in element:
const NestedSets = ; const tree = id: 'id' lvl: 'lvl' parentId: 'parent_id' lft: 'lft' rgt: 'rgt' hide: 'hide'; const treeArray = id: 1 lvl: 0 parent_id: 0 lft: 1 rgt: 20 name: 'parent element' hide: false id: 2 lvl: 1 parent_id: 1 lft: 2 rgt: 3 hide: false name: 'first child' //exclude second element id: 3 lvl: 1 parent_id: 1 lft: 4 rgt: 5 hide: true name: 'second' //...; tree;//get all childs exclude second elementlet childsWithoutHidden = treeresults; //get all childs, ignore hide flag let childs = treeresults;
in search method:
let childs = tree
Search methods
All methods are effective for taking both element's ID and element itself. All methods returns the NestedSets instance.
- get childs of root element:
getRootCats: NestedSets;
- get root element
getRootEl: NestedSets;
- check child element:
static isChildparent: CollectionEl, child: CollectionEl, : Keys: boolean;
- get childs (with el):
getChildsel: stringOrNumberType | CollectionEl, hide: boolean: NestedSets;
- is valid id:
isValidIdid: string | number: boolean;
- get all childs:
getAllChildsel: stringOrNumberType | CollectionEl, hide: boolean: NestedSets;
- get depth of tree:
getDepth: number;
- get parent of element:
getParentel: stringOrNumberType | CollectionEl: NestedSets;
- get all parents chain:
getAllParentsel: stringOrNumberType | CollectionEl, hide: boolean: NestedSets;