An Angular treeview component with checkbox
This component is currently supporting Bootstrap 4. If you are using Bootstrap 4 alpha 6, please downgrade to the older version 1.0.10.
You can customize CSS yourself to break down dependencies to Bootstrap.
- Unlimited tree level
- State: disabled / collapse, expand
- Internationalization (i18n) support
- Checkbox with tri-state
After install the above dependencies, install
npm install ngx-treeview --save
Once installed you need to import our main module in your application module:
Treeview with dropdown:
config is optional. This is the default configuration:
hasAllCheckBox: truehasFilter: falsehasCollapseExpand: falsedecoupleChildFromParent: falsemaxHeight: 500
You can change default configuration easily because TreeviewConfig is injectable.
To map your JSON objects to TreeItem objects.
Create a TreeviewItem:
const itCategory =text: "IT"value: 9children:text: "Programming"value: 91children:text: "Frontend"value: 911children:text: "Angular 1" value: 9111text: "Angular 2" value: 9112text: "ReactJS" value: 9113text: "Backend"value: 912children:text: "C#" value: 9121text: "Java" value: 9122text: "Python" value: 9123 checked: falsetext: "Networking"value: 92children:text: "Internet" value: 921text: "Security" value: 922;
You can pass the second paramater 'autoCorrectChecked' with value=true (default is false) in constructor of TreeviewItem to correct checked value of it and all of its descendants. In some cases, you need to push or pop children flexibly, checked of parent may be not correct. Then you need to call function correctChecked() to help to correct from root to its descendants.
const vegetableCategory =text: "Vegetable"value: 2children:text: "Salad" value: 21text: "Potato" value: 22;vegetableCategorychildren;vegetableCategory; // need this to make 'Vegetable' node to change checked value from true to false
Extract data from list of checked TreeviewItem and send it in parameter of event selectedChange. Some built-in TreeviewEventParser:
- DefaultTreeviewEventParser: return values of checked items.
- DownlineTreeviewEventParser: return list of checked items in orginal order with their ancestors.
- OrderDownlineTreeviewEventParser: return list of checked items in checked order with their ancestors. Note that: value of a leaf must be different from value of other leaves.
See example 4 & 5.
I am very appreciate for your ideas, proposals and found bugs which you can leave in github issues. Thanks in advance!