1.3.2 • Published 5 years ago

lb-vue-tree v1.3.2

Weekly downloads
15
License
GNU
Repository
gitlab
Last release
5 years ago

Screenshot

Description

Install

npm install lb-vue-tree --save

Include plugin in your main.js file.

import Vue from 'vue'
import lbVueTree from 'lb-vue-tree';
Vue.component('lb-vue-tree', lbVueTree);

Usage

Local data (or complete serverside load)

In this example, if you have a call to server, or static data that is smaller and has all the nodes inside, you can fetch data once and use it in a tree.

template code for local data

<template>
    <lb-vue-tree v-model="data" v-on:itemclick="itemClick" display-field="text">
        <template slot="rootitem"slot-scope="{item, parent}">
            <span>Root item</span>
        </template>
        <template slot="menu" slot-scope="{item, parent}">
            <ul>
                <li>New</li>
                <li>Edit</li>
            </ul>
        </template>
        <template v-slot:itemcontent="{item}">
            <div class="item-inputs">
                Min: <input type="text" v-model="item.text">
                Max: <input type="text">
                Active: <input type="checkbox">
            </div>
        </template>
    </lb-vue-tree>
</template>

script code for local data

export default {
    data(){
        return {
            data: [{
                id: '1',
                text: 'test',
                children: [{
                    id: '2',
                    text: 'child test'
                }]
            }]
        }
    },
    methods: {
        itemClick(item, parent) {

        }
    }
};

Remote data (server side data), fetching node by node, also draggable support

In this case you can load server side data only for singe node, so for example you will load only initial root level, then by clicking on the nodes your assigned method will be called to fetch "ONLY" that node children

Method for fetching node data MUST be a promise, callbacks are not supported

template code for server side data

<template>
    <lb-vue-tree v-model="data" :async-method="getData" v-on:itemclick="itemClick" :draggable="true" v-on:dragend="dragEnd" display-field="username">
        <template slot="rootitem">
            <span>Root item</span>
        </template>
        <template slot="menu" slot-scope="{item, parent}">
            <ul>
                <li>New</li>
                <li>Edit</li>
            </ul>
        </template>
    </lb-vue-tree>
</template>

There are two optional slots to use: "rootitem": used if you wan't to display root item of the tree in case your tree data does not have it and you need a starting point "menu": if you need a menu on right click, you can add it here, there are also two arguments, item and parent passed to menu slot

script code for remote data

export default {
    data(){
        return {
            data: []
        }
    },
    async created()
    {
        //make an initial load of data
        this.data = await this.getData(null);
    },
    methods: {
        itemClick(item) {

        },
        dragEnd(data)
        {
            //data.start holds the item you are dragging
            //data.end holds the item dragged on
            //data.position holds the postion related to end position (upper, center, lower)
            console.log(data.start, data.end, data.position);
        },
        async getData(item, parent)
        {
            if(item != null)
            {
                //fetch data from server for specific node based on data from item, for example:
                return await axios.get('/getTreedata?id='+item.id)
            }
            else
            {
                return await axios.get('/getTreedata')
            }
        }
    }
};

Available settings

PropertyTypeRequiredDescription
value (v-model)Array*value array
display-fieldstringDisplay field key from value item object (Defaults to text)
display-funcfunctionIf you want to combine multiple fields to display text, you can specify custom function which receives one argument (item)
children-fieldstringChildren field key (key which holds descendants) from value item object (Defaults to children)
children-expanded-fieldstringIf you want to show this branch already expanded set this to boolean field (Defaults to expanded)
has-children-fieldstringIn case of async tree, if you already know if that branch has children set the filed that holds the boolean here
checkboxbooleanIf you want to enable checkboxes in the tree (Defaults to false)
checked-fieldstringKey from value which holds boolean state if the item is checked or not
async-methodfunctionIf you want to use serverside fetching node by node (in case of big tree) method receives item as argument
draggablebooleanIf you want to enable drag and drop, put boolean true here (not string)
clickonopenbooleanIf you want to enable item click event on openning of the item
showvaluebooleanIf you have a display function and you don't wan't to display value also, put this to false
showsearchbooleanIf you want to have search boxes for each level enabled
searchPlaceholderstringString to use for search placeholder on each item, you can use {level} specifier inside string

Available events

EventArgumentsDescription
itemclickitem, parentItemitem from data array you clicked on and it's parent
itemrightclickitem, parentItemitem from data array you clicked on with right mouse button and it's parent
dragenddataFunction to be called when drag ends, one argument, object with start, end and position attributes

1.3.2

5 years ago

1.3.1

5 years ago

1.3.0

5 years ago

1.2.0

5 years ago

1.1.6

5 years ago

1.1.5

5 years ago

1.1.4

6 years ago

1.1.3

6 years ago

1.1.2

6 years ago

1.1.1

6 years ago

1.1.0

6 years ago

1.0.42

6 years ago

1.0.41

6 years ago

1.0.40

6 years ago

1.0.39

6 years ago

1.0.38

6 years ago

1.0.37

6 years ago

1.0.36

6 years ago

1.0.35

6 years ago

1.0.34

6 years ago

1.0.33

6 years ago

1.0.31

6 years ago

1.0.30

6 years ago

1.0.29

6 years ago

1.0.27

6 years ago

1.0.19

6 years ago

1.0.18

6 years ago

1.0.17

6 years ago

1.0.16

6 years ago

1.0.14

6 years ago

1.0.13

6 years ago

1.0.12

6 years ago

1.0.11

6 years ago

1.0.10

6 years ago

1.0.9

6 years ago

1.0.8

6 years ago

1.0.5

7 years ago

1.0.4

7 years ago

1.0.3

7 years ago

1.0.2

7 years ago