Import
-import Tree from 'primevue/tree';
+import TreeTable from 'primevue/treetable';
Getting Started
- Tree component requires an array of TreeNode objects as its value.
+ Tree component requires an array of TreeNode objects as its value and columns defined with Column component.
- TreeNode API
+ TreeNode API utilized by the TreeTable
@@ -28,30 +28,12 @@ import Tree from 'primevue/tree';
null |
Mandatory unique key of the node. |
-
- label |
- string |
- null |
- Label of the node. |
-
data |
any |
null |
Data represented by the node. |
-
- type |
- string |
- null |
- Type of the node to match a template. |
-
-
- icon |
- string |
- null |
- Icon of the node to display next to content. |
-
children |
TreeNode[] |
@@ -70,12 +52,6 @@ import Tree from 'primevue/tree';
null |
Style class of the node. |
-
- selectable |
- boolean |
- null |
- Whether the node is selectable when selection mode is enabled. |
-
leaf |
boolean |
@@ -86,10 +62,14 @@ import Tree from 'primevue/tree';
- Example below loads the tree nodes from a remote datasource via a service called NodeService.
+ Example below loads the nodes from a remote datasource via a service called NodeService.
-<Tree :value="nodes"></Tree>
+<TreeTable :value="nodes">
+ <Column field="name" header="Name" :expander="true"></Column>
+ <Column field="size" header="Size"></Column>
+ <Column field="type" header="Type"></Column>
+</TreeTable>
@@ -107,7 +87,7 @@ export default {
this.nodeService = new NodeService();
},
mounted() {
- this.nodeService.getTreeNodes().then(data => this.nodes = data);
+ this.nodeService.getTreeTableNodes().then(data => this.nodes = data);
}
}
@@ -117,70 +97,516 @@ import axios from 'axios';
export default class NodeService {
- getTreeNodes() {
- return axios.get('demo/data/treenodes.json').then(res => res.data.root);
+ getTreeTableNodes() {
+ return axios.get('demo/data/treetablenodes.json').then(res => res.data.root);
}
}
The json response sample would be as following.
+
+
{
- "root": [
+ "root":
+ [
{
"key": "0",
- "label": "Documents",
- "data": "Documents Folder",
- "icon": "pi pi-fw pi-inbox",
- "children": [{
- "key": "0-0",
- "label": "Work",
- "data": "Work Folder",
- "icon": "pi pi-fw pi-cog",
- "children": [{ "key": "0-0-0", "label": "Expenses.doc", "icon": "pi pi-fw pi-file", "data": "Expenses Document" }, { "key": "0-0-1", "label": "Resume.doc", "icon": "pi pi-fw pi-file", "data": "Resume Document" }]
+ "data":{
+ "name":"Applications",
+ "size":"100kb",
+ "type":"Folder"
},
- {
- "key": "0-1",
- "label": "Home",
- "data": "Home Folder",
- "icon": "pi pi-fw pi-home",
- "children": [{ "key": "0-1-0", "label": "Invoices.txt", "icon": "pi pi-fw pi-file", "data": "Invoices for this month" }]
- }]
+ "children":[
+ {
+ "key": "0-0",
+ "data":{
+ "name":"Vue",
+ "size":"25kb",
+ "type":"Folder"
+ },
+ "children":[
+ {
+ "key": "0-0-0",
+ "data":{
+ "name":"Vue.app",
+ "size":"10kb",
+ "type":"Application"
+ }
+ },
+ {
+ "key": "0-0-1",
+ "data":{
+ "name":"native.app",
+ "size":"10kb",
+ "type":"Application"
+ }
+ },
+ {
+ "key": "0-0-2",
+ "data":{
+ "name":"mobile.app",
+ "size":"5kb",
+ "type":"Application"
+ }
+ }
+ ]
+ },
+ {
+ "key": "0-1",
+ "data":{
+ "name":"editor.app",
+ "size":"25kb",
+ "type":"Application"
+ }
+ },
+ {
+ "key": "0-2",
+ "data":{
+ "name":"settings.app",
+ "size":"50kb",
+ "type":"Application"
+ }
+ }
+ ]
},
- {
+ {
"key": "1",
- "label": "Events",
- "data": "Events Folder",
- "icon": "pi pi-fw pi-calendar",
- "children": [
- { "key": "1-0", "label": "Meeting", "icon": "pi pi-fw pi-calendar-plus", "data": "Meeting" },
- { "key": "1-1", "label": "Product Launch", "icon": "pi pi-fw pi-calendar-plus", "data": "Product Launch" },
- { "key": "1-2", "label": "Report Review", "icon": "pi pi-fw pi-calendar-plus", "data": "Report Review" }]
- },
- {
- "key": "2",
- "label": "Movies",
- "data": "Movies Folder",
- "icon": "pi pi-fw pi-star",
- "children": [{
- "key": "2-0",
- "icon": "pi pi-fw pi-star",
- "label": "Al Pacino",
- "data": "Pacino Movies",
- "children": [{ "key": "2-0-0", "label": "Scarface", "icon": "pi pi-fw pi-video", "data": "Scarface Movie" }, { "key": "2-0-1", "label": "Serpico", "icon": "pi pi-fw pi-video", "data": "Serpico Movie" }]
+ "data":{
+ "name":"Cloud",
+ "size":"20kb",
+ "type":"Folder"
},
- {
- "key": "2-1",
- "label": "Robert De Niro",
- "icon": "pi pi-fw pi-star",
- "data": "De Niro Movies",
- "children": [{ "key": "2-1-0", "label": "Goodfellas", "icon": "pi pi-fw pi-video", "data": "Goodfellas Movie" }, { "key": "2-1-1", "label": "Untouchables", "icon": "pi pi-fw pi-video", "data": "Untouchables Movie" }]
- }]
+ "children":[
+ {
+ "key": "1-0",
+ "data":{
+ "name":"backup-1.zip",
+ "size":"10kb",
+ "type":"Zip"
+ }
+ },
+ {
+ "key": "1-1",
+ "data":{
+ "name":"backup-2.zip",
+ "size":"10kb",
+ "type":"Zip"
+ }
+ }
+ ]
+ },
+ {
+ "key": "2",
+ "data": {
+ "name":"Desktop",
+ "size":"150kb",
+ "type":"Folder"
+ },
+ "children":[
+ {
+ "key": "2-0",
+ "data":{
+ "name":"note-meeting.txt",
+ "size":"50kb",
+ "type":"Text"
+ }
+ },
+ {
+ "key": "2-1",
+ "data":{
+ "name":"note-todo.txt",
+ "size":"100kb",
+ "type":"Text"
+ }
+ }
+ ]
+ },
+ {
+ "key": "3",
+ "data":{
+ "name":"Documents",
+ "size":"75kb",
+ "type":"Folder"
+ },
+ "children":[
+ {
+ "key": "3-0",
+ "data":{
+ "name":"Work",
+ "size":"55kb",
+ "type":"Folder"
+ },
+ "children":[
+ {
+ "key": "3-0-0",
+ "data":{
+ "name":"Expenses.doc",
+ "size":"30kb",
+ "type":"Document"
+ }
+ },
+ {
+ "key": "3-0-1",
+ "data":{
+ "name":"Resume.doc",
+ "size":"25kb",
+ "type":"Resume"
+ }
+ }
+ ]
+ },
+ {
+ "key": "3-1",
+ "data":{
+ "name":"Home",
+ "size":"20kb",
+ "type":"Folder"
+ },
+ "children":[
+ {
+ "key": "3-1-0",
+ "data":{
+ "name":"Invoices",
+ "size":"20kb",
+ "type":"Text"
+ }
+ }
+ ]
+ }
+ ]
+ },
+ {
+ "key": "4",
+ "data": {
+ "name":"Downloads",
+ "size":"25kb",
+ "type":"Folder"
+ },
+ "children":[
+ {
+ "key": "4-0",
+ "data": {
+ "name":"Spanish",
+ "size":"10kb",
+ "type":"Folder"
+ },
+ "children":[
+ {
+ "key": "4-0-0",
+ "data":{
+ "name":"tutorial-a1.txt",
+ "size":"5kb",
+ "type":"Text"
+ }
+ },
+ {
+ "key": "4-0-1",
+ "data":{
+ "name":"tutorial-a2.txt",
+ "size":"5kb",
+ "type":"Text"
+ }
+ }
+ ]
+ },
+ {
+ "key": "4-1",
+ "data":{
+ "name":"Travel",
+ "size":"15kb",
+ "type":"Text"
+ },
+ "children":[
+ {
+ "key": "4-1-0",
+ "data":{
+ "name":"Hotel.pdf",
+ "size":"10kb",
+ "type":"PDF"
+ }
+ },
+ {
+ "key": "4-1-1",
+ "data":{
+ "name":"Flight.pdf",
+ "size":"5kb",
+ "type":"PDF"
+ }
+ }
+ ]
+ }
+ ]
+ },
+ {
+ "key": "5",
+ "data": {
+ "name":"Main",
+ "size":"50kb",
+ "type":"Folder"
+ },
+ "children":[
+ {
+ "key": "5-0",
+ "data":{
+ "name":"bin",
+ "size":"50kb",
+ "type":"Link"
+ }
+ },
+ {
+ "key": "5-1",
+ "data":{
+ "name":"etc",
+ "size":"100kb",
+ "type":"Link"
+ }
+ },
+ {
+ "key": "5-2",
+ "data":{
+ "name":"var",
+ "size":"100kb",
+ "type":"Link"
+ }
+ }
+ ]
+ },
+ {
+ "key": "6",
+ "data":{
+ "name":"Other",
+ "size":"5kb",
+ "type":"Folder"
+ },
+ "children":[
+ {
+ "key": "6-0",
+ "data":{
+ "name":"todo.txt",
+ "size":"3kb",
+ "type":"Text"
+ }
+ },
+ {
+ "key": "6-1",
+ "data":{
+ "name":"logo.png",
+ "size":"2kb",
+ "type":"Picture"
+ }
+ }
+ ]
+ },
+ {
+ "key": "7",
+ "data":{
+ "name":"Pictures",
+ "size":"150kb",
+ "type":"Folder"
+ },
+ "children":[
+ {
+ "key": "7-0",
+ "data":{
+ "name":"barcelona.jpg",
+ "size":"90kb",
+ "type":"Picture"
+ }
+ },
+ {
+ "key": "7-1",
+ "data":{
+ "name":"primeng.png",
+ "size":"30kb",
+ "type":"Picture"
+ }
+ },
+ {
+ "key": "7-2",
+ "data":{
+ "name":"prime.jpg",
+ "size":"30kb",
+ "type":"Picture"
+ }
+ }
+ ]
+ },
+ {
+ "key": "8",
+ "data":{
+ "name":"Videos",
+ "size":"1500kb",
+ "type":"Folder"
+ },
+ "children":[
+ {
+ "key": "8-0",
+ "data":{
+ "name":"primefaces.mkv",
+ "size":"1000kb",
+ "type":"Video"
+ }
+ },
+ {
+ "key": "8-1",
+ "data":{
+ "name":"intro.avi",
+ "size":"500kb",
+ "type":"Video"
+ }
+ }
+ ]
}
]
}
+
+
+ Dynamic Columns
+ Column components can be dynamically generated using a v-for as well.
+
+
+
+<TreeTable :value="nodes">
+ <Column v-for="col of columns" :key="col.field"
+ :field="col.field" :header="col.header" :expander="col.expander"></Column>
+</TreeTable>
+
+
+
+
+import NodeService from '../../service/NodeService';
+
+export default {
+ data() {
+ return {
+ nodes: null,
+ columns: null
+ }
+ },
+ nodeService: null,
+ created() {
+ this.nodeService = new NodeService();
+
+ this.columns = [
+ {field: 'name', header: 'Vin', expander: true},
+ {field: 'size', header: 'Size'},
+ {field: 'type', header: 'Type'}
+ ];
+ },
+ mounted() {
+ this.nodeService.getTreeTableNodes().then(data => this.nodes = data);
+ }
+}
+
+
+ Column Component Properties utilized by the TreeTable
+
+
+
+
+ Name |
+ Type |
+ Default |
+ Description |
+
+
+
+
+ columnKey |
+ any |
+ null |
+ Identifier of a column if field property is not defined. |
+
+
+ expander |
+ boolean |
+ false |
+ Whether the column would display a toggle icon. |
+
+
+ field |
+ string |
+ null |
+ Property of a row data. |
+
+
+ sortField |
+ string |
+ null |
+ Property of a row data used for sorting, defaults to field. |
+
+
+ sortable |
+ any |
+ false |
+ Defines if a column is sortable. |
+
+
+ header |
+ any |
+ null |
+ Header content of the column. |
+
+
+ footer |
+ any |
+ null |
+ Footer content of the column. |
+
+
+ headerStyle |
+ object |
+ null |
+ Inline style of the column. |
+
+
+ headerClass |
+ string |
+ null |
+ Style class of the column. |
+
+
+ bodyStyle |
+ object |
+ null |
+ Inline style of the column. |
+
+
+ bodyClass |
+ string |
+ null |
+ Style class of the column. |
+
+
+ footerStyle |
+ object |
+ null |
+ Inline style of the column. |
+
+
+ footerClass |
+ string |
+ null |
+ Style class of the column. |
+
+
+ filterMatchMode |
+ string |
+ null |
+ Defines filterMatchMode; "startsWith", "contains", "endsWidth", "equals", "notEquals", "in" and "custom". |
+
+
+ excludeGlobalFilter |
+ boolean |
+ false |
+ Whether to exclude from global filtering or not. |
+
+
+
+
Programmatic Control
Tree state can be controlled programmatically with the expandedKeys property that defines the keys
@@ -190,11 +616,15 @@ export default class NodeService {
Example below expands and collapses all nodes with buttons.
-<div>
+<div style="margin-bottom: 1em">
<Button type="button" icon="pi pi-plus" label="Expand All" @click="expandAll" />
<Button type="button" icon="pi pi-minus" label="Collapse All" @click="collapseAll" />
</div>
-<Tree :value="nodes" :expandedKeys="expandedKeys"></Tree>
+<TreeTable :value="nodes" :expandedKeys="expandedKeys">
+ <Column field="name" header="Name" :expander="true"></Column>
+ <Column field="size" header="Size"></Column>
+ <Column field="type" header="Type"></Column>
+</TreeTable>
@@ -213,7 +643,7 @@ export default {
this.nodeService = new NodeService();
},
mounted() {
- this.nodeService.getTreeNodes().then(data => this.nodes = data);
+ this.nodeService.getTreeTableNodes().then(data => this.nodes = data);
},
methods: {
expandAll() {
@@ -227,8 +657,9 @@ export default {
this.expandedKeys = {};
},
expandNode(node) {
- this.expandedKeys[node.key] = true;
if (node.children && node.children.length) {
+ this.expandedKeys[node.key] = true;
+
for (let child of node.children) {
this.expandNode(child);
}
@@ -254,13 +685,211 @@ export default {
this.nodeService = new NodeService();
},
mounted() {
- this.nodeService.getTreeNodes().then(data => {
+ this.nodeService.getTreeTableNodes().then(data => {
this.nodes = data;
this.expandedKeys[this.nodes[0].key] = true;
this.expandedKeys[this.nodes[1].key] = true;
});
}
}
+
+
+ Templating
+ Field data of a corresponding node is displayed as the cell content by default, this can be customized using a body template where current node data and column properties are passed via the slot props.
+ On the other hand, header and footer sections of a column can either be defined with the properties or the templates. Similarly TreeTable itself also provides header and footer properties along with the templates for the main header and footer of the table.
+
+
+
+<TreeTable :value="nodes">
+ <template #header>
+ FileSystem
+ </template>
+ <Column field="name" header="Name" :expander="true"></Column>
+ <Column field="size" header="Size"></Column>
+ <Column field="type" header="Type"></Column>
+ <Column headerStyle="width: 8em" bodyStyle="text-align: center">
+ <template #header>
+ <Button type="button" icon="pi pi-cog"></Button>
+ </template>
+ <template #body="slotProps">
+ <Button type="button" icon="pi pi-search" class="p-button-success" style="margin-right: .5em"></Button>
+ <Button type="button" icon="pi pi-pencil" class="p-button-warning"></Button>
+ </template>
+ </Column>
+ <template #footer>
+ <div style="text-align:left">
+ <Button icon="pi pi-refresh" />
+ </div>
+ </template>
+</TreeTable>
+
+
+
+ Pagination
+ Pagination is enabled by setting paginator property to true and defining the rows property defines the number of rows per page.
+ See the Paginator for the available customization options such as paginator templates, page links,
+ rows per page options and more which can be passed through the TreeTable.
+
+
+
+<TreeTable :value="nodes" :paginator="true" :rows="10">
+ <Column field="name" header="Name" :expander="true"></Column>
+ <Column field="size" header="Size"></Column>
+ <Column field="type" header="Type"></Column>
+</TreeTable>
+
+
+
+ paginatorLeft and paginatorLeft templates are available to specify custom content at the left and right side.
+
+
+<TreeTable :value="nodes" :paginator="true" :rows="10">
+ <Column field="name" header="Name" :expander="true"></Column>
+ <Column field="size" header="Size"></Column>
+ <Column field="type" header="Type"></Column>
+ <template #paginatorLeft>
+ <Button type="button" icon="pi pi-refresh" />
+ </template>
+ <template #paginatorRight>
+ <Button type="button" icon="pi pi-cloud" />
+ </template>
+</TreeTable>
+
+
+
+ Paginator can also be programmed programmatically using a binding to the first property that defines the index of the
+ first element to display. For example setting first to zero will reset the paginator to the very first page. This property
+ also supports "sync" keyword in case you'd like your binding to be updated whenever the user changes the page.
+
+
+<TreeTable :value="nodes" :paginator="true" :rows="10" :first="firstRecordIndex">
+ <Column field="name" header="Name" :expander="true"></Column>
+ <Column field="size" header="Size"></Column>
+ <Column field="type" header="Type"></Column>
+</TreeTable>
+
+
+
+ Sorting
+ Enabling sortable property at column component would be enough to make a column sortable.
+ The property to use when sorting is the field by default and can be customized using the sortField.
+
+
+
+<TreeTable :value="nodes" sortMode="single">
+ <Column field="name" header="Name" :expander="true" :sortable="true"></Column>
+ <Column field="size" header="Size" :sortable="true"></Column>
+ <Column field="type" header="Type" :sortable="true"></Column>
+</TreeTable>
+
+
+
+ By default sorting is executed on the clicked column only. To enable multiple field sorting, set sortMode property to "multiple" and use metakey when clicking on another column.
+
+
+<TreeTable :value="nodes" sortMode="multiple">
+ <Column field="name" header="Name" :expander="true" :sortable="true"></Column>
+ <Column field="size" header="Size" :sortable="true"></Column>
+ <Column field="type" header="Type" :sortable="true"></Column>
+</TreeTable>
+
+
+
+ In case you'd like to display the table as sorted per a single column by default on mount or programmatically apply sort, use sortField and sortOrder properties. These
+ two properties also support the "sync" keyword to get updated when the user applies sort a column.
+
+
+<TreeTable :value="nodes" sortField="size" :sortOrder="1"">
+ <Column field="name" header="Name" :expander="true" :sortable="true"></Column>
+ <Column field="size" header="Size" :sortable="true"></Column>
+ <Column field="type" header="Type" :sortable="true"></Column>
+</TreeTable>
+
+<TreeTable :value="nodes" sortMode="single" sortField="dynamicSortField" :sortOrder="dynamicSortOrder">
+ <Column field="name" header="Name" :expander="true" :sortable="true"></Column>
+ <Column field="size" header="Size" :sortable="true"></Column>
+ <Column field="type" header="Type" :sortable="true"></Column>
+</TreeTable>
+
+
+
+ In multiple mode, use the multiSortMeta property and bind an array of SortMeta objects instead.
+
+
+<TreeTable :value="nodes" sortMode="multiple" :multiSortMeta="multiSortMeta">
+ <Column field="name" header="Name" :expander="true" :sortable="true"></Column>
+ <Column field="size" header="Size" :sortable="true"></Column>
+ <Column field="type" header="Type" :sortable="true"></Column>
+</TreeTable>
+
+
+
+
+data() {
+ return {
+ multiSortMeta: [
+ {field: 'year', order: 1},
+ {field: 'brand', order: -1}
+ ]
+ }
+}
+
+
+ Filtering
+ Filtering is enabled by defining a filter template per column to populate the filters property of the TreeTable. The filters
+ property should be an key-value object where keys are the field name and the value is the filter value. The filter template receives the column properties
+ via the slotProps and accepts any form element as the filter element. Default match mode is "startsWith" and this can be configured per column using the filterMatchMode property that also accepts
+ "contains", "endsWith", "equals", "notEquals" and "in" as available modes.
+ Optionally a global filter is available to search against all the fields, in this case the special global keyword should be the property to be populated.
+
+ In addition filterMode specifies the filtering strategy. In lenient mode when the query matches a node, children of the node are not searched further as all descendants of the node are included. On the other hand,
+ in strict mode when the query matches a node, filtering continues on all descendants.
+
+
+<TreeTable :value="nodes" :filters="filters" filterMode="lenient">
+ <template #header>
+ <div style="text-align: right">
+ <i class="pi pi-search" style="margin: 4px 4px 0px 0px;"></i>
+ <InputText v-model="filters['global']" placeholder="Global Search" size="50" />
+ </div>
+ </template>
+ <Column field="name" header="Name" :expander="true">
+ <template #filter>
+ <InputText type="text" v-model="filters['name']" class="p-column-filter" />
+ </template>
+ </Column>
+ <Column field="size" header="Size">
+ <template #filter>
+ <InputText type="text" v-model="filters['size']" class="p-column-filter" />
+ </template>
+ </Column>
+ <Column field="type" header="Type">
+ <template #filter>
+ <InputText type="text" v-model="filters['type']" class="p-column-filter" />
+ </template>
+ </Column>
+</TreeTable>
+
+
+
+
+import NodeService from '../../service/NodeService';
+
+export default {
+ data() {
+ return {
+ filters: {},
+ nodes: null
+ }
+ },
+ nodeService: null,
+ created() {
+ this.nodeService = new NodeService();
+ },
+ mounted() {
+ this.nodeService.getTreeTableNodes().then(data => this.nodes = data);
+ }
+}
Selection
@@ -272,21 +901,33 @@ export default {
in "checkbox" mode, instead of a boolean, value should be an object that has "checked" and "partialChecked" properties to represent the checked state of a node.
-<h3>Single Selection</h3>
-<Tree :value="nodes" selectionMode="single" :selectionKeys.sync="selectedKey1"></Tree>
-
-<h3>Multiple Selection with MetaKey</h3>
-<Tree :value="nodes" selectionMode="multiple" :selectionKeys.sync="selectedKeys1"></Tree>
+<TreeTable :value="nodes" selectionMode="multiple" :selectionKeys.sync="selectedKeys1">
+ <Column field="name" header="Name" :expander="true"></Column>
+ <Column field="size" header="Size"></Column>
+ <Column field="type" header="Type"></Column>
+</TreeTable>
<h3>Multiple Selection without MetaKey</h3>
-<Tree :value="nodes" selectionMode="multiple" :selectionKeys.sync="selectedKeys2" :metaKeySelection="false"></Tree>
+<TreeTable :value="nodes" selectionMode="multiple" :selectionKeys.sync="selectedKeys2" :metaKeySelection="false">
+ <Column field="name" header="Name" :expander="true"></Column>
+ <Column field="size" header="Size"></Column>
+ <Column field="type" header="Type"></Column>
+</TreeTable>
<h3>Checkbox Selection</h3>
-<Tree :value="nodes" selectionMode="checkbox" :selectionKeys.sync="selectedKeys3"></Tree>
+<TreeTable :value="nodes" selectionMode="checkbox" :selectionKeys.sync="selectedKeys3">
+ <Column field="name" header="Name" :expander="true"></Column>
+ <Column field="size" header="Size"></Column>
+ <Column field="type" header="Type"></Column>
+</TreeTable>
<h3>Events</h3>
-<Tree :value="nodes" selectionMode="single" :selectionKeys.sync="selectedKey2" :metaKeySelection="false"
- @node-select="onNodeSelect" @node-unselect="onNodeUnselect"></Tree>
+<TreeTable :value="nodes" selectionMode="single" :selectionKeys.sync="selectedKey2"
+ @node-select="onNodeSelect" @node-unselect="onNodeUnselect">
+ <Column field="name" header="Name" :expander="true"></Column>
+ <Column field="size" header="Size"></Column>
+ <Column field="type" header="Type"></Column>
+</TreeTable>
@@ -309,14 +950,14 @@ export default {
this.nodeService = new NodeService();
},
mounted() {
- this.nodeService.getTreeNodes().then(data => this.nodes = data);
+ this.nodeService.getTreeTableNodes().then(data => this.nodes = data);
},
methods: {
onNodeSelect(node) {
- this.$toast.add({severity:'success', summary: 'Node Selected', detail: node.label, life: 3000});
+ this.$toast.add({severity:'success', summary: 'Node Selected', detail: node.data.name, life: 3000});
},
onNodeUnselect(node) {
- this.$toast.add({severity:'success', summary: 'Node Unselected', detail: node.label, life: 3000});
+ this.$toast.add({severity:'success', summary: 'Node Unselected', detail: node.data.name, life: 3000});
}
}
}
@@ -342,7 +983,7 @@ export default {
this.nodeService = new NodeService();
},
mounted() {
- this.nodeService.getTreeNodes().then(data => {
+ this.nodeService.getTreeTableNodes().then(data => {
this.nodes = data;
//single preselection
@@ -360,14 +1001,168 @@ export default {
Lazy
- Lazy Loading is handy to deal with huge datasets. Idea is instead of loading the whole tree, load child nodes on demand
- using expand expand. The important part is setting leaf to true on a node instance so that even without children,
+
Lazy Loading is handy to deal with huge datasets. Idea is instead of loading the whole tree, load nodes on demand when necessary.
+ The important part when lazy loading nodes is setting leaf to true on a node instance so that even without children,
tree would render an expand icon. Example below uses an in memory collection to mimic a lazy loading scenario with timeouts.
+ In addition lazy loading of root level nodes is implemented by handling pagination and sorting using page and sort events by making a remote query using the information
+ passed to the events such as first offset, number of rows and sort field for ordering. Filtering is handled differently as filter elements are defined using templates, use
+ the event you prefer on your form elements such as input, change, blur to make a remote call by passing the filters property to update the displayed data. Note that,
+ in lazy filtering, totalRecords should also be updated to align the data with the paginator.
.
+
+ Example below uses an in memory collection to mimic a lazy loading scenario with timeouts.
+
-<Tree :value="nodes" @node-expand="onNodeExpand" :loading="loading"></Tree>
+<TreeTable :value="nodes" :lazy="true" :paginator="true" :rows="rows" :loading="loading"
+ @node-expand="onExpand" @page="onPage" :totalRecords="totalRecords">
+ <Column field="name" header="Name" :expander="true"></Column>
+ <Column field="size" header="Size"></Column>
+ <Column field="type" header="Type"></Column>
+</TreeTable>
+
+
+
+
+export default {
+ data() {
+ return {
+ nodes: null,
+ rows: 10,
+ loading: false,
+ totalRecords: 0
+ }
+ },
+ mounted() {
+ this.loading = true;
+
+ setTimeout(() => {
+ this.loading = false;
+ this.nodes = this.loadNodes(0, this.rows);
+ this.totalRecords = 1000;
+ }, 1000);
+ },
+ methods: {
+ onExpand(node) {
+ if (!node.children) {
+ this.loading = true;
+
+ setTimeout(() => {
+ let lazyNode = {...node};
+
+ lazyNode.children = [
+ {
+ data: {
+ name: lazyNode.data.name + ' - 0',
+ size: Math.floor(Math.random() * 1000) + 1 + 'kb',
+ type: 'File'
+ },
+ },
+ {
+ data: {
+ name: lazyNode.data.name + ' - 1',
+ size: Math.floor(Math.random() * 1000) + 1 + 'kb',
+ type: 'File'
+ }
+ }
+ ];
+
+ let nodes = this.nodes.map(n => {
+ if (n.key === node.key) {
+ n = lazyNode;
+ }
+
+ return n;
+ });
+
+ this.loading = false;
+ this.nodes = nodes;
+ }, 250);
+ }
+ },
+ onPage(event) {
+ this.loading = true;
+
+ //imitate delay of a backend call
+ setTimeout(() => {
+ this.loading = false;
+ this.nodes = this.loadNodes(event.first, this.rows);
+ }, 1000);
+ },
+ loadNodes(first, rows) {
+ let nodes = [];
+
+ for(let i = 0; i < rows; i++) {
+ let node = {
+ key: (first + i),
+ data: {
+ name: 'Item ' + (first + i),
+ size: Math.floor(Math.random() * 1000) + 1 + 'kb',
+ type: 'Type ' + (first + i)
+ },
+ leaf: false
+ };
+
+ nodes.push(node);
+ }
+
+ return nodes;
+ }
+ }
+}
+
+
+ Column Resize
+ Columns can be resized using drag drop by setting the resizableColumns to true. There are two resize modes; "fit" and "expand". Fit is the default one and the overall table width does not change when a column is resized.
+ In "expand" mode, table width also changes along with the column width. column-resize-end is a callback that passes the resized column header and delta change as a parameter.
+
+
+<h3>Fit Mode</h3>
+<TreeTable :value="nodes" :resizableColumns="true" columnResizeMode="fit">
+ <Column field="name" header="Name" :expander="true"></Column>
+ <Column field="size" header="Size"></Column>
+ <Column field="type" header="Type"></Column>
+</TreeTable>
+
+<h3>Expand Mdoe</h3>
+<TreeTable :value="nodes" :resizableColumns="true" columnResizeMode="expand">
+ <Column field="name" header="Name" :expander="true"></Column>
+ <Column field="size" header="Size"></Column>
+ <Column field="type" header="Type"></Column>
+</TreeTable>
+
+
+
+ It is important to note that when you need to change column widths, since table width is 100%, giving fixed pixel widths does not work well as browsers scale them, instead give percentage widths.
+
+
+<TreeTable :value="nodes" :resizableColumns="true">
+ <Column field="name" header="Name" :expander="true"></Column headerStyle="width: 20%">
+ <Column field="size" header="Size" headerStyle="width: 40%"></Column>
+ <Column field="type" header="Type headerStyle="width: 40%""></Column>
+</TreeTable>
+
+
+
+ Responsive
+ TreeTable display can be optimized according to screen sizes, this example demonstrates a demo where columns are stacked on small screens.
+
+
+<TreeTable :value="nodes" class="p-treetable-responsive">
+ <template #header>
+ Responsive
+ </template>
+ <Column field="name" header="Name" :expander="true">
+ <template #body="slotProps">
+ {{slotProps.node.data.name}}
+ <span class="sm-visible">{{slotProps.node.data.size}}</span>
+ <span class="sm-visible">{{slotProps.node.data.type}}</span>
+ </template>
+ </Column>
+ <Column field="size" header="Size" headerClass="sm-invisible" bodyClass="sm-invisible"></Column>
+ <Column field="type" header="Type" headerClass="sm-invisible" bodyClass="sm-invisible"></Column>
+</TreeTable>
@@ -377,7 +1172,6 @@ import NodeService from '../../service/NodeService';
export default {
data() {
return {
- loading: false,
nodes: null
}
},
@@ -386,164 +1180,32 @@ export default {
this.nodeService = new NodeService();
},
mounted() {
- this.loading = true;
-
- setTimeout(() => {
- this.nodes = this.initateNodes();
- this.loading = false;
- }, 2000);
- },
- methods: {
- onNodeExpand(node) {
- if (!node.children) {
- this.loading = true;
-
- setTimeout(() => {
- let _node = {...node};
- _node.children = [];
-
- for (let i = 0; i < 3; i++) {
- _node.children.push({
- key: node.key + '-' + i,
- label: 'Lazy ' + node.label + '-' + i
- });
- }
-
- let _nodes = {...this.nodes}
- _nodes[parseInt(node.key, 10)] = _node;
-
- this.nodes = _nodes;
- this.loading = false;
- }, 500);
- }
- },
- initateNodes() {
- return [{
- key: '0',
- label: 'Node 0',
- leaf: false
- },
- {
- key: '1',
- label: 'Node 1',
- leaf: false
- },
- {
- key: '2',
- label: 'Node 2',
- leaf: false
- }];
- }
+ this.nodeService.getTreeTableNodes().then(data => this.nodes = data);
}
}
- Templating
- The type property of a TreeNode is used to map a template to a node to create the node label. If it is undefined and no default template is available,
- label of the node is used.
-
-
-<Tree :value="nodes">
- <template #default="slotProps">
- <b>{{slotProps.node.label}}</b>
- </template>
- <template #url="slotProps">
- <a :href="slotProps.node.data">{{slotProps.node.label}}</a>
- </template>
-</Tree>
-
-
-
-
-export default {
- data() {
- return {
- nodes: [
- {
- key: '0',
- label: 'Introduction',
- children: [
- {key: '0-0', label: 'What is Vue.js?', data:'https://vuejs.org/v2/guide/#What-is-Vue-js', type: 'url'},
- {key: '0-1', label: 'Getting Started', data: 'https://vuejs.org/v2/guide/#Getting-Started', type: 'url'},
- {key: '0-2', label: 'Declarative Rendering', data:'https://vuejs.org/v2/guide/#Declarative-Rendering', type: 'url'},
- {key: '0-3', label: 'Conditionals and Loops', data: 'https://vuejs.org/v2/guide/#Conditionals-and-Loops', type: 'url'}
- ]
- },
- {
- key: '1',
- label: 'Components In-Depth',
- children: [
- {key: '1-0', label: 'Component Registration', data: 'https://vuejs.org/v2/guide/components-registration.html', type: 'url'},
- {key: '1-1', llabel: 'Props', data: 'https://vuejs.org/v2/guide/components-props.html', type: 'url'},
- {key: '1-2', llabel: 'Custom Events', data: 'https://vuejs.org/v2/guide/components-custom-events.html', type: 'url'},
- {key: '1-3', llabel: 'Slots', data: 'https://vuejs.org/v2/guide/components-slots.html', type: 'url'}
- ]
- }
- ]
- }
- }
+
+.sm-visible {
+ display: none;
}
-
- Filtering
- Filtering is enabled by setting the filter property to true, by default label property of a node
- is used to compare against the value in the text field, in order to customize which field(s) should be used during search, define the filterBy property as a comma separated list.
-
- In addition filterMode specifies the filtering strategy. In lenient mode when the query matches a node, children of the node are not searched further as all descendants of the node are included. On the other hand,
- in strict mode when the query matches a node, filtering continues on all descendants.
-
-
-
-<h3>Lenient Filter</h3>
-<Tree :value="nodes" :filter="true" filterMode="lenient"></Tree>
-
-<h3>Strict Filter</h3>
-<Tree :value="nodes" :filter="true" filterMode="strict"></Tree>
-
-
-
-
-import NodeService from '../../service/NodeService';
-
-export default {
- data() {
- return {
- nodes: null,
- expandedKeys: {}
+@media screen and (max-width: 40em) {
+ /deep/ {
+ .sm-invisible {
+ display: none;
}
- },
- nodeService: null,
- created() {
- this.nodeService = new NodeService();
- },
- mounted() {
- this.nodeService.getTreeNodes().then(data => this.nodes = data);
- },
- methods: {
- expandAll() {
- for (let node of this.nodes) {
- this.expandNode(node);
- }
- this.expandedKeys = {...this.expandedKeys};
- },
- collapseAll() {
- this.expandedKeys = {};
- },
- expandNode(node) {
- this.expandedKeys[node.key] = true;
- if (node.children << node.children.length) {
- for (let child of node.children) {
- this.expandNode(child);
- }
- }
+ .sm-visible {
+ display: inline;
+ margin-right: .5em;
}
}
}
Properties
- Any valid attribute such as name and autofocus are passed to the underlying input element. Following is the additional property to configure the component.
+ Any valid attribute such as style and class are passed to the underlying root element. Following is the additional property to configure the component.
@@ -567,18 +1229,18 @@ export default {
null |
A map of keys to represent the state of the tree expansion state in controlled mode. |
-
- selectionMode |
- string |
- null |
- Defines the selection mode, valid values "single", "multiple", and "checkbox". |
-
selectionKeys |
any |
null |
A map of keys to control the selection state. |
+
+ selectionMode |
+ string |
+ null |
+ Defines the selection mode, valid values "single", "multiple", and "checkbox". |
+
metaKeySelection |
boolean |
@@ -586,29 +1248,143 @@ export default {
Defines how multiple items can be selected, when true metaKey needs to be pressed to select or unselect an item and when set to false selection of each item
can be toggled individually. On touch enabled devices, metaKeySelection is turned off automatically. |
+
+ rows |
+ number |
+ null |
+ Number of rows to display per page. |
+
+
+ first |
+ number |
+ 0 |
+ Index of the first row to be displayed. |
+
+
+ totalRecords |
+ number |
+ null |
+ Number of total records, defaults to length of value when not defined. |
+
+
+ paginator |
+ boolean |
+ false |
+ When specified as true, enables the pagination. |
+
+
+ paginatorPosition |
+ string |
+ bottom |
+ Position of the paginator, options are "top","bottom" or "both". |
+
+
+ alwaysShowPaginator |
+ boolean |
+ true |
+ Whether to show it even there is only one page. |
+
+
+ paginatorTemplate |
+ string |
+ FirstPageLink PrevPageLink PageLinks NextPageLink LastPageLink RowsPerPageDropdown |
+ Template of the paginator. |
+
+
+ paginatorLeft |
+ Element |
+ null |
+ Content for the left side of the paginator. |
+
+
+ paginatorRight |
+ Element |
+ null |
+ Content for the right side of the paginator. |
+
+
+ pageLinkSize |
+ number |
+ 5 |
+ Number of page links to display. |
+
+
+ rowsPerPageOptions |
+ array |
+ null |
+ Array of integer values to display inside rows per page dropdown. |
+
+
+ currentPageReportTemplate |
+ string |
+ ({currentPage} of {totalPages}) |
+ Template of the current page report element. |
+
+
+ lazy |
+ boolean |
+ false |
+ Defines if data is loaded and interacted with in lazy manner. |
+
loading |
boolean |
false |
- Whether to display loading indicator. |
+ Displays a loader to indicate data load is in progress. |
loadingIcon |
string |
- pi pi-spin |
- Icon to display when tree is loading. |
+ pi pi-spinner |
+ The icon to show while indicating data load is in progress. |
- filter |
+ rowHover |
boolean |
false |
- When specified, displays an input field to filter the items. |
+ When enabled, background of the rows change on hover. |
- filterBy |
+ autoLayout |
+ boolean |
+ false |
+ Whether the cell widths scale according to their content or not. |
+
+
+ sortField |
string |
- label |
- When filtering is enabled, filterBy decides which field or fields (comma separated) to search against. |
+ null |
+ Name of the field to sort data by default. |
+
+
+ sortOrder |
+ number |
+ null |
+ Order to sort the data by default. |
+
+
+ defaultSortOrder |
+ number |
+ 1 |
+ Default sort order of an unsorted column. |
+
+
+ multiSortMeta |
+ array |
+ null |
+ An array of SortMeta objects to sort the data by default in multiple sort mode. |
+
+
+ sortMode |
+ string |
+ single |
+ Defines whether sorting works on single column or on multiple columns. |
+
+
+ filters |
+ object |
+ null |
+ Filters object with key-value pairs to define the filters. |
filterMode |
@@ -617,10 +1393,16 @@ export default {
Mode for filtering valid values are "lenient" and "strict". Default is lenient. |
- filterPlaceholder |
+ resizableColumns |
+ boolean |
+ false |
+ When enabled, columns can be resized using drag and drop. |
+
+
+ columnResizeMode |
string |
- null |
- Placeholder text to show when filter input is empty. |
+ fit |
+ Defines whether the overall table width should change on column resize, valid values are "fit" and "expand". |
@@ -637,6 +1419,29 @@ export default {