All Projects → sarimabbas → vue-teatree

sarimabbas / vue-teatree

Licence: MIT license
🍵 A simple treeview component for Vue, with no additional dependencies.

Programming Languages

Vue
7211 projects
typescript
32286 projects
HTML
75241 projects
javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to vue-teatree

TreeView
"TreeView - sub-cells simplified" (c). Enable subcells in UITableView with a single drop-in extension. CocoaPod:
Stars: ✭ 54 (+125%)
Mutual labels:  treeview
M5Stack TreeView
M5Stack TreeView menu UI library.
Stars: ✭ 50 (+108.33%)
Mutual labels:  treeview
treelistview
A Pascal treelistview component, showing a treeview together with the columns of a listview (for Delphi and Lazarus)
Stars: ✭ 41 (+70.83%)
Mutual labels:  treeview
vue-virtualised
Blazing fast scrolling and updating for any amount of list and hierarchical data.
Stars: ✭ 18 (-25%)
Mutual labels:  treeview
react-tree
Hierarchical tree component for React in Typescript
Stars: ✭ 174 (+625%)
Mutual labels:  treeview
ExpandableRecyclerView
ExpandableRecyclerView with smoothly animation.
Stars: ✭ 412 (+1616.67%)
Mutual labels:  treeview
gitbook-treeview
🌲 A gitbook plugin for generating tree view for ech page
Stars: ✭ 38 (+58.33%)
Mutual labels:  treeview
react-dnd-treeview
A draggable / droppable React-based treeview component. You can use render props to create each node freely.
Stars: ✭ 207 (+762.5%)
Mutual labels:  treeview
liblarch
A Python library to easily handle complex data structures, with a GTK binding
Stars: ✭ 28 (+16.67%)
Mutual labels:  treeview
tree-tree
No description or website provided.
Stars: ✭ 15 (-37.5%)
Mutual labels:  treeview
android-thinkmap-treeview
Tree View; Mind map; Think map; tree map; custom view; 自定义;关系图;树状图;思维导图;组织机构图;层次图
Stars: ✭ 314 (+1208.33%)
Mutual labels:  treeview
comment tree
Render comment tree like facebook comment - reply
Stars: ✭ 37 (+54.17%)
Mutual labels:  treeview
QmlTreeWidget
A TreeView component, implemented by QML, providing convenient interfaces and customizable stylesheet, also available for iOS and Android.
Stars: ✭ 34 (+41.67%)
Mutual labels:  treeview
react-treeview-component
A react tree-component where user can customize the the tree according to their need
Stars: ✭ 18 (-25%)
Mutual labels:  treeview
react-folder-tree
A versatile react treeview library that supports custom icons and event handlers
Stars: ✭ 56 (+133.33%)
Mutual labels:  treeview
vuejs-tree
A highly customizable and blazing fast Vue tree component ⚡🌲
Stars: ✭ 310 (+1191.67%)
Mutual labels:  treeview
TreeEdit
Qml TreeEdit with Controls2 (Qml树结构编辑器,使用Controls2实现)
Stars: ✭ 50 (+108.33%)
Mutual labels:  treeview
FilterTreeView
WPF/MVVM Search and Filter Reference Application
Stars: ✭ 56 (+133.33%)
Mutual labels:  treeview
ADM-treeView
Pure AngularJs TreeView
Stars: ✭ 30 (+25%)
Mutual labels:  treeview
elm-treeview
ELM tree view component
Stars: ✭ 20 (-16.67%)
Mutual labels:  treeview

🍵 Vue Teatree

A simple treeview component for VueJS with no added dependencies.

Animated

Install

yarn add vue-teatree # (or use npm)

https://www.npmjs.com/package/vue-teatree

Example

  1. Import your components:
import { Teatree, NodeType, NodeName, NodeToggle } from "vue-teatree";
  • Teatree is the main component you will pass your data to (below).
  • NodeType contains the Typescript interface that defines each node type.
  • NodeName is a pre-built component you will pass into a slot.
  • NodeToggle is a pre-built component you will pass into a slot.

NodeName and NodeToggle are provided as a convenience. Feel free to write your own.

  1. Prepare your data
const data: NodeType[] = [
  {
    name: "parent",
    show: true,
    showChildren: true,
    selected: false,
    children: [
      {
        name: "child",
        show: true,
        showChildren: true,
        selected: false,
        children: [
          {
            name: "grandchild",
            show: true,
            showChildren: true,
            selected: false,
            children: [],
          },
        ],
      },
    ],
  },
];
  1. Pass it into Teatree
<Teatree :roots="data">
  <template slot="node-toggle" slot-scope="{ node }">
    <NodeToggle :node="node" />
  </template>
  <template slot="node-name" slot-scope="{ node }">
    <NodeName
      :node="node"
      :handleNodeLeftClick="() => {}"
      :handleNodeRightClick="() => {}"
    />
  </template>
</Teatree>

More details

  1. The Teatree treeview is purely a function of your data. If you want to make changes to the treeview (e.g. toggling children, hiding nodes etc.) you should modify the data object.
  2. NodeToggle and NodeName can be replaced with your own components and passed into the node-toggle and node-name slots respectively.

NodeType

Teatree accepts an array of NodeType as its roots prop. This means you can render multiple roots in the treeview.

interface NodeType {
  // show: toggling this will show/hide the node and its children
  show: boolean;
  // showChildren: toggling this will toggle its children
  showChildren: boolean;
  // selected: toggling this will apply the "selected" CSS style
  selected: boolean;
  // children: the children must also conform to the node specification
  children: Array<NodeType>;
  // name: the name of the node
  name: string;
  // icon: base64 encoded icon (optional)
  icon?: string;
  // data: payload by the user of the library (optional)
  data?: object;
}

NodeToggle

It is a pre-built component to render the node's toggle. If you want to implement your own, take a look at the source code!

Props:

Name Type Required Notes
node NodeType Yes

NodeName

It is a pre-built component to render the node's name. If you want to implement your own, take a look at the source code!

It has a number of props that can be wired up to provide additional functionality (track clicks etc.):

Name Type Required Notes
node NodeType Yes
handleNodeLeftClick (event: any, node: NodeType) => any Yes Pass an empty function () => {} if you don't have one.
handleNodeRightClick (event: any, node: NodeType) => any Yes Pass an empty function () => {} if you don't have one.

Styling

Import default styles using: @import "~vue-teatree/build/Teatree.css";

Here are all the default styles. Override them to your liking:

Teatree

.teatree {
  cursor: pointer;
  height: 100%;
  overflow: hidden;
}

.teatree-node {
  padding-right: 0.25rem;
}

.teatree-node-item {
  display: flex;
  align-items: center;
  height: 1.5rem;
  background: transparent;

  /* hack to make hover the full width of parent */
  padding-left: 100%;
  margin-left: -100%;
  padding-right: 100%;
  margin-right: -100%;
}

.teatree-node-item:hover {
  background-color: #718096;
}

.teatree-node-item-selected {
  background-color: #718096;
}

.teatree-node-item-name-padded-leaf {
  padding-left: 1.25rem;
}

NodeToggle

.teatree-node-item-icon {
  display: flex;
  align-items: center;
  margin-left: 0.5rem;
  color: #cbd5e0;
}

NodeName

.teatree-node-item-name {
  display: inline-block;
  font-size: 0.875rem;
  color: #a0aec0;
  margin-left: 0.5rem;
  user-select: none;

  /* truncate */
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.teatree-node-item-name-padded {
  padding-left: 1.5rem;
}

Common use cases

The following code/advice is not well tested. Apologies for any errors.

Toggle everything shut

You can achieve an effect similar to VSCode where you can toggle/collapse all nodes closed. You need to write a simple tree-traversal:

const toggleTreeClosed = (rootNode: NodeType) => {
  rootNode.showChildren = false;
  rootNode.children.forEach((child) => toggleTreeClosed(child));
};

Hide leaves

If you're using the treeview for a file explorer, it might be useful to hide the leaves, and only show the parent directories. You can use the show node property to hide the leaf nodes.

const isLeaf = (node: NodeType) => {
  return !node.children.length;
};

const hideTreeLeaves = (rootNode: NodeType) => {
  if (isLeaf(rootNode)) {
    rootNode.show = false;
  } else {
    rootNode.children.forEach((child) => hideTreeLeaves(child));
  }
};

Show a right-click menu

You'll need something like: https://github.com/rawilk/vue-context.

Use the handleNodeRightClick component prop to call the context menu:

export default class MyComponent extends Vue {
  handleNodeRightClick(node: NodeType, event: any) {
    this.$refs.menu.open(event, node);
  }
}
<vue-context ref="menu">
  <template slot-scope="child">
    <your-menu-component :node="child.data" />
  </template>
</vue-context>
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].