Vue UI Components Documentation
Vue TreeGrid Component
The TreeGrid component for Vue represents a lightweight widget which represents data in a tree-like structure.
Prerequisites
Refer to Vue Getting Started before you start with this help topic.
Configuration
After you have created your App.vue file, here is how you should structure it:
The TreeGrid component for Vue requires the following import:
import JqxTreeGrid from 'jqwidgets-scripts/jqwidgets-vue/vue_jqxtreegrid.vue';
Add the jqxTreeGrid component to the components section of the the Vue class:
components: { JqxTreeGrid },
Template
The App.vue has a <template>
structural tag where we determine the application structure.
There we will also set the tags for the new components - <JqxTreeGrid/>
<template> <JqxTreeGrid :width="width" :pageable="pageable" :columns="columns" :source="dataAdapter"> </JqxTreeGrid></template>
Properties
The properties of the <JqxTreeGrid/> component are defined in the data
member of the Vue class.
We should put them in the return object of the data function:
data: function () { return { width: 800, pageable: true, dataAdapter: new jqx.dataAdapter(this.source), columns: [ { text: 'FirstName', dataField: 'FirstName', minWidth: 100, width: 200 }, { text: 'LastName', dataField: 'LastName', width: 200 }, { text: 'Department Name', dataField: 'DepartmentName', width: 150 }, { text: 'Title', dataField: 'Title', width: 300 }, { text: 'Birth Date', dataField: 'BirthDate', cellsFormat: 'd', width: 120 }, { text: 'Hire Date', dataField: 'HireDate', cellsFormat: 'd', width: 120 }, { text: 'Phone', dataField: 'Phone', cellsFormat: 'd', width: 120 } ] }}
Events
The events in Vue are set as an attribute with @ prefix, for example:
<JqxTreeGrid @rowSelect="onRowSelect()" :width="width" :pageable="pageable" :columns="columns" :source="dataAdapter"></JqxTreeGrid>
All events that are bound to a component are implemented in the methods
member of the Vue class.
methods: { onRowSelect: function () { // Do something... }}
Methods
To use a component's method we should have its reference. In Vue we refer to a component by the special $refs
property.
Before that we need to add the desired name reference to that component:
<JqxTreeGrid ref="treegrid"></JqxTreeGrid>
Here how you can use a component's method:
this.$refs.treegrid.getSelection();
methods: { // Add here all used callbacks and/or events onRowSelect: function () { // Do something... let selection = this.refs.treegrid.getSelection(); alert(selected); }}
If we want to add additional methods we should also implement them in the methods
member.
In case we need to do some precalculation or something else before the components are rendered, we should use the beforeCreate
member.
In this case:
beforeCreate: function () { // Add here any data where you want to transform before components be rendered this.source = { dataType: "csv", dataFields: [ { name: 'EmployeeKey', type: 'number' }, { name: 'ParentEmployeeKey', type: 'number' }, { name: 'FirstName', type: 'string' }, { name: 'LastName', type: 'string' }, { name: 'Title', type: 'string' }, { name: 'HireDate', type: 'date' }, { name: 'BirthDate', type: 'date' }, { name: 'Phone', type: 'string' }, { name: 'Gender', type: 'string' }, { name: 'DepartmentName', type: 'string' } ], hierarchy: { keyDataField: { name: 'EmployeeKey' }, parentDataField: { name: 'ParentEmployeeKey' } }, id: 'EmployeeKey', url: 'sampledata/employeesadv.csv' };},
If you have followed the above steps, you App.vue file would look like this:
App.vue:
<template> <JqxTreeGrid ref="treegrid" @rowSelect="onRowSelect()" :width="width" :pageable="pageable" :columns="columns" :source="dataAdapter"> </JqxTreeGrid></template> <script> // Import the components that will be used import JqxTreeGrid from 'jqwidgets-scripts/jqwidgets-vue/vue_jqxtreegrid.vue'; export default { components: { // Adding imported widgets here JqxTreeGrid }, data: function () { // Define properties which will use in the widget return { width: 800, pageable: true, dataAdapter: new jqx.dataAdapter(this.source), columns: [ { text: 'FirstName', dataField: 'FirstName', minWidth: 100, width: 200 }, { text: 'LastName', dataField: 'LastName', width: 200 }, { text: 'Department Name', dataField: 'DepartmentName', width: 150 }, { text: 'Title', dataField: 'Title', width: 300 }, { text: 'Birth Date', dataField: 'BirthDate', cellsFormat: 'd', width: 120 }, { text: 'Hire Date', dataField: 'HireDate', cellsFormat: 'd', width: 120 }, { text: 'Phone', dataField: 'Phone', cellsFormat: 'd', width: 120 } ] } }, beforeCreate: function () { // Add here any data where you want to transform before components be rendered this.source = { dataType: "csv", dataFields: [ { name: 'EmployeeKey', type: 'number' }, { name: 'ParentEmployeeKey', type: 'number' }, { name: 'FirstName', type: 'string' }, { name: 'LastName', type: 'string' }, { name: 'Title', type: 'string' }, { name: 'HireDate', type: 'date' }, { name: 'BirthDate', type: 'date' }, { name: 'Phone', type: 'string' }, { name: 'Gender', type: 'string' }, { name: 'DepartmentName', type: 'string' } ], hierarchy: { keyDataField: { name: 'EmployeeKey' }, parentDataField: { name: 'ParentEmployeeKey' } }, id: 'EmployeeKey', url: 'sampledata/employeesadv.csv' }; }, methods: { // Add here all used callbacks and/or events onRowSelect: function () { // Do something... let selection = this.refs.treegrid.getSelection(); alert(selected); } } }</script> <style></style>
TreeGrid Example
<template> <jqxTreeGrid ref="treeGrid" :width="width" :source="dataAdapter" :altRows="true" :enableHover="false" :editable="true" :selectionMode="'none'" :columns="columns" :ready="ready"> </jqxTreeGrid></template> <script> import JqxTreeGrid from 'jqwidgets-scripts/jqwidgets-vue/vue_jqxtreegrid.vue'; export default { components: { JqxTreeGrid }, data: function () { return { width: 800, dataAdapter: new jqx.dataAdapter(this.source), columns: [ { text: 'ID', dataField: 'id', width: 150 }, { text: 'Name', dataField: 'name', width: 270 }, { text: 'Location', dataField: 'location' }, { text: 'Budget', align: 'right', cellsRenderer: this.cellsRendererFunction, cellsAlign: 'right', cellClassName: 'conditionalFormatting', cellsFormat: 'c2', dataField: 'budget', width: 200 } ] } }, beforeCreate: function () { const data = [{ 'id': '1', 'name': 'Corporate Headquarters', 'budget': '1230000', 'location': 'Las Vegas', 'children': [ { 'id': '2', 'name': 'Finance Division', 'budget': '423000', 'location': 'San Antonio', 'children': [ { 'id': '3', 'name': 'Accounting Department', 'budget': '113000', 'location': 'San Antonio' }, { 'id': '4', 'name': 'Investment Department', 'budget': '310000', 'location': 'San Antonio', 'children': [ { 'id': '5', 'name': 'Banking Office', 'budget': '240000', 'location': 'San Antonio' }, { 'id': '6', 'name': 'Bonds Office', 'budget': '70000', 'location': 'San Antonio' }, ] } ] }, { 'id': '7', 'name': 'Operations Division', 'budget': '600000', 'location': 'Miami', 'children': [ { 'id': '8', 'name': 'Manufacturing Department', 'budget': '300000', 'location': 'Miami' }, { 'id': '9', 'name': 'Public Relations Department', 'budget': '200000', 'location': 'Miami' }, { 'id': '10', 'name': 'Sales Department', 'budget': '100000', 'location': 'Miami' } ] }, { 'id': '11', 'name': 'Research Division', 'budget': '200000', 'location': 'Boston' } ] }]; this.source = { dataType: 'json', dataFields: [ { name: 'name', type: 'string' }, { name: 'budget', type: 'number' }, { name: 'id', type: 'number' }, { name: 'children', type: 'array' }, { name: 'location', type: 'string' } ], hierarchy: { root: 'children' }, localData: data, id: 'id' }; }, methods: { cellsRendererFunction: function (row, dataField, cellValueInternal, rowData, cellText) { let cellValue = rowData[dataField]; if (cellValue < 400000) { return "<span style='color: #e91b1b;'>" + cellText + "</span><span class='red-arrow-down'></span>"; } return "<span style='color: #028b2b;'>" + cellText + "</span><span class='green-arrow-up'></span>"; }, ready: function () { this.$refs.treeGrid.expandRow(1); this.$refs.treeGrid.expandRow(2); this.$refs.treeGrid.expandRow(7); } } }</script> <style> html, body { width: 100vw; height: 100vh; padding: 0; margin: 0; } .green-arrow-up { position: relative; top: -10px; width: 0; height: 0; border-left: 5px solid transparent; border-right: 5px solid transparent; border-bottom: 5px solid #028b2b; } .red-arrow-down { position: relative; top: 10px; width: 0; height: 0; border-left: 5px solid transparent; border-right: 5px solid transparent; border-top: 5px solid #e91b1b; } .conditionalFormatting { background: white !important; }</style>