Giter Site home page Giter Site logo

zicrael / ngx-tree-dnd Goto Github PK

View Code? Open in Web Editor NEW
39.0 4.0 11.0 1.76 MB

Angular 7 support, data sortable draggable smart tree.

Home Page: https://ngx-tree-dnd.stackblitz.io

License: MIT License

JavaScript 5.53% TypeScript 77.01% CSS 5.15% HTML 12.31%
dnd tree-dnd tree-structure angular-tree tree-component angular-tree-component ngx-tree-dnd data-tree angular-data-tree data-sortable

ngx-tree-dnd's Introduction

ngx-tree-dnd npm version npm downloads

Angular 7 support tree with drag-and-drop sortable data tree. It`s fast and smart.

Dependency Status Build status codecov Known Vulnerabilities License MIT

Help the project and star it :3

Installation

New ngx-tree-dnd with draggable/sortable tree data, easy for use.

To install library, run:

$ npm install ngx-tree-dnd --save

Import NgxTreeModule module

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppComponent } from './app.component';
import { NgxTreeDndModule } from 'ngx-tree-dnd'; // here

@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule,
    NgxTreeDndModule,   // add  NgxTreeDndModule to imports
    LibraryModule 
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

Once library is imported, you can use it`s components, directives and pipes in your Angular application:

<h1>
  {{title}}
</h1>
<lib-ngx-tree-component [treeData]='youTree' [config]='config'></lib-ngx-tree-component>

Styles

You need to import default styles for tree ( you can change them by rewrite classes )

"apps": [
    {
      "root": "src",
      "outDir": "dist",
      "assets": [
        "assets",
        "favicon.ico"
      ],
      "index": "index.html",
      "main": "main.ts",
      "polyfills": "polyfills.ts",
      "test": "test.ts",
      "tsconfig": "tsconfig.app.json",
      "testTsconfig": "tsconfig.spec.json",
      "prefix": "app",
      "styles": [
        "styles.css",
        "node_modules/ngx-tree-dnd/styles-tree-dnd.css" 
      ],
      "scripts": [],
      "environmentSource": "environments/environment.ts",
      "environments": {
        "dev": "environments/environment.ts",
        "prod": "environments/environment.prod.ts"
      }
    }
  ],

Demo

Demo with editor:

Run on stackblitz.com

Demo application:

Run on stackblitz.com

NGX-TREE-DND DOCS

With the development of the plug-in docs will be replenished. So do not forget update the packadge.

1.Set the base tree

Use [treeData] on ngx-tree-component BUT be careful!

The interface accepts only the following structure:

 export interface TreeModel {
    name: string; // name of item
    id: number; // id of item
    options?: TreeItemOptions; // options of item
    childrens: TreeModel[]; // childrens list
}
export interface TreeItemOptions {
    // item options
    href?: string;
    hidden?: boolean;
    hideChildrens?: boolean;
    draggable?: boolean;
    position?: number;
    edit?: boolean;
    disabled?: boolean;
    // item buttons
    showDropChildZone?: boolean;
    showActionButtons?: boolean;
    showDeleteButton?: boolean;
    showExpandButton?: boolean;
}

example

in you template file:

<lib-ngx-tree-component [treeData]='youTree' ></lib-ngx-tree-component>

in you component file:

  youTree = [
    {
      name: 'first elem',
      id: 1234567890,
      childrens: [
        {
          name: 'first child elem',
          id: 0987654321,
          childrens: []
        }
      ]
    },
  ];

2.Events

You can trigger events by following code:

ondragstart()

Trigger start of dragging element

example

in you template file:

<lib-ngx-tree-component [treeData]='youTree' (ondragstart)='onDragStart($event)'> </lib-ngx-tree-component>

in you component file:

    onDragStart(event) {
    console.log(event);
  }

ondragenter()

Trigger if draggable element enter to drop zone.

example

in you template file:

<lib-ngx-tree-component [treeData]='youTree' (ondragenter)='onDragEnter($event)'> </lib-ngx-tree-component>

in you component file:

    onDragEnter(event) {
    console.log(event);
  }

ondragleave()

Trigger if draggable element enter to drop zone.

example

in you template file:

<lib-ngx-tree-component [treeData]='youTree' (ondragleave)='onDragLeave($event)'> </lib-ngx-tree-component>

in you component file:

    onDragLeave(event) {
    console.log(event);
  }

ondragend()

Trigger end of drag event

example

in you template file:

<lib-ngx-tree-component [treeData]='youTree' (ondragend)='onDragEnd($event)'> </lib-ngx-tree-component>

in you component file:

    onDragEnd(event) {
    console.log(event);
  }

onallowdrop()

Trigger enter on drop zone

example

in you template file:

<lib-ngx-tree-component [treeData]='youTree' (onallowdrop)='onAllowDrop($event)'> </lib-ngx-tree-component>

in you component file:

    onAllowDrop(event) {
    console.log(event);
  }

ondrop()

Trigger drop item action

example

in you template file:

<lib-ngx-tree-component [treeData]='youTree' (ondrop)='onDrop($event)'> </lib-ngx-tree-component>

in you component file:

    onDrop(event) {
    console.log(event);
  }

onadditem()

Trigger add new item action

example

in you template file:

<lib-ngx-tree-component [treeData]='youTree' (onadditem)='onAddItem($event)'> </lib-ngx-tree-component>

in you component file:

    onAddItem(event) {
    console.log(event);
  }

onStartRenameItem()

Trigger start renaming item

example

in you template file:

<lib-ngx-tree-component [treeData]='youTree' (onStartRenameItem)='onStartRenameItem($event)'> </lib-ngx-tree-component>

in you component file:

  onStartRenameItem(event) {
    console.log(event);
  }

onFinishRenameItem()

Trigger finish renaming item after validation

example

in you template file:

<lib-ngx-tree-component [treeData]='youTree' (onFinishRenameItem)='onFinishRenameItem($event)'> </lib-ngx-tree-component>

in you component file:

  onFinishRenameItem(event) {
    console.log(event);
  }

onStartDeleteItem()

Trigger start Deleting action item

example

in you template file:

<lib-ngx-tree-component [treeData]='youTree' (onStartDeleteItem)='onStartDeleteItem($event)'> </lib-ngx-tree-component>

in you component file:

    onStartDeleteItem(event) {
    console.log(event);
  }

onFinishDeleteItem()

Trigger end Deleting action item

example

in you template file:

<lib-ngx-tree-component [treeData]='youTree' (onFinishDeleteItem)='onFinishDeleteItem($event)'> </lib-ngx-tree-component>

in you component file:

    onFinishDeleteItem(event) {
    console.log(event);
  }

onCancelDeleteItem()

Trigger cancel Deleting action item

example

in you template file:

<lib-ngx-tree-component [treeData]='youTree' (onCancelDeleteItem)='onCancelDeleteItem($event)'> </lib-ngx-tree-component>

in you component file:

    onCancelDeleteItem(event) {
    console.log(event);
  }

3. Config

You can enable/disable and set some elements on tree by write simple config:

example

in you template file:

<lib-ngx-tree-component [treeData]='youTree' [config]='config'> </lib-ngx-tree-component>

in you component file:

    config =  {
      showActionButtons: true, // show/hide action buttons for all elements
      showAddButtons: true, // show/hide add button for all elements
      showRenameButtons: true, // show/hide rename buttons for all elements
      showDeleteButtons: true, // show/hide delete buttons for all elements
      showRootActionButtons: true, // shlow/hide root action bottons
      enableExpandButtons: true, // // show/hide expand buttons for all elements
      enableDragging: true, // enable/disable dragging
      rootTitle: 'Root', // Tree titile name
      validationText: 'Enter valid name', // form validation text 
      minCharacterLength: 1, // minimum valid chars
      setItemsAsLinks: false, // set tree as <a> link-items, use 'href' option for set link.
      setFontSize: 16, // font-size of items in tree.
      setIconSize: 14 // font-size of font-awesome icons inside buttons.
    }

4. Items options

You can change items options by set 'options' key on item object.

example

in you component file:

    youTree = [
      {
        name: 'google.com',
        id: 777,
        options: {
            // item options
            href: 'google.com', // item href for <a> , use if you set 'setItemsAsLinks: true' in config.
            hidden: 'false', // hide element without removing from data array.
            hideChildrens: false, // hide childrens of element.
            draggable: true, // allow block dragging single element if set 'enableDragging: true'
            position: 1,  // set position of item for sort tree.
            disabled: false, // disaled buttons and add opacity to element.
            // buttons
            showActionButtons: true, // show/hide  action buttons for element.
            showDeleteButton: true, // if 'false' element cannot be deleted.
            showExpandButton: true // show/hide expand buttons for element.
        }
        childrens: []
      }
    ];

5. Styles

You can rewrite all styles like you want. Here some for example:

  /* ===Tree items=== */
  .tree-child {
    /* change styles for child elements */
  }
  .tree-title {
    /* change styles for items title */
  }
  .tree-content {
    /* change styles for tree.childrens wrapper */
  }
  .tree-link {
    /* change styles for links if  config.setTreeItemAsLinks = true  */
  }
  /* ===Form=== */
  .input-rename {
    /* change styles for input name/rename  */
  }
  /* ===Buttons=== */
  .tree-btn {
    /*  used in all tree buttons  */
  }
  .add-btn {
      /*  styles for add button  */
  }
  .submit-btn {
      /*  styles for submit button  */
  }
  .edit-btn {
      /*  styles for edit button  */
  }
  .delete-btn {
      /*  styles for delete button  */
  }
  .show-btn {
      /*  styles for show expand button  */
  }
  .hide-btn {
      /*  styles for hide expand button  */
  }

Special thanks to the people who help to improve and maintain this repository:

njofce

Thank you for use my plug-in! Subscribe for more plugins! :)

Made with love by Zicrael(Yaroslav Kikot) ^^

License

MIT © Yaroslav Kikot

ngx-tree-dnd's People

Contributors

manish-jain-alept avatar njofce avatar snyk-bot avatar zicrael avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

ngx-tree-dnd's Issues

Is there a way to disable drag on my items?

Looks like a very nice tree, but I didn't see a way to mark some items (ie my top level folders) so that they can be locked and not dragged. I realize I could disallow the drop, but this would be a better user experience.

How can I call an event when edit button is clicked?

I see you have provided (onrenameitem) event, which triggers once the user has updated the node in the tree and clicked on the check mark.

But, can we have an event when the edit button is clicked, because I would like to open a new page when we click on edit button.
Thanks.

onDeleteItem(event) does not work

Hi Yaroslav, My problem is ;
When I clicked onDeleteItem(event) button it give me wrong items id for example ;

--> element: {id: 90, name: "iiiii", orderNo: 0, parentCategoryId: null, options: {…}, …}
when I clicked on delete button in child items, it return id:90 when ı clicked id:93 it return id:90 .
it gives me same result with whole clicks
So how can ı fix this ?

thanks,
Ayyildiz

Fix base config

''ERROR TypeError: Cannot read property 'enableDragging' of null".

bug with "onFinishDeleteItem" event

Describe the bug
when we click on delete icon(x) and call "onFinishDeleteItem" event the node not removing from myTree variable

To Reproduce
Steps to reproduce the behavior:

  1. Go to 'application'
  2. Click on 'delete icon and "onFinishDeleteItem" event'
  3. See error like myTree variable not updating with deleted node

Expected behavior
i want myTree variable with updated data when click on delete icon (x)

Desktop (please complete the following information):

  • OS: [e.g. Windows]
  • Browser [chrome]

How to drag and drop an element between two trees ?

Is your feature request related to a problem? Please describe.
A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]

Describe the solution you'd like
A clear and concise description of what you want to happen.

Describe alternatives you've considered
A clear and concise description of any alternative solutions or features you've considered.

Additional context
Add any other context or screenshots about the feature request here.

Unable load SVG in production build while using ngx tree dnd

I have follow same step to resolve this issue as mentioned in this closed issue #46 but i am still not able to resolve this.

I am using <lib-ngx-tree-component [treeData]='youTree' (ondragend)='onDragEnd($event)'> in my component and i have also import the fortawesome in my module and component but still i don't know where we use this import fortawesome(import { faEdit,faMinus,faPlus,faTimes } from '@fortawesome/free-solid-svg-icons';) because i am using <lib-ngx-tree-component [treeData]='youTree' (ondragend)='onDragEnd($event)'> instead of <fa-icon [icon]="faCoffee">.

So, Please provide more clarity to resolve this issue.

checkTreeLength bug

For empty treeStorage there is TypeError which breaks tree render.

TypeError: Cannot read property 'options' of undefined at NgxTreeService.push../node_modules/ngx-tree-dnd/fesm5/ngx-tree-dnd.js.NgxTreeService.checkTreeLength (ngx-tree-dnd.js:840) at Observable._subscribe (ngx-tree-dnd.js:75) at Observable.push../node_modules/rxjs/_esm5/internal/Observable.js.Observable._trySubscribe (Observable.js:42) at Observable.push../node_modules/rxjs/_esm5/internal/Observable.js.Observable.subscribe (Observable.js:28) at NgxTreeParentComponent.push../node_modules/ngx-tree-dnd/fesm5/ngx-tree-dnd.js.NgxTreeParentComponent.getTreeData (ngx-tree-dnd.js:1057) at NgxTreeParentComponent.set [as treeData] (ngx-tree-dnd.js:941) at updateProp (core.js:22207) at checkAndUpdateDirectiveInline (core.js:21962) at checkAndUpdateNodeInline (core.js:23265) at checkAndUpdateNode (core.js:23227)

NgxTreeService.checkTreeLength method has wrong IF statement. For empty treeStorage (0 is smaller then 2) code tries to read first array item which is non existing by index of zero.

if (this.treeStorage.length < 2) { this.treeStorage[0].options.showDeleteButton = false; } else { try { for (var _b = __values(this.treeStorage), _c = _b.next(); !_c.done; _c = _b.next()) { var el = _c.value; if (el && el.options) { el.options.showDeleteButton = true; } } } catch (e_7_1) { e_7 = { error: e_7_1 }; } finally { try { if (_c && !_c.done && (_a = _b.return)) _a.call(_b); } finally { if (e_7) throw e_7.error; } } }

Possible fix could be?

if (this.treeStorage.length == 0) { // do nothing } else { // no need to remove delete button // if (this.treeStorage.length == 1) { // this.treeStorage[0].options.showDeleteButton = false; // } ...

Package version
"ngx-tree-dnd": "^2.5.0"

How can I call an event when delete button is clicked?

Hi @Zicrael , This one is similar to an earlier issue I raised #16 . This time I would like you to provide two separate events when delete button is clicked.

Basically, I would like to ask for confirmation before a node is removed from the tree, so a (onStartDeleteItem) and (onFinishDeleteItem) events would be great!.

Thanks,

How to set custom button only for child?

How to set right side menu popup like (...) and when on click (...) this menu open dialog for rename/delete option only for child. basically i want drag and drop menu like https://todoist.com

we want also custom button and its custom function can we get it.?

Can't Find Component

Describe the bug
A clear and concise description of what the bug is.
When I use the directive, it can't find the component

To Reproduce
Steps to reproduce the behavior:

  1. Go to '...'
  2. Click on '....'
  3. Scroll down to '....'
  4. See error

Expected behavior
A clear and concise description of what you expected to happen.

Screenshots
If applicable, add screenshots to help explain your problem.

Desktop (please complete the following information):

  • OS: [e.g. iOS]
  • Browser [e.g. chrome, safari]
  • Version [e.g. 22]

Smartphone (please complete the following information):

  • Device: [e.g. iPhone6]
  • OS: [e.g. iOS8.1]
  • Browser [e.g. stock browser, safari]
    Chrome
  • Version [e.g. 22]

Additional context
Add any other context about the problem here.
core.js:1673 ERROR Error: Uncaught (in promise): Error: Template parse errors:
Can't bind to 'treeData' since it isn't a known property of 'ngx-tree-component'.

  1. If 'ngx-tree-component' is an Angular component and it has 'treeData' input, then verify that it is part of this module.
  2. If 'ngx-tree-component' is a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' to the '@NgModule.schemas' of this component to suppress this message.
  3. To allow any property add 'NO_ERRORS_SCHEMA' to the '@NgModule.schemas' of this component. ("

<ngx-tree-component [ERROR ->][treeData]='youTree'>

<ion-content (onload)="testFunc()">
"): ng:///HomePageModule/HomePage.html@12:20
'ngx-tree-component' is not a known element:

  1. If 'ngx-tree-component' is an Angular component, then verify that it is part of this module.
  2. If 'ngx-tree-component' is a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' to the '@NgModule.schemas' of this component to suppress this message. ("

[ERROR ->]<ngx-tree-component [treeData]='youTree'>

<ion-content (onload)="testFunc()">
"): ng:///HomePageModule/HomePage.html@12:0
Error: Template parse errors:
Can't bind to 'treeData' since it isn't a known property of 'ngx-tree-component'.

  1. If 'ngx-tree-component' is an Angular component and it has 'treeData' input, then verify that it is part of this module.
  2. If 'ngx-tree-component' is a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' to the '@NgModule.schemas' of this component to suppress this message.
  3. To allow any property add 'NO_ERRORS_SCHEMA' to the '@NgModule.schemas' of this component. ("

<ngx-tree-component [ERROR ->][treeData]='youTree'>

<ion-content (onload)="testFunc()">
"): ng:///HomePageModule/HomePage.html@12:20
'ngx-tree-component' is not a known element:

  1. If 'ngx-tree-component' is an Angular component, then verify that it is part of this module.
  2. If 'ngx-tree-component' is a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' to the '@NgModule.schemas' of this component to suppress this message. ("

[ERROR ->]<ngx-tree-component [treeData]='youTree'>

<ion-content (onload)="testFunc()">
"): ng:///HomePageModule/HomePage.html@12:0
at syntaxError (compiler.js:1016)
at TemplateParser.push../node_modules/@angular/compiler/fesm5/compiler.js.TemplateParser.parse (compiler.js:14813)
at JitCompiler.push../node_modules/@angular/compiler/fesm5/compiler.js.JitCompiler._parseTemplate (compiler.js:24000)
at JitCompiler.push../node_modules/@angular/compiler/fesm5/compiler.js.JitCompiler._compileTemplate (compiler.js:23987)
at compiler.js:23930
at Set.forEach ()
at JitCompiler.push../node_modules/@angular/compiler/fesm5/compiler.js.JitCompiler._compileComponents (compiler.js:23930)
at compiler.js:23840
at Object.then (compiler.js:1007)
at JitCompiler.push../node_modules/@angular/compiler/fesm5/compiler.js.JitCompiler._compileModuleAndComponents (compiler.js:23839)
at syntaxError (compiler.js:1016)
at TemplateParser.push../node_modules/@angular/compiler/fesm5/compiler.js.TemplateParser.parse (compiler.js:14813)
at JitCompiler.push../node_modules/@angular/compiler/fesm5/compiler.js.JitCompiler._parseTemplate (compiler.js:24000)
at JitCompiler.push../node_modules/@angular/compiler/fesm5/compiler.js.JitCompiler._compileTemplate (compiler.js:23987)
at compiler.js:23930
at Set.forEach ()
at JitCompiler.push../node_modules/@angular/compiler/fesm5/compiler.js.JitCompiler._compileComponents (compiler.js:23930)
at compiler.js:23840
at Object.then (compiler.js:1007)
at JitCompiler.push../node_modules/@angular/compiler/fesm5/compiler.js.JitCompiler._compileModuleAndComponents (compiler.js:23839)
at resolvePromise (zone.js:814)
at resolvePromise (zone.js:771)
at zone.js:873
at ZoneDelegate.push../node_modules/zone.js/dist/zone.js.ZoneDelegate.invokeTask (zone.js:421)
at Object.onInvokeTask (core.js:3815)
at ZoneDelegate.push../node_modules/zone.js/dist/zone.js.ZoneDelegate.invokeTask (zone.js:420)
at Zone.push../node_modules/zone.js/dist/zone.js.Zone.runTask (zone.js:188)
at drainMicroTaskQueue (zone.js:595)

Is there any way to get node level in the tree ?

Hi @Zicrael, I am using this plugin in one of my app and I want to add a dropdown instead of input at a particular level. Is there any option, that I can get the selected node level in the event while adding or renaming node. So, that I can do my stuff based on node level.

How to pass dynamic data to the tree using services ?

Is your feature request related to a problem? Please describe.
A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]

Describe the solution you'd like
A clear and concise description of what you want to happen.

Describe alternatives you've considered
A clear and concise description of any alternative solutions or features you've considered.

Additional context
Add any other context or screenshots about the feature request here.

Unable load SVG in production build

Describe the bug
SVG icon unable to load in production mode in angular

To Reproduce
Steps to reproduce the behavior:

  1. Make demo from https://stackblitz.com/edit/ngx-tree-dnd
  2. ng build --prod to make build production
  3. run application on browser(any)
  4. there are not able to see plus,minus,delete button in tree view

Expected behavior
when i see in production build there are missing icon of font-awesome which are useful to manage child and parent of tree.
in console showing to many errors for each icons like :
FontAwesome: Could not find icon with iconName=plus and prefix=fas

Screenshots
Screenshot from 2019-04-12 11-32-34
Screenshot from 2019-04-12 11-33-32

onAddItem doesn't work

Describe the bug
when onAddItem event triggers returns wrong parentList

To Reproduce
Steps to reproduce the behavior:

  1. Go to https://stackblitz.com/edit/ngx-tree-dnd
  2. make a console.log(event) in line 71.
  3. Click on add new item button in front of Iphone item.
  4. See console that shows the google play as parent not the apple item.

How to add more fields to TreeModel interface

Hı,

How can I possibly add more fields to TreeModel interface:-

export interface TreeModel {
name: string;
id: number;
code: string;
translateId: string;

options?: TreeItemOptions;
childrens: TreeModel[]; // childrens list
}

and also should show in the view, or maybe add beforeAddItemEvent that can be bind to, by so doing the developer can raise maybe a dialog modal form for data entry for the selected ITEM-ID.

please help.

Thanks in advance.

root

Is your feature request related to a problem? Please describe.
A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]

Describe the solution you'd like
A clear and concise description of what you want to happen.

Describe alternatives you've considered
A clear and concise description of any alternative solutions or features you've considered.

Additional context
Add any other context or screenshots about the feature request here.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.