项目作者: courage007

项目描述 :
Developing a Tree Component Based On Angular2+(基于Angluar2 开发的通用树组件)
高级语言: TypeScript
项目地址: git://github.com/courage007/AngularTree.git


Contents

Introduction and Features

Using this Module you can utilize an Angular Tree Component in Angular 2+.
Feel free to contribute, raise feature requests and make it better. Here is the main Features:
1. toggle: expand or collapse
2. active: active or deactive
3. focus: focus or blur
4. keys operations:down | up | left | right | space | enter
5. node operation api: add node | remove node
6. contextmenu demo added by catching up with the ContextMenu event

Common Tree Component for Angular2 Plus

Setup

Installation

  1. npm install ng2tree-common --save

Sample

  • (1) Include TreeModule in Main Module where you want to use the tree component.(eg: app.module.ts):
    ```typescript
    import { BrowserModule } from ‘@angular/platform-browser’;
    import { NgModule } from ‘@angular/core’;
    import { TreeModule } from ‘ng2tree-common’;

import { AppComponent } from ‘./app.component’;

@NgModule({
declarations: [
AppComponent
],
imports: [
TreeModule,
BrowserModule
],
providers: [
],
bootstrap: [
AppComponent
],
entryComponents: [
]
})
export class AppModule { }

  1. - (2) Create Editor options in component.(eg: app.component.ts)
  2. ```typescript
  3. import { Component } from '@angular/core';
  4. export const DATA = [
  5. {
  6. id: 1,
  7. name: 'root1',
  8. subTitle: 'the root',
  9. type: 'type1',
  10. children: [
  11. {
  12. id: 2,
  13. name: 'child1.1',
  14. type: 'type2',
  15. subTitle: 'a good child'
  16. }
  17. ]
  18. },
  19. {
  20. id: 3,
  21. name: 'root3',
  22. subTitle: 'the third root',
  23. }
  24. ];
  25. @Component({
  26. selector: 'app-root',
  27. templateUrl: './app.component.html',
  28. styleUrls: ['./app.component.css'],
  29. styles: [
  30. `button: {
  31. line - height: 24px;
  32. box-shadow: 0 0 0 1px rgba(0, 0, 0, 0.5);
  33. border: none;
  34. border-radius: 2px;
  35. background: #A3D9F5;
  36. cursor: pointer;
  37. margin: 0 3px;
  38. }`
  39. ],
  40. })
  41. export class AppComponent {
  42. title = 'app';
  43. nodes = DATA ; // Outside static Data
  44. }
  • (3) Include editor in html with options and ngModel bindings.(eg: app.component.html)
    1. <ng2tree [nodes]="nodes"></ng2tree>

Events

Output event (onToggle、onActivate、onDeactivate、onActiveChanged, …) expose the tree instance that can be used for performing custom operations on it.

  1. <ng2tree [nodes]="nodes" (onToggle)="toggleEventHandler($event)" (onActivate)="activateEventHandler($event)"
  2. (onDeactivate)="deactivateEventHandler($event)" (onActiveChanged)="activeChangedEventHandler($event)"
  3. (onFocus)="focusEventHandler($event)" (onBlur)="blurEventHandler($event)" (onDoubleClick)="doubleClickEventHandler($event)"
  4. (onContextMenu)="contextMenuEventHandler($event)"
  5. ></ng2tree>
  1. export class AppComponent {
  2. title = 'app';
  3. nodes = DATA ; // Outside static Data
  4. // 自定义事件处理器
  5. toggleEventHandler = ($event) => console.log($event);
  6. activateEventHandler = ($event) => console.log($event);
  7. deactivateEventHandler = ($event) => console.log($event);
  8. activeChangedEventHandler = ($event) => console.log($event);
  9. focusEventHandler = ($event) => console.log($event);
  10. blurEventHandler = ($event) => console.log($event);
  11. doubleClickEventHandler($event){
  12. console.log("Double Click Handler. The event is:", $event);
  13. }
  14. contextMenuEventHandler = ($event) => console.log("Show ContextMenu:with or without custom contex menu", $event);
  15. }

Configurations

The ng2tree-common exposes api for user to customize his/her config.

  • (1) Create tree options in component.(eg: app.component.ts)
    ```typescript
    export class AppComponent {
    nodes = DATA ; // Outside static Data

    // Custom Options
    customTemplateStringOptions = {
    allowDrag: false,
    enableCustomContextMenu: false
    }
    }

  1. - (2) Using your custom options as the ng2tree-common's input.(eg: app.component.html)
  2. ```html
  3. <ng2tree [nodes]="nodes" [options]="customTemplateStringOptions" ></ng2tree>

License

MIT © John Wang