Giter VIP home page Giter VIP logo

tree-vue's Introduction

Tree-Vue

A light-weight library for management of hierachical content. Most solutions I found did not offer the depth of flexibility I needed with the tree. I decided to solve my problem and also give back to the Vue community. Feel free to log issues, I will jump on them at the slightest opportunity. ๐Ÿ˜Š

How to install.

npm: npm i v-tree-vue

Vue 3 Project: https://github.com/geekhybrid/vue3-tree-vue

Vue 3 npm package: npm i vue3-tree-vue

Features

  1. โœ”๏ธ Hierachical rendering of content.
  2. โœ”๏ธ Subscribing to items checked event (based on type)
  3. โœ”๏ธ Moving Items between folders (drag-and-drop)
  4. โœ”๏ธ Customising Item Rendering based on item type
  5. โœ”๏ธ Rendering selectable items like checkboxes or plain content
  6. โœ”๏ธ Double clicking to rename item

Features in Development

  1. Programmatically toggle item visibility based on the type property.
  2. Sorting items alphametically or grouping based on types
  3. Disabling and Enabling Item(s)
  4. Programmatically determining what item can be dragged into another item.
  5. -Custom Context Menu depending on item type.

image

Basic Component Rendering

<template>
    <tree-view :treeViewItems="treeViewNodes" />
</template>
<script lang='ts'>
import { Vue, Component} from 'vue-property-decorator';

import { TreeViewItem, ItemTypes } from '@/businessLogic/contracts/types';

@Component
export default class App extends Vue {
  treeViewNodes: TreeViewItem[] = []
}

Item Schema

export interface TreeViewItem {
    children?: TreeViewItem[]
    type: string
    checkedStatus?: CheckedState,
    name: string,
    id: string,
    parentId?: string
}

How to use Advance

Custom Icon Based on Item Type.

You can customise item based on their type property.

<template>
  <tree-view :treeViewItems="treeViewNodes">
      <template v-slot:icon="treeViewItem">
          <img src="@/assets/folder.svg" alt="folder" v-if="treeViewItem.type === 'folder'" >
          <img src="@/assets/word.png" alt="vue-logo" v-else-if="treeViewItem.type === '.doc'" height="22" width="22">
          <img src="@/assets/excel.png" alt="vue-logo" v-else-if="treeViewItem.type === '.excel'" height="22" width="22">
      </template>
  </tree-view>
</template>

Advanced Customisation

The library provides a way to further customise the tree view by listening to the v-on:created event from the tree-view component. The payload surplied provides robust set of options for configuring the tree.

Schema of CreatedEvent Payload

    export interface TreeViewCreatedEventPayload {
        itemCustomisations: ItemTypeCustomisations;
        eventManager: EventManager
        ...
    }

Schema for ItemTypeCustomisations

export interface ItemTypeCustomisations {
    isDropValid(droppedNode: TreeViewItem, dropHost: TreeViewItem): boolean;
    makeItemsCheckable(types: string[]): void;
    registerItemRenamedHandler(type: string, callback: (renamedItem: TreeViewItem) => Promise<TreeViewItem>): void;
    registerDragAndDropValidator(canItemMoveCallBack: (movingItem: TreeViewItem, destinationItem: TreeViewItem) => boolean): void;
    disableDragAndDrop(): void;
    
    getCustomisation(type: string): Customisations;
    getRenameHandler(type: string): (item: TreeViewItem) => Promise<TreeViewItem>;

    registerItemDeletedHandler(type: string, callback: (item: TreeViewItem) => Promise<boolean>): void;
    registerItemMovedHandler(callBack: (movedItem: TreeViewItem) => Promise<TreeViewItem>): void;
    
    registerAnyItemDeleted(callback: (item: TreeViewItem) => Promise<boolean>): void;
    registerAnyItemRenamed(callback: (item: TreeViewItem) => Promise<TreeViewItem>): void;
    registerAnyItemDragAndDrop(): void;
}

Example

<template>
  <tree-view :treeViewItems="treeViewNodes" @created="customiseTreeView" />
</template>
<script lang='ts'>
import { Vue, Component} from 'vue-property-decorator';

import { TreeViewCreatedEventPayload, TreeViewItem } from '@/businessLogic/contracts/types';

@Component
export default class App extends Vue {
  
  // This method is called when the tree view is created (on Created hook). And allows you to customise the tree-view items using the payload passed into the function.

  customiseTreeView(treeCreatedEvent: TreeViewCreatedEventPayload) {
    const customisations = treeCreatedEvent.itemCustomisations;
    
    // `Tree-vue` supports 2-major types of tree items. Checkable items or plain items.
    customisations.makeItemsCheckable([".doc", ".excel", "media" ]);
  }

  treeViewNodes: TreeViewItem[] = [] // Populate tree items here.
  ];
}
</script>

Output

image

Listening to Items Checked

To carter for advanced cases where children of the hierachical tree may be of different types. And you want to perform some further actions whenever something happens to them. You can subscribe for checked events of item types you may be interested in. And perform further actions.

Use case

E.g A school has departments, and you want to check some departments and delete them.

Solution

You can attach callbacks that notify you when departments have been checked on the tree.

How to Use

<template>    
    <!-- Examples of how to subscribe for events -->
    <tree-view :treeViewItems="schools" @created="customiseSchools" />
</template>
<script lang='ts'>
import { Vue, Component} from 'vue-property-decorator';

import { TreeViewCreatedEventPayload } from '@/businessLogic/contracts/types';

@Component
export default class App extends Vue {
  customiseSchools(treeCreatedEvent: TreeViewCreatedEventPayload) {
    const customisations = treeCreatedEvent.itemCustomisations;
    const eventManager = treeCreatedEvent.eventManager;

    eventManager.subscribeToItemChecked("department", (items) => console.log(items));
    customisations.makeItemsCheckable(["department"]);
  }
  schools: TreeViewItem[] = [
    {
      id: '1',
      type: 'school',
      name: 'Vue School',
      children: [
        {
          id: '2',
          type: 'department',
          name: 'Typescript Department',
          parentId: '1'
        },
        {
          id: '3',
          type: 'department',
          name: 'Open Source Department',
          parentId: '1'
        }
      ]
    }
  ]
}

Properties

Property Default Description
treeViewItems Empty array An array of TreeViewItem.
hideGuideLines false Determines the visibility of the guidelines
selectionMode Multiple Single or Multiple. This determines how many items can be simultaneously selected/checked in the tree.

tree-vue's People

Contributors

geekhybrid avatar ikeohachidi 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

Watchers

 avatar

tree-vue's Issues

CDN Version

Do you have a CDN version? I'll run it through browserify so I can use it outside of NodeJS in plain JS.

Also a live demo would be great.

You have one of the best Vue treeview components that I've seen!

I did npm install but I'm getting typescript errors even though the paths are specified in tsconfig.json. I get the same errors in the command-prompt and vs code.

You have src in the path, what gives???

    "paths": {
      "@/*": [
        "src/*"
      ]
    },

image

Okay I did

npm run build

The output dist/index.html had absolute paths that I had to switch to relative paths.

/css/app.b6216f1d.css

It needs a webpack config to make the paths relative.

I hacked it with a script on the index page.

<script>
  let imgs = document.querySelectorAll('img');
  for (index in imgs) {
    let img = imgs[index];
    if (img.src) {
      let rmIndex = img.src.indexOf('/img');
      img.src = img.src.substring(rmIndex + 1);
    }
  }
</script>

The demo works, but I can't use it unless I can browserify the tree-view component.

importing the package

Sorry for another question, but is this only available when using Typescript? I installed the package, it's in my node_modules folder. My IDE finds the folder but says "module not installed".. Any idea why this occures?

Screenshot 2021-07-19 at 10 43 10

How to expand all nodes without knowing the types

Is there an easy way to expand all nodes in the tree without knowing the types? My problem is the following. I read the tree structure from the server, but the types are not know in the WEB application. I don't want to fix them, because new types can appear later and this will lead to update of the WEB application. I tried the following workaround, but I don't like it, therefore I'm searching a better solution. In the lifetime hook "created" I read the tree structure (needs to be blocking), then I search all the types that are received, I create dynamically the array that is given to the property "expandedTypes" and set the property "items". This works, but I want to avoid this blocking read. I tried to make it asynchronous (with fetch), but then I had different problems. It seems that I set "items" before "expandedTypes", the expansion is not applied. Also if I do the correct sequence, but on "mounted", the also the expansion is not made. What is the right way to do this?

NPM install

Hi there,

I'd like to try this component out but I can't find how to install this as a NPM package in de documentation. Is this possible or what is the way to install this?

Greetings

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.