Git Product home page Git Product logo

vue-jstree's Introduction

vue-jstree

npm

English/中文

Introduction

A tree plugin for vue2

DEMO

http://zdy1988.github.io/vue-jstree

NPM

    npm install vue-jstree

ES6

    import VJstree from 'vue-jstree'
    
    new Vue({
      components: {
        VJstree
      }
    })

Setup

    npm install
    npm run dev

Usage

    <v-jstree :data="data" show-checkbox multiple allow-batch whole-row @item-click="itemClick"></v-jstree>
    
    new Vue({
      data: {
        data: [
          {
            "text": "Same but with checkboxes",
            "children": [
              {
                "text": "initially selected",
                "selected": true
              },
              {
                "text": "custom icon",
                "icon": "fa fa-warning icon-state-danger"
              },
              {
                "text": "initially open",
                "icon": "fa fa-folder icon-state-default",
                "opened": true,
                "children": [
                  {
                    "text": "Another node"
                  }
                ]
              },
              {
                "text": "custom icon",
                "icon": "fa fa-warning icon-state-warning"
              },
              {
                "text": "disabled node",
                "icon": "fa fa-check icon-state-success",
                "disabled": true
              }
            ]
          },
          {
            "text": "Same but with checkboxes",
            "opened": true,
            "children": [
              {
                "text": "initially selected",
                "selected": true
              },
              {
                "text": "custom icon",
                "icon": "fa fa-warning icon-state-danger"
              },
              {
                "text": "initially open",
                "icon": "fa fa-folder icon-state-default",
                "opened": true,
                "children": [
                  {
                    "text": "Another node"
                  }
                ]
              },
              {
                "text": "custom icon",
                "icon": "fa fa-warning icon-state-warning"
              },
              {
                "text": "disabled node",
                "icon": "fa fa-check icon-state-success",
                "disabled": true
              }
            ]
          },
          {
            "text": "And wholerow selection"
          }
        ]
      },
      methods: {
        itemClick (node) {
          console.log(node.model.text + ' clicked !')
        }
      }
    })

API

Props Type Default Describe
data Array set tree data
size String set tree item size , value : 'large' or '' or ''small'
show-checkbox Boolean false set it show checkbox
allow-transition Boolean true allow use transition animation
whole-row Boolean false use whole row state
no-dots Boolean false show or hide dots
collapse Boolean false set all tree item collapse state
multiple Boolean false set multiple selected tree item
allow-batch Boolean false in multiple choices. allow batch select
text-field-name String 'text' set tree item display field
value-field-name String 'value' set tree item value field
children-field-name String 'children' set tree item children field
item-events Object {} register any event to tree item, example
async Function async load callback function , if node is a leaf ,you can set 'isLeaf: true' in data
loading-text String 'Loading' set loading text
draggable Boolean false set tree item can be dragged , selective drag and drop can set 'dragDisabled: true' and 'dropDisabled: true' , all default value is 'false'
drag-over-background-color String '#C9FDC9' set drag over background color
klass String set append tree class

Methods in node.model

Method Params
addChild (object) newDataItem
addAfter (object) newDataItem, (object) selectedNode
addBefore (object) newDataItem, (object) selectedNode
openChildren
closeChildren

Event

@item-click(node, item, e)

@item-toggle(node, item, e)

@item-drag-start(node, item, e)

@item-drag-end(node, item, e)

@item-drop-before(node, item, draggedItem, e)

@item-drop(node, item, draggedItem, e)

node : current node vue object

item : current node data item object

e : event

Data Item Optional Properties

Name Type Default Describe
icon String custom icon css class
opened Boolean false set leaf opened
selected Boolean false set node selected
disabled Boolean false set node disabled
isLeaf Boolean false if node is a leaf , set true can hide '+'
dragDisabled Boolean false selective drag
dropDisabled Boolean false selective drop

Custom Item Example

<v-jstree :data="data">
  <template scope="_">
    <div style="display: inherit; width: 200px" @click.ctrl="customItemClickWithCtrl">
      <i :class="_.vm.themeIconClasses" role="presentation" v-if="!_.model.loading"></i>
      {{_.model.text}}
      <button style="border: 0px; background-color: transparent; cursor: pointer;" @click="customItemClick(_.vm, _.model, $event)"><i class="fa fa-remove"></i></button>
    </div>
  </template>
</v-jstree>

more elegant:

<v-jstree :data="data">
  <template scope="_">
    <div style="display: inherit; width: 200px" @click.ctrl="customItemClickWithCtrl" @click.exact="customItemClick(_.vm, _.model, $event)">
    <i :class="_.vm.themeIconClasses" role="presentation" v-if="!_.model.loading"></i>
    {{_.model.text}}
    </div>
  </template>
</v-jstree>

scope be replaced in the [email protected]+ , over [email protected]+ use slot-scope

License

Licensed under the MIT license.

Thanks For jstree's UI

vue-jstree's People

Contributors

wdda avatar zdy1988 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  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  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  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

vue-jstree's Issues

addChild is not a function

Hi Zhu,

It appears that if I take the same example as in the documentation, I'm not able to addChild on a node.

image

Node editing and deletion works fine but I'm afraid I don't get addChild to work properly, the exact error message is "Uncaught TypeError: this.editingNode.addChild is not a function"

Reorder items

I really miss the possibility to reorder items. Do you have any plans to add this feature? If so, is there a way to help you?

Setting treedata won't allow me to expand collapsed nodes

Hello, i'm trying to setting the tree data dynamic, but somehow the tree won't expand collapsed nodes after the tree is build:

me.treeData = [{ "text": "Same but with checkboxes", "children": [ { "text": "initially selected", "selected": true }, { "text": "custom icon", "icon": "fa fa-warning icon-state-danger" }, { "text": "initially open", "icon": "fa fa-folder icon-state-default", "opened": true, "children": [ { "text": "Another node" } ] }, { "text": "custom icon", "icon": "fa fa-warning icon-state-warning" }, { "text": "disabled node", "icon": "fa fa-check icon-state-success", "disabled": true } ] }, { "text": "Same but with checkboxes", "opened": true, "children": [ { "text": "initially selected", "selected": true }, { "text": "custom icon", "icon": "fa fa-warning icon-state-danger" }, { "text": "initially open", "icon": "fa fa-folder icon-state-default", "opened": true, "children": [ { "text": "Another node" } ] }, { "text": "custom icon", "icon": "fa fa-warning icon-state-warning" }, { "text": "disabled node", "icon": "fa fa-check icon-state-success", "disabled": true } ] }, { "text": "And wholerow selection" }];

Regards

Drag event (e.g. to expand an item on hover)

First of all: Thanks for this component, it's nice! 🎉

I need the possibility to open items on hover.
As stated in the title I want to expand an item if it is hovered by another item (another item is dragged onto this item). I tried to achieve this using the item-drag-start and item-drag-end events (to set a dragging flag) and the mouseover event in item-events to check the dragging flag and expand the hovered item if dragging = true. But the mouseover event is not fired, since the dragged element is to top-most element.
Would it be possible to add a drag event or is there any other solution I don't see? :)

how to add a function

我该如何新添一个事件,我想增加右击事件,和hover事件,该怎么做?

Async Loading

Please provide the option to directly set a leaf

maybe something like this ?
handleAsyncLoad(oriParent, oriNode, oriItem) { var self = this if (this.async) { if (oriParent[0].loading) { this.async(oriNode, (data) => { if (data.length > 0) { for (let i in data) { if(!data[i].isLeaf) { data[i].children = [self.initializeLoading()] } var dataItem = self.initializeDataItem(data[i]) self.$set(oriParent, i, dataItem) } } else { oriNode.model.children = [] } }) } } },

How to make leaf node in AJAX?

When I load the tree dynamically, can I specify that it doesn't have any children? I tried to set children property to false or [ ], but it didn't work:

var leaf_lode = {
    text: 'some description',
    children: false,
    id: 'some id'
};

TypeError: Cannot read property 'parentItem' of null

There is some error on Drag'n'Drop:

[Vue warn]: Error in nextTick: "TypeError: Cannot read property 'parentItem' of null vue.runtime.esm.js?ff9b:1713 TypeError: Cannot read property 'parentItem' of null at VueComponent.eval (webpack-internal:///278:1:8400)

here:
var r=this;this.$nextTick(function(){r.draggedItem.parentItem.splice(r.draggedItem.index,1)})

So nextTick seems to be called after onItemDragEnd...

If change to this:
var r=this.draggedItem;this.$nextTick(function(){r.parentItem.splice(r.index,1)})
then no errors anymore!

@item-toggle, event never fired

Thanks for this fantastic plugin integration for Vue 2.0.

The plugin is working fine and as expected, I only have 2 questions (things that I cant' achieve)

  • Detect when an unselected node is toggled (expanded / collapsed) as I need to store the opened / closed state of nodes. Tried @item-toggle="itemToggle" as @item-click is working fine but I didn't succeed, it does nothing even with a console.log inside to see if it goes through the function (my assumption is the function is never called and the event onItemToggle is never fired

  • I want to send a node deletion command to the tree using Vue 2.0 (and also rename the node) as I created buttons to create new nodes (works fine), delete an existing node (undone for the moment), rename a node (undone for the moment).

Any help would be greatly appreciated and at the end I'll be able to share a full example with more capabilities than the current documentation.

Thanks in advance.

Need the ability to receive "external" drop.

I have an application where I simulate actual system folders. I need the "treeview" to trigger a method that will accept the payload of the "drag-and-drop" event. I'm not quite sure I understand your API on exposing events. Would it be possible to expose drop and dragend in the same manner that you expose item-click?

Updating 'data' props from parent does not seem to call `.initializeData`

Hi,

I was unable to access functions like openChildren closeChildren from my item using the item-click event. It seems that .initializeData is only called in created hook. In my case, I do update data from outside of my component.

It this intended? I tried to used updated instead of created but wasn't able to get it work either..

Thanks for your help!

Error in initializeData

Error in created hook: "TypeError: Cannot assign to read only property '0' of object '[object Array]'"

screenshot_3

I think this is because of trying to mutate the 'data' prop object.

icon

how to change icon

Custom text variable doesn't work

Hello,

if you don't use text field name "text", the item will not be shown in the tree. Please, make the correction in:

Tree Item.vue to this:

        initializeDataItem (item) {
            function Model(item, textFieldName, valueFieldName) {
                this.id = item.id || ITEM_ID++
                this['text'] = item[textFieldName] || ''
                this['value'] = item[valueFieldName] || item[textFieldName]
                this.icon = item.icon || ''
                this.opened = item.opened || false
                this.selected = item.selected || false
                this.disabled = item.disabled || false
                this.loading = item.loading || false
                this.children = item.children || []
            }

Bugs on your example

Bugs on your example!

  1. when click top line same but with checkboxes also selected node initially open!
  2. edit tree/ click the tree node - table has two row with icon filed

if possible - add drop to external component example

How i can update the tree vue when the route is change to same component with other data?

this is my async code:

async loadData(oriNode, resolve) {
                var data = [];
                if (!oriNode || !oriNode.data.id) {
                    debugger;
                    let _oe = new objectEntities;
                    let _objectName = _oe.getObject(this.objectId).LocalizeName;
                    data = null;
                    data = [
                        {
                            "text": _objectName,
                            "value": "0",
                            "id": "0",
                        },
                    ];
                }
                else {
                    let os = new objectService;
                    var _apiData = [];
                    if (oriNode.data.value === "0") {
                        _apiData = await os.loadLookupData(this.objectId, "ParentID is Null");
                    }
                    else {
                        _apiData = await os.loadLookupData(this.objectId, "ParentID = " + oriNode.data.value);
                    }
                    data = _apiData.map(function (s) {
                        var childlength = s.children;
                        if (childlength === 0) {

                            return {
                                "text": s.name + childlength,
                                "value": s.id,
                                "id": s.id,
                            }
                        }
                        else {
                            return {
                                "text": s.name + childlength,
                                "value": s.id,
                                "id": s.id,
                            }
                        }
                    });
                }
                resolve(data);
            },

watch: {
            '$route'(to, from) {
                this.loadData();
            },
        }

关于vue-jstree数据的问题

貌似该插件只支持写死的数据,如果从后台请求过来的数据更新了data之后没有初始化,里面的所有方法都没有了,比如addAfter(),addChild()等都失效了

咨询两个问题

我现在有个系统在做从angularjs迁移vue的工作, 里面用到了jstree, 不过用angularjs进行了封装, 原有的jstree功能都继承过来了, 包含右键菜单, 数据转换, 所有事件与API. 在迁移VueJS的时候本来想找找看有没有类似angularjs现成的jstree组件.
发现了你的项目, 第一眼从api和sample中的数据结构/样式, 差点以为是原生jstree的封装.
我有两点疑问请教一下:

  1. 我是刚开始用Vue还不太熟练, Jstree发展了很多年了, 功能和稳定度都已经很成熟了, 为什么作者当初没有选择对原有jstree进行封装, 而是自己重新实现一遍, 这样很多jstree原有的功能都丢失了, 比如:右键菜单, 其他的还没细看

  2. 原有的jstree能够直接处理下面这种父子节点关系声明式的数据, 在vue-jstree里面是要自己处理成层级关系的数据才能用吗

'data' : [
       { "id" : "ajson1", "parent" : "#", "text" : "Simple root node" },
       { "id" : "ajson2", "parent" : "#", "text" : "Root node 2" },
       { "id" : "ajson3", "parent" : "ajson2", "text" : "Child 1" },
       { "id" : "ajson4", "parent" : "ajson2", "text" : "Child 2" },
    ]

期待回答, 谢谢

Exposure of drag/drop events unclear

From looking at the source, I can see that item-click is accessible. I can even access drop However, I would also think that item-drag-start (or drag-start) and the like would be exposed. Please explicitly state all exposed events. If there are only the few listed, please expose more.

Primary use case I need to solve involves updating folder hierarchy on the server. While I can acquire the target folder, I need to know what item is being dragged and dropped. I suppose I could work around it by looking inside the target folder then calling update for every contained folder, but that seems a terrible solution.

Failed to mount component: template or render function not defined

按照wiki里面的介绍,使用组件的时候报上面的错误。

报错的详细信息如下:

17:573 [Vue warn]: Failed to mount component: template or render function not defined.

found in

---> <VJstree>
       <ElAside>
         <ElContainer>
           <Wiki> at D:\WeiboGit\content-platform-static\src\components\page\content\board\Wiki.vue
             <Home> at D:\WeiboGit\content-platform-static\src\components\common\board\Home.vue
               <App> at D:\WeiboGit\content-platform-static\src\App.vue
                 <Root>

下面是我引用的时候页面的代码:

<template>
    <el-container class="el-container">
        <el-aside class="el-aside" width="300px">
            <v-jstree :data="treeData" show-checkbox multiple allow-batch whole-row @item-click="itemClick"></v-jstree>
        </el-aside>
        <el-container>
            <el-main>

            </el-main>
        </el-container>
    </el-container>
</template>

<script>
    import VJstree from 'vue-jstree'

    export default {
        components: {
            VJstree
        },
        data() {
            return {
                treeData: [
                    {
                        "text": "Same but with checkboxes",
                        "children": [
                            {
                                "text": "initially selected",
                                "selected": true
                            },
                            {
                                "text": "custom icon",
                                "icon": "fa fa-warning icon-state-danger"
                            },
                            {
                                "text": "initially open",
                                "icon": "fa fa-folder icon-state-default",
                                "opened": true,
                                "children": [
                                    {
                                        "text": "Another node"
                                    }
                                ]
                            },
                            {
                                "text": "custom icon",
                                "icon": "fa fa-warning icon-state-warning"
                            },
                            {
                                "text": "disabled node",
                                "icon": "fa fa-check icon-state-success",
                                "disabled": true
                            }
                        ]
                    },
                    {
                        "text": "Same but with checkboxes",
                        "opened": true,
                        "children": [
                            {
                                "text": "initially selected",
                                "selected": true
                            },
                            {
                                "text": "custom icon",
                                "icon": "fa fa-warning icon-state-danger"
                            },
                            {
                                "text": "initially open",
                                "icon": "fa fa-folder icon-state-default",
                                "opened": true,
                                "children": [
                                    {
                                        "text": "Another node"
                                    }
                                ]
                            },
                            {
                                "text": "custom icon",
                                "icon": "fa fa-warning icon-state-warning"
                            },
                            {
                                "text": "disabled node",
                                "icon": "fa fa-check icon-state-success",
                                "disabled": true
                            }
                        ]
                    },
                    {
                        "text": "And wholerow selection"
                    }
                ],

            }
        },
        methods: {
            itemClick(node) {
                console.log(node.model.text + ' clicked !')
            }
        },
    }
</script>

<style scoped>
   
</style>

IE11 Support?

Do you have IE11 support? When I tried to open your demo page I receive errors and it fails to render the page.

Customizing problem!

I can't customizing! When I set the style it affects the height. How to set padding between items.
image
If you specify a padding, the script does not correctly calculate the height.

后台load数据的问题

我后台load数据到data中的一个list,然后照着demo写了,数据是帮上去了,但是点击事件都没了,如果我用后来load的json数据直接写到data里,就没有这个问题,能帮忙看看吗

get selected items ids

hi thanks for update it's very use full.

can you help me to get array of selected ids?

like this:
{1,23,43,12}

allow-batch or allowBatch?

How correctly the parameter is named?

On manual it called allowBatch
in the example it called allow-batch

What does it do?

When is stable release expected to be released?

Looks really good and handy library. I am looking to move to vuejs and hence stumbled into this as I like to include jstree to one of my works.

Wondering if there is any timeline for the stable release of vue-jstree?

Thanks heaps!

Example of how to save state?

I'm trying to implement a local storage (vuex) of the current state of the tree, so that it will not start "closed" when I return the next time.

Managed to save current state through @item-toggle callback, but if I use it to set initial state i get an infinite loop. Could you please provide an example of how to solve this?

Also, I'm wondering if there are any plans on handling drag-n-drop to change the individual order of nodes? Seems like it could only handle dragging between different parents today?

EVENTS onBeforeNodeOpen, onAfterNodeClosed

Please add events:

onBeforeNodeOpen(node) - on click "+"
onAfterNodeClosed(node) - on click "-"

onBeforeNodeOpen - i will load node children from database to tree (If several users simultaneously work with the tree, need to update the children before open the node)
onAfterNodeClosed - i will delete node children and create one fake child for "+" was visible

This will solve the performance problem - because the total number of nodes in the tree will decrease.

API clarification

Hi, could you please explain how to use
textFieldName and valueFieldName props?

Custom html for the items

Hi, very nice and useful lib!

Can I use custom html to render the tree items? for example using slots or whatever?
My problem is that I need a more complex item, that is, not just a text line but also images and other info, so I can't just customize the item with CSS.

Luca

when multiple set false,can not unselect

if set multiple=true,it allow to unselect all node,
but when set multiple=false,if i select a node ,i can not unselect it
i think it reasonable that user want to cancel it if select by mistake.
thanks!

How do we enable selection-related CSS?

In the demos it appears that selected rows highlight grey. However, using a vanilla install, this doesn't seem to be the case. Full, disclosure, I am using Bootstrap, but I can't discern any collisions of any sort. Please advise.

Performace

To my example i added performance test

Use my example to check performance

  1. click on Folder1 then add 1000 files to it, click '-'
  2. click on Folder2 then add 1000 files to it, click '-'
  3. Right click on Folder3 and add one file

click +/- of Folder3 - get big delay! Why? There is only one file!
What is calculated so long?

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.