0.0.4 • Published 3 years ago

@reallife/vue-tree-grid v0.0.4

Weekly downloads
31
License
MIT
Repository
github
Last release
3 years ago

Vue Tree Grid

WORK IN PROGRESS! USE IN PRODUCTION ON YOUR OWN RISK!

Install

npm i --save @reallife/vue-tree-grid

or

yarn add @reallife/vue-tree-grid

Usage

<template>
  <div id="app">
    <VueTreeGrid
      :columns="columns"
      :tree="tree"
      @check="onRowChecked"
      @expand="onRowExpanded"
    >
      <template slot="description--row-cell" slot-scope="scope">
        Path: <code>{{scope.row._path}}</code>
      </template>
    </VueTreeGrid>
  </div>
</template>

<script>
import VueTreeGrid from '@reallife/vue-tree-grid' 

export default {
  name: 'app',
  components: {
    VueTreeGrid,
  },
  data() {
    return {
      columns: [
        {
          type: 'checkbox',
          width: '10%',
        },
        {
          type: 'prop',
          prop: 'name',
          label: 'Name',
          width: '20%',
        },
        {
          type: 'prop',
          prop: 'description',
          label: 'Description',
          width: '70%',
        },
      ],
      tree: [
        {
          name: 'Parent 1',
          _isExpanded: true,
          children: [
            {
              name: 'Child 1',
            },
            {
              name: 'Child 2',
              children: [
                {
                  name: 'Child 2-1',
                  _isChecked: true,
                },
                {
                  name: 'Child 2-2',
                },
              ],
            },
          ],
        },
      ],
    };
  },
  methods: {
    onRowChecked(checkedRows) {
      console.log(checkedRows);
    },
    onRowExpanded(row) {
      if (row._isExpanded) {
        console.log('Row with path' + row._path + ' has been expanded');
      } else {
        console.log('Row with path' + row._path + ' has been collapsed');
      }
    }
  },
};
</script>

Component properties

NameRequiredTypeDefaultDescription
columnsyesArray (Details)-Columns array
treeyesArray (Details)-Tree structure of rows
childrenPropertynoString'children'Name of tree's row property that contains children rows
emptyTextnoString'No data'Text for empty grid
rowClassnoFunction (params: row )-Function that returns style classes for each row

Column properties

NameRequiredTypeDefaultDescription
typeyesString-Type of column. Available values: "checkbox", "prop".
propnoString-Row's prop name for output in this column. Using only when type is "prop"
labelnoString-Column label
widthnoString'auto'Column width

Examples

[
  // Column with checkbox
  {
    type: 'checkbox',
    width: '10%'
  }
    
  // Column for row's property "name"
  {
    type: 'prop',
    prop: 'name',
    label: 'Name',
    width: '300px',
  }
]

Tree structure properties

Each row in tree after passing in component will be contain this properties:

NameTypeDefaultDescription
_isCheckedBooleanfalseDetermines is row checked
_isIndeterminateBooleanfalseDetermines is row indeterminate
_isExpandedBooleanfalseDetermines is row expanded
_isFoldedBooleanfalseDetermines is row folded (for children rows). If parent row is expanded then children rows will be folded
_isDisabledBooleanfalseDetermines is row disabled (row don't change own _isChecked property on parent row checking/unchecking)
_isHiddenBooleanfalseDetermines is row hidden (row just hidden, it continue reacts on parent checking/unchecking). If row is hidden, all it's children also hides
_childrenLengthInteger-Count of row's children.
_levelInteger-Row's level in tree structure. Counts with 1
_pathString-Row's path in tree structure. For example: 0.children.4.children.1
_indexInteger-Row's index in table

You can use this properties to define initial state of tree and change it from outside of component.

Example tree structure

[
  {
    name: 'Parent 1',
    _isExpanded: true,
    children: [
      {
        name: 'Child 1',
      },
      {
        name: 'Child 2',
        children: [
          {
            name: 'Child 2-1',
            _isChecked: true,
          },
          {
            name: 'Child 2-2',
          },
        ],
      },
    ],
  },
]

Component events

NameTypeParametersDescription
checkFunctioncheckedRows - array of checked rowsEvent fires when any row in the tree has been checked or unchecked
expandFunctionrow - row that has been expanded or collapsedEvent fires when any row in the tree has been expanded or collapsed

Component slots

You can use slots for changing output of every header label or cell content.

Slot names for body cells looks like %column_prop%--row-cell. For example: name--row-cell.

Slot names for header cells looks like %column_prop%--header-cell. For example: name--header-cell.

Example

<VueTreeGrid
  :columns="columns"
  :tree="tree"
  @check="onRowChecked"
  @expand="onRowExpanded"
>
  <template slot="description--row-cell" slot-scope="scope">
    Path: <code>{{scope.row._path}}</code>
  </template>
</VueTreeGrid>