Giter Club home page Giter Club logo

v-treeview's Introduction

v-treeview

A treeview component for Vue.js2

Support customizable context menu and tree types, configurable font awesom icon, depth control

npm npm GitHub stars GitHub forks Build Status license

How to use

Step1: install v-treeview

npm install v-treeview --save

Step2๏ผš In your vue component script import v-treeview

import VTreeview from "v-treeview"

Step3: In your export script

export default {
  data() { ...     
  },
  computed: { ...
  },
  methods: { ...
  },  
  components: {
    VTreeview
  }
};

Step4: In your index.html, add fontawesome CDN

<script defer src="https://use.fontawesome.com/releases/v5.0.6/js/all.js"></script>

Demo

https://hyounoo.github.io/v-treeview/

Html

  <v-treeview v-model="treeData" :treeTypes="treeTypes" @selected="selected" :openAll="openAll" :contextItems="contextItems" @contextSelected="contextSelected"></v-treeview>

JS

import VTreeview from "../src/index.js";

export default {
  data() {
    return {
      openAll: true,
      treeTypes: [
        {
          type: "#",
          max_children: 6,
          max_depth: 4,
          valid_children: [
            "FMM_EMPLOYEE",
            "FMM_SPOUSE",
            "FMM_CHILD",
            "FMM_SIBLING",
            "FMM_PARENT",
            "FMM_PARENT_IN_LAW"
          ]
        },
        {
          type: "FMM_EMPLOYEE",
          icon: "far fa-user",
          valid_children: ["Basic", "Top-up"]
        },
        {
          type: "FMM_SPOUSE",
          icon: "far fa-user",
          valid_children: ["Basic", "Top-up"]
        },
        {
          type: "FMM_CHILD",
          icon: "far fa-user",
          valid_children: ["Basic", "Top-up"]
        },
        {
          type: "FMM_SIBLING",
          icon: "far fa-user",
          valid_children: ["Basic", "Top-up"]
        },
        {
          type: "FMM_PARENT",
          icon: "far fa-user",
          valid_children: ["Basic", "Top-up"]
        },
        {
          type: "FMM_PARENT_IN_LAW",
          icon: "far fa-user",
          valid_children: ["Basic", "Top-up"]
        },
        {
          type: "Basic",
          icon: "far fa-hospital",
          valid_children: ["Top-up"]
        },
        {
          type: "Top-up",
          icon: "far fa-plus-square",
          valid_children: []
        }
      ],
      treeData: [
        {
          id: 100767.0, text: "Employee", type: "FMM_EMPLOYEE", count: 0,
          children: [
            {
              id: 100811.0, text: "Basic plan", type: "Basic", count: 0,
              children: [
                {
                  id: 101161.0, text: "Top-up", type: "Top-up", count: 152, children: []
                }
              ]
            },
            {
              id: 100812.0, text: "Basic plan", type: "Basic", count: 0, children: []
            },
            {
              id: 101162.0, text: "This Top-up can be at level 2", type: "Top-up", count: 152, children: []
            }
          ]
        },
        {
          id: 100768.0, text: "Spouse", type: "FMM_SPOUSE", count: 0,
          children: [
            {
              id: 100813.0, text: "Basic plan", type: "Basic", count: 0, children: [
                {
                  id: 101163.0, text: "Top-up", type: "Top-up", count: 152, children: []
                }
              ]
            },
            {
              id: 100814.0, text: "Basic plan", type: "Basic", count: 0, children: [
                {
                  id: 101164.0, text: "Top-up", type: "Top-up", count: 152, children: []
                }
              ]
            }
          ]
        },
        {
          id: 100769.0, text: "Child", type: "FMM_CHILD", count: 0, children: [
            {
              id: 100815.0, text: "Basic plan", type: "Basic", count: 0, children: [
                {
                  id: 101165.0, text: "Top-up", type: "Top-up", count: 152, children: []
                }
              ]
            },
            {
              id: 100816.0, text: "Basic plan", type: "Basic", count: 0, children: [
                {
                  id: 101166.0, text: "Top-up", type: "Top-up", count: 0, children: []
                }
              ]
            }
          ]
        },
        {
          id: 100770.0, text: "Parents", type: "FMM_PARENT", count: 0, children: [
            {
              id: 100817.0, text: "Basic plan", type: "Basic", count: 0, children: [
                {
                  id: 101167.0, text: "Top-up", type: "Top-up", count: 124, children: []
                }
              ]
            }
          ]
        }
      ],
      contextItems: [],
      selectedNode: null
    };
  },
  methods: {
    getTypeRule(type) {
      var typeRule = this.treeTypes.filter(t => t.type == type)[0];
      return typeRule;
    },
    contextSelected(command) {
      switch (command) {
        case "Create Basic":
          var node = {
            text: "New Basic Plan",
            type: "Basic",
            children: []
          };
          this.selectedNode.addNode(node);
          break;
        case "Create Top-up":
          var node = {
            text: "New Top-up",
            type: "Top-up",
            children: []
          };
          this.selectedNode.addNode(node);          
          break;
        case "Rename":
          this.selectedNode.editName();
          break;
        case "Remove":
          break;
      }
    },
    selected(node) {
      this.selectedNode = node;
      this.contextItems = [];
      var typeRule = this.getTypeRule(this.selectedNode.model.type);
      typeRule.valid_children.map(function(type, key) {
        var childType = this.getTypeRule(type);
        var item = {
          title: "Create " + type,
          icon: childType.icon,
          type: childType
        };
        this.contextItems.push(item);
      }, this);

      this.contextItems.push({ title: "Rename", icon: "far fa-edit" });
      this.contextItems.push({ title: "Remove", icon: "far fa-trash-alt" });
    }
  },
  components: {
    VTreeview
  }
};

v-treeview's People

Contributors

hyunoosung avatar kdepp 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

Watchers

 avatar  avatar  avatar  avatar

v-treeview's Issues

Vue 3 support

when tryin to implement it in vuejs3 project, right click is not workin. How can we solve this problem?

Is it possible to select a tree item programatically

I would like to select a tree item based on some condition. So each time when i load the tree view i want to have a node selected. Is it possible with current implementation? If yes, any sample code on that is really helpful.

count property / custom data

can i use the count property for store custom data?
Or there is other way to access custom data when node selected

thks~

Context Menu is not working with new Vue 2.6

Hi @hyounoo

This component was working fine with Vue2.5 but not working as expected in the latest Vue2.6 release.
when I right click using the mouse it triggers both events for the context menu of v-treeview and context menu of Browser and clicking on context menu item too not working.

Thanks

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.