Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

ngx-treeview

leovo270827.7kMIT10.0.2TypeScript support: included

An Angular treeview component with checkbox

ng, ngx, angular, ngx-treeview, tree, treeview, tree-view, checkbox, dropdown, recursive, angular tree component

readme

ngx-treeview Build Status npm version

An Angular treeview component with checkbox

Dependencies

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.

Features

  • Unlimited tree level
  • State: disabled / collapse, expand
  • Filtering
  • Internationalization (i18n) support
  • Template
  • Checkbox with tri-state

Demo

https://leovo2708.github.io/ngx-treeview/

Installation

After install the above dependencies, install ngx-treeview via:

npm install ngx-treeview --save

Once installed you need to import our main module in your application module:

import { TreeviewModule } from 'ngx-treeview';

@NgModule({
  declarations: [AppComponent, ...],
  imports: [TreeviewModule.forRoot(), ...],
  bootstrap: [AppComponent]
})
export class AppModule {
}

Usage

Treeview:

<ngx-treeview
  [config]="config"
  [items]="items"
  (selectedChange)="onSelectedChange($event)"
  (filterChange)="onFilterChange($event)"
>
</ngx-treeview>

Treeview with dropdown:

<ngx-dropdown-treeview
  [buttonClass]="buttonClass"
  [config]="config"
  [items]="items"
  (selectedChange)="onSelectedChange($event)"
  (filterChange)="onFilterChange($event)"
>
</ngx-dropdown-treeview>

config is optional. This is the default configuration:

{
   hasAllCheckBox: true,
   hasFilter: false,
   hasCollapseExpand: false,
   decoupleChildFromParent: false,
   maxHeight: 500
}

You can change default configuration easily because TreeviewConfig is injectable.

Pipe ngxTreeview:

To map your JSON objects to TreeItem objects.

<ngx-dropdown-treeview
  [config]="config"
  [items]="items | ngxTreeview:'textField'"
  (selectedChange)="onSelectedChange($event)"
>
</ngx-dropdown-treeview>

Create a TreeviewItem:

const itCategory = new TreeviewItem({
  text: "IT",
  value: 9,
  children: [
    {
      text: "Programming",
      value: 91,
      children: [
        {
          text: "Frontend",
          value: 911,
          children: [
            { text: "Angular 1", value: 9111 },
            { text: "Angular 2", value: 9112 },
            { text: "ReactJS", value: 9113 },
          ],
        },
        {
          text: "Backend",
          value: 912,
          children: [
            { text: "C#", value: 9121 },
            { text: "Java", value: 9122 },
            { text: "Python", value: 9123, checked: false },
          ],
        },
      ],
    },
    {
      text: "Networking",
      value: 92,
      children: [
        { text: "Internet", value: 921 },
        { text: "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 = new TreeviewItem({
  text: "Vegetable",
  value: 2,
  children: [
    { text: "Salad", value: 21 },
    { text: "Potato", value: 22 },
  ],
});
vegetableCategory.children.push(
  new TreeviewItem({ text: "Mushroom", value: 23, checked: false })
);
vegetableCategory.correctChecked(); // need this to make 'Vegetable' node to change checked value from true to false

TreeviewEventParser:

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.

Templating:

See example 4 & 5.

Contributing

I am very appreciate for your ideas, proposals and found bugs which you can leave in github issues. Thanks in advance!

changelog

0.0.3 (2017-04-20)

Upgrade project for Angular 4 from my old component: ng2-dropdown-treeview

0.0.5 (2017-05-25)

Enhancement:

  • Item always expands when matching search filter.

1.0.6 (2017-07-10)

Enhancement:

  • Build bundles.
  • 100% code coverage.

1.0.7 (2017-07-19)

Enhancement:

  • Template for header
export interface TreeviewHeaderTemplateContext {
  config: TreeviewConfig;
  item: TreeviewItem;
  onCollapseExpand: () => void;
  onCheckedChange: (checked: boolean) => void;
  onFilterTextChange: (text: string) => void;
}

Refactoring:

  • Changes on interface of Treeview
export interface TreeviewItemTemplateContext {
  item: TreeviewItem;
  onCollapseExpand: () => void;
  onCheckedChange: () => void;
}
  • Changes on TreeviewConfig
export class TreeviewConfig {
  hasAllCheckBox = true;
  hasFilter = false;
  hasCollapseExpand = false;
  maxHeight = 500;
}

Demo:

  • Example for ngx-dropdown-treeview-select component.

1.0.8 (2017-07-20)

Enhancement:

  • Refactor CSS.

Demo:

  • Update examples.

1.0.9 (2017-07-21)

Enhancement:

  • Expose DropdownDirective from DropdownTreeviewComponent.

1.1.0 (2017-08-16)

Enhancement:

  • Support Bootstrap 4 beta.

1.2.0 (2017-09-18)

Enhancement:

  • Support tri-state checkbox.

1.2.4 (2017-10-19)

Enhancement:

  • Support configuration property to decouple parent and child.

1.2.5 (2017-11-01)

Enhancement:

  • Rename ngOutletContext (deprecated) to ngTemplateOutletContext.

2.0.0 (2017-12-21)

Enhancement:

  • Upgrade to Angular 5.

2.0.2 (2018-01-30)

Enhancement:

  • Upgrade to Bootstrap 4 Final.

2.0.3 (2018-02-02)

Enhancement:

  • Fix bugs.

2.0.4 (2018-02-05)

Enhancement:

  • Allow to toggle on checkbox's label.

2.0.5 (2018-03-19)

Security:

  • Update NPM packages to fix security problem from "zkat / ssri".

2.0.6 (2018-04-09)

Enhancement:

  • Add event filterChange.

2.0.7 (2018-06-07)

Enhancement:

  • Change lodash imports.

6.0.0 (2018-06-08)

Enhancement:

  • Upgrade to Angular 6.

10.0.1 (2020-07-06)

Enhancement:

  • Upgrade to Angular 10.
  • Upgrade Bootstrap to 4.5.1
  • Remove fontawesome

10.0.2 (2020-07-07)

Enhancement:

  • Fix error ExpressionChangedAfterItHasBeenCheckedError