TreeTableTreeTable is used to display hierarchical data in tabular format.

TreeTable  - 图1

Documentation

Import

  1. import {TreeTableModule} from 'primeng/treetable';
  2. import {TreeNode} from 'primeng/api';
  3.  

Getting Started

TreeTable component requires a collection of TreeNode objects as its value and templates for the presentation. TreeNode API represents a node with various properties, here is the list of properties utilized by the TreeTable.

  1. export interface TreeNode {
  2. data?: any;
  3. children?: TreeNode[];
  4. leaf?: boolean;
  5. expanded?: boolean;
  6. }
  7.  

Usually nodes will be loaded from a remote datasoure, an example NodeService that fetches the data from a json file would be;

  1. @Injectable()
  2. export class NodeService {
  3. constructor(private http: Http) {}
  4. getFilesystem() {
  5. return this.http.get('showcase/resources/data/filesystem.json')
  6. .toPromise()
  7. .then(res => <TreeNode[]> res.json().data);
  8. }
  9. }
  10.  

The filesystem.json file consists of sample data. In a real application, this should be a dynamic response generated from the remote call.

  1. {
  2. "data":
  3. [
  4. {
  5. "data":{
  6. "name":"Documents",
  7. "size":"75kb",
  8. "type":"Folder"
  9. },
  10. "children":[
  11. {
  12. "data":{
  13. "name":"Work",
  14. "size":"55kb",
  15. "type":"Folder"
  16. },
  17. "children":[
  18. {
  19. "data":{
  20. "name":"Expenses.doc",
  21. "size":"30kb",
  22. "type":"Document"
  23. }
  24. },
  25. {
  26. "data":{
  27. "name":"Resume.doc",
  28. "size":"25kb",
  29. "type":"Resume"
  30. }
  31. }
  32. ]
  33. },
  34. {
  35. "data":{
  36. "name":"Home",
  37. "size":"20kb",
  38. "type":"Folder"
  39. },
  40. "children":[
  41. {
  42. "data":{
  43. "name":"Invoices",
  44. "size":"20kb",
  45. "type":"Text"
  46. }
  47. }
  48. ]
  49. }
  50. ]
  51. },
  52. {
  53. "data":{
  54. "name":"Pictures",
  55. "size":"150kb",
  56. "type":"Folder"
  57. },
  58. "children":[
  59. {
  60. "data":{
  61. "name":"barcelona.jpg",
  62. "size":"90kb",
  63. "type":"Picture"
  64. }
  65. },
  66. {
  67. "data":{
  68. "name":"primeui.png",
  69. "size":"30kb",
  70. "type":"Picture"
  71. }
  72. },
  73. {
  74. "data":{
  75. "name":"optimus.jpg",
  76. "size":"30kb",
  77. "type":"Picture"
  78. }
  79. }
  80. ]
  81. }
  82. ]
  83. }
  84.  

Files get loaded from a service and then bound to the value property whereas header and body templates are used to define the content of these sections.

  1. export class TreeTableDemoComponent implements OnInit {
  2. files: TreeNode[];
  3. constructor(private nodeService: NodeService) {}
  4. ngOnInit() {
  5. this.nodeService.getFileSystem().then(files => this.files = files);
  6. }
  7. }
  8.  

Body template gets the following parameters;

  • $implicit: Wrapper object of a node used to serialized a TreeNode.
  • node: TreeNode instance.
  • rowData: Data of the TreeNode instance.
  • columns: Columns of the TreeTable.
    Toggle icon is configured using the p-treeTableToggler by binding the rowNode instance. Most of the time, toggler icon is added to the first column however there is no restriction on where the toggler should be located inside the row.
  1. <p-treeTable [value]="files">
  2. <ng-template pTemplate="header">
  3. <tr>
  4. <th>Name</th>
  5. <th>Size</th>
  6. <th>Type</th>
  7. </tr>
  8. </ng-template>
  9. <ng-template pTemplate="body" let-rowNode let-rowData="rowData">
  10. <tr>
  11. <td>
  12. <p-treeTableToggler [rowNode]="rowNode"></p-treeTableToggler>
  13. {{rowData.name}}
  14. </td>
  15. <td>{{rowData.size}}</td>
  16. <td>{{rowData.type}}</td>
  17. </tr>
  18. </ng-template>
  19. </p-treeTable>
  20.  

Dynamic Columns

Instead of configuring columns one by one, a simple ngFor can be used to implement dynamic columns. cols property below is an array of objects that represent a column, only property that table component uses is field, rest of the properties like header depend on your choice.

  1. export class TreeTableDemo implements OnInit {
  2. files: TreeNode[];
  3. cols: any[];
  4. constructor(private nodeService: NodeService) { }
  5. ngOnInit() {
  6. this.nodeService.getFilesystem().then(files => this.files = files);
  7. this.cols = [
  8. { field: 'name', header: 'Name' },
  9. { field: 'size', header: 'Size' },
  10. { field: 'type', header: 'Type' }
  11. ];
  12. }
  13. }
  14.  

There are two ways to render dynamic columns, since cols property is in the scope of component you can just simply bind it to ngFor directive to generate the structure.

  1. <p-treeTable [value]="files">
  2. <ng-template pTemplate="header" let-columns>
  3. <tr>
  4. <th *ngFor="let col of cols">
  5. {{col.header}}
  6. </th>
  7. </tr>
  8. </ng-template>
  9. <ng-template pTemplate="body" let-rowNode let-rowData="rowData">
  10. <tr>
  11. <td *ngFor="let col of cols; let i = index">
  12. <p-treeTableToggler [rowNode]="rowNode" *ngIf="i == 0"></p-treeTableToggler>
  13. {{rowData[col.field]}}
  14. </td>
  15. </tr>
  16. </ng-template>
  17. </p-treeTable>
  18.  

Other alternative is binding the cols array to the columns property and then defining a template variable to access it within your templates. There is only 1 case where this is required which is reorderable columns.

  1. <p-treeTable [value]="files" [columns]="cols">
  2. <ng-template pTemplate="header" let-columns>
  3. <tr>
  4. <th *ngFor="let col of columns">
  5. {{col.header}}
  6. </th>
  7. </tr>
  8. </ng-template>
  9. <ng-template pTemplate="body" let-rowNode let-rowData="rowData" let-columns="columns">
  10. <tr>
  11. <td *ngFor="let col of columns; let i = index">
  12. <p-treeTableToggler [rowNode]="rowNode" *ngIf="i == 0"></p-treeTableToggler>
  13. {{rowData[col.field]}}
  14. </td>
  15. </tr>
  16. </ng-template>
  17. </p-treeTable>
  18.  

Tip: Use ngSwitch to customize the column content per dynamic column.

Table Layout

For performance reasons, default table-layout is fixed meaning the cell widths do not depend on their content. If you require cells to scale based on their contents set autoLayout property to true. Note that for scrollable tables or tables with resizable columns auto layout is not supported.

Templates

TreeTable is a template driven component with named templates such as header and body that we've used so far. Templates grant a great level of customization and flexibility where you have total control over the presentation while table handles the features such as paging, sorting and more. This speeds up development without sacrifing flexibility. Here is the full list of available templates.

NameParametersDescription
caption-Caption content of the table.
header$implicit: ColumnsContent of the thead element.
body$implicit: Wrapper object of a node used to serialized a TreeNode node: TreeNode instance. rowData: Data of the TreeNode instance columns: Columns of the TreeTable Content of the tbody element.
footer$implicit: ColumnsContent of the tfoot element.
summary-Summary section to display below the table.
colgroup$implicit: ColumnsColGroup element of the table to customize columns.
frozenheader$implicit: ColumnsContent of the thead element in frozen side.
frozenbody$implicit: Wrapper object of a node used to serialized a TreeNode node: TreeNode instance. rowData: Data of the TreeNode instance columns: Columns of the TreeTable Content of the tbody element in frozen side.
frozenfooter$implicit: ColumnsContent of the tfoot element in frozen side.
frozencolgroup$implicit: ColumnsColGroup element of the table to customize frozen columns.
emptymessage$implicit: ColumnsContent to display when there is no value to display.
paginatorleftstate: $implicit state.page: Current page state.rows: Rows per page state.first: Index of the first records state.totalRecords: Number of total recordsContent to display when there is no value to display.
paginatorrightstate: $implicit state.page: Current page state.rows: Rows per page state.first: Index of the first records state.totalRecords: Number of total recordsContent to display when there is no value to display.
loadingbodycolumns: Columns collection Content of the tbody element to show when data is being loaded in virtual scroll mode.

Change Detection

TreeTable may need to be aware of changes in its value in some cases. For the sake of performance, this is only done when the reference of the value changes meaning a setter is used instead of ngDoCheck/IterableDiffers which can reduce performance. So when you manipulate the value such as removing a node, adding a node or changing children of a node, instead of using array methods such as push, splice create a new array reference using a spread operator or similar.

  1. this.value = [...this.value];
  2.  

Keyboard Navigation

Nodes can be navigated and toggles using arrow keys if the optional ttRow directive is applied to the body row element.

  1. <ng-template pTemplate="body" let-rowNode let-rowData="rowData" let-columns="columns">
  2. <tr [ttRow]="rowNode">
  3. <td *ngFor="let col of columns; let i = index">
  4. <p-treeTableToggler [rowNode]="rowNode" *ngIf="i == 0"></p-treeTableToggler>
  5. {{rowData[col.field]}}
  6. </td>
  7. </tr>
  8. </ng-template>
  9.  

Sections

Table offers various templates to display additional information about the data such as a caption, header, summary and footer.

  1. <p-treeTable [value]="files" [columns]="cols">
  2. <ng-template pTemplate="caption">
  3. FileSystem
  4. </ng-template>
  5. <ng-template pTemplate="header" let-columns>
  6. <tr>
  7. <th *ngFor="let col of columns">
  8. {{col.header}}
  9. </th>
  10. </tr>
  11. </ng-template>
  12. <ng-template pTemplate="body" let-rowNode let-rowData="rowData" let-columns="columns">
  13. <tr>
  14. <td *ngFor="let col of columns; let i = index">
  15. <p-treeTableToggler [rowNode]="rowNode" *ngIf="i == 0"></p-treeTableToggler>
  16. {{rowData[col.field]}}
  17. </td>
  18. </tr>
  19. </ng-template>
  20. <ng-template pTemplate="footer" let-columns>
  21. <tr>
  22. <td *ngFor="let col of columns">
  23. {{col.header}}
  24. </td>
  25. </tr>
  26. </ng-template>
  27. <ng-template pTemplate="summary">
  28. There are {{files?.length}} Root Folders
  29. </ng-template>
  30. </p-treeTable>
  31.  

See the live example.

Column Grouping

Columns can easily be grouped using templating. Let's start with sample data of sales of brands per year.

  1. export class TreeTableColGroupDemo implements OnInit {
  2. sales: TreeNode[];
  3. cols: any[];
  4. ngOnInit() {
  5. this.sales = [
  6. {
  7. data: { brand: 'Bliss', lastYearSale: '51%', thisYearSale: '40%', lastYearProfit: '$54,406.00', thisYearProfit: '$43,342'},
  8. expanded: true,
  9. children: [
  10. {
  11. data: { brand: 'Product A', lastYearSale: '25%', thisYearSale: '20%', lastYearProfit: '$34,406.00', thisYearProfit: '$23,342' },
  12. expanded: true,
  13. children: [
  14. {
  15. data: { brand: 'Product A-1', lastYearSale: '20%', thisYearSale: '10%', lastYearProfit: '$24,406.00', thisYearProfit: '$13,342' },
  16. },
  17. {
  18. data: { brand: 'Product A-2', lastYearSale: '5%', thisYearSale: '10%', lastYearProfit: '$10,000.00', thisYearProfit: '$10,000' },
  19. }
  20. ]
  21. },
  22. {
  23. data: { brand: 'Product B', lastYearSale: '26%', thisYearSale: '20%', lastYearProfit: '$24,000.00', thisYearProfit: '$23,000' },
  24. }
  25. ]
  26. },
  27. {
  28. data: { brand: 'Fate', lastYearSale: '83%', thisYearSale: '96%', lastYearProfit: '$423,132', thisYearProfit: '$312,122' },
  29. children: [
  30. {
  31. data: { brand: 'Product X', lastYearSale: '50%', thisYearSale: '40%', lastYearProfit: '$223,132', thisYearProfit: '$156,061' },
  32. },
  33. {
  34. data: { brand: 'Product Y', lastYearSale: '33%', thisYearSale: '56%', lastYearProfit: '$200,000', thisYearProfit: '$156,061' },
  35. }
  36. ]
  37. },
  38. {
  39. data: { brand: 'Ruby', lastYearSale: '38%', thisYearSale: '5%', lastYearProfit: '$12,321', thisYearProfit: '$8,500' },
  40. children: [
  41. {
  42. data: { brand: 'Product M', lastYearSale: '18%', thisYearSale: '2%', lastYearProfit: '$10,300', thisYearProfit: '$5,500' },
  43. },
  44. {
  45. data: { brand: 'Product N', lastYearSale: '20%', thisYearSale: '3%', lastYearProfit: '$2,021', thisYearProfit: '$3,000' },
  46. }
  47. ]
  48. },
  49. {
  50. data: { brand: 'Sky', lastYearSale: '49%', thisYearSale: '22%', lastYearProfit: '$745,232', thisYearProfit: '$650,323' },
  51. children: [
  52. {
  53. data: { brand: 'Product P', lastYearSale: '20%', thisYearSale: '16%', lastYearProfit: '$345,232', thisYearProfit: '$350,000' },
  54. },
  55. {
  56. data: { brand: 'Product R', lastYearSale: '29%', thisYearSale: '6%', lastYearProfit: '$400,009', thisYearProfit: '$300,323' },
  57. }
  58. ]
  59. },
  60. {
  61. data: { brand: 'Comfort', lastYearSale: '17%', thisYearSale: '79%', lastYearProfit: '$643,242', thisYearProfit: '500,332' },
  62. children: [
  63. {
  64. data: { brand: 'Product S', lastYearSale: '10%', thisYearSale: '40%', lastYearProfit: '$243,242', thisYearProfit: '$100,000' },
  65. },
  66. {
  67. data: { brand: 'Product T', lastYearSale: '7%', thisYearSale: '39%', lastYearProfit: '$400,00', thisYearProfit: '$400,332' },
  68. }
  69. ]
  70. },
  71. {
  72. data: { brand: 'Merit', lastYearSale: '52%', thisYearSale: ' 65%', lastYearProfit: '$421,132', thisYearProfit: '$150,005' },
  73. children: [
  74. {
  75. data: { brand: 'Product L', lastYearSale: '20%', thisYearSale: '40%', lastYearProfit: '$121,132', thisYearProfit: '$100,000' },
  76. },
  77. {
  78. data: { brand: 'Product G', lastYearSale: '32%', thisYearSale: '25%', lastYearProfit: '$300,000', thisYearProfit: '$50,005' },
  79. }
  80. ]
  81. },
  82. {
  83. data: { brand: 'Violet', lastYearSale: '82%', thisYearSale: '12%', lastYearProfit: '$131,211', thisYearProfit: '$100,214' },
  84. children: [
  85. {
  86. data: { brand: 'Product SH1', lastYearSale: '30%', thisYearSale: '6%', lastYearProfit: '$101,211', thisYearProfit: '$30,214' },
  87. },
  88. {
  89. data: { brand: 'Product SH2', lastYearSale: '52%', thisYearSale: '6%', lastYearProfit: '$30,000', thisYearProfit: '$70,000' },
  90. }
  91. ]
  92. },
  93. {
  94. data: { brand: 'Dulce', lastYearSale: '44%', thisYearSale: '45%', lastYearProfit: '$66,442', thisYearProfit: '$53,322' },
  95. children: [
  96. {
  97. data: { brand: 'Product PN1', lastYearSale: '22%', thisYearSale: '25%', lastYearProfit: '$33,221', thisYearProfit: '$20,000' },
  98. },
  99. {
  100. data: { brand: 'Product PN2', lastYearSale: '22%', thisYearSale: '25%', lastYearProfit: '$33,221', thisYearProfit: '$33,322' },
  101. }
  102. ]
  103. },
  104. {
  105. data: { brand: 'Solace', lastYearSale: '90%', thisYearSale: '56%', lastYearProfit: '$765,442', thisYearProfit: '$296,232' },
  106. children: [
  107. {
  108. data: { brand: 'Product HT1', lastYearSale: '60%', thisYearSale: '36%', lastYearProfit: '$465,000', thisYearProfit: '$150,653' },
  109. },
  110. {
  111. data: { brand: 'Product HT2', lastYearSale: '30%', thisYearSale: '20%', lastYearProfit: '$300,442', thisYearProfit: '$145,579' },
  112. }
  113. ]
  114. },
  115. {
  116. data: { brand: 'Essence', lastYearSale: '75%', thisYearSale: '54%', lastYearProfit: '$21,212', thisYearProfit: '$12,533' },
  117. children: [
  118. {
  119. data: { brand: 'Product TS1', lastYearSale: '50%', thisYearSale: '34%', lastYearProfit: '$11,000', thisYearProfit: '$8,562' },
  120. },
  121. {
  122. data: { brand: 'Product TS2', lastYearSale: '25%', thisYearSale: '20%', lastYearProfit: '$11,212', thisYearProfit: '$3,971' },
  123. }
  124. ]
  125. }
  126. ];
  127. }
  128. }
  129.  
  1. <p-treeTable [value]="sales">
  2. <ng-template pTemplate="header">
  3. <tr>
  4. <th rowspan="3">Brand</th>
  5. <th colspan="4">Sale Rate</th>
  6. </tr>
  7. <tr>
  8. <th colspan="2">Sales</th>
  9. <th colspan="2">Profits</th>
  10. </tr>
  11. <tr>
  12. <th>Last Year</th>
  13. <th>This Year</th>
  14. <th>Last Year</th>
  15. <th>This Year</th>
  16. </tr>
  17. </ng-template>
  18. <ng-template pTemplate="body" let-rowNode let-rowData="rowData">
  19. <tr>
  20. <td>
  21. <p-treeTableToggler [rowNode]="rowNode"></p-treeTableToggler>
  22. {{rowData.brand}}
  23. </td>
  24. <td>{{rowData.lastYearSale}}</td>
  25. <td>{{rowData.thisYearSale}}</td>
  26. <td>{{rowData.lastYearProfit}}</td>
  27. <td>{{rowData.thisYearProfit}}</td>
  28. </tr>
  29. </ng-template>
  30. <ng-template pTemplate="footer">
  31. <tr>
  32. <td colspan="3">Totals</td>
  33. <td>$3,283,772</td>
  34. <td>$2,126,925</td>
  35. </tr>
  36. </ng-template>
  37. </p-treeTable>
  38.  

See the live example.

Paginator

Pagination is enabled by setting paginator property to true, rows property defines the number of rows per page and pageLinks specify the the number of page links to display. See paginator component for more information.

  1. <p-treeTable [value]="files" [columns]="cols" [paginator]="true" [rows]="10">
  2. <ng-template pTemplate="header" let-columns>
  3. <tr>
  4. <th *ngFor="let col of columns">
  5. {{col.header}}
  6. </th>
  7. </tr>
  8. </ng-template>
  9. <ng-template pTemplate="body" let-rowNode let-rowData="rowData" let-columns="columns">
  10. <tr>
  11. <td *ngFor="let col of columns; let i = index">
  12. <p-treeTableToggler [rowNode]="rowNode" *ngIf="i == 0"></p-treeTableToggler>
  13. {{rowData[col.field]}}
  14. </td>
  15. </tr>
  16. </ng-template>
  17. </p-treeTable>
  18.  

Paginator accepts custom content for the left and the right side via named templates.

  1. <p-treeTable [value]="files" [columns]="cols" [paginator]="true" [rows]="10">
  2. <ng-template pTemplate="header" let-columns>
  3. <tr>
  4. <th *ngFor="let col of columns">
  5. {{col.header}}
  6. </th>
  7. </tr>
  8. </ng-template>
  9. <ng-template pTemplate="body" let-rowNode let-rowData="rowData" let-columns="columns">
  10. <tr>
  11. <td *ngFor="let col of columns; let i = index">
  12. <p-treeTableToggler [rowNode]="rowNode" *ngIf="i == 0"></p-treeTableToggler>
  13. {{rowData[col.field]}}
  14. </td>
  15. </tr>
  16. </ng-template>
  17. <ng-template pTemplate="paginatorleft" let-state>
  18. {{state.first}}
  19. <button type="button" pButton icon="fa-refresh"></button>
  20. </ng-template>
  21. <ng-template pTemplate="paginatorright">
  22. <button type="button" pButton icon="fa-cloud-upload"></button>
  23. </ng-template>
  24. </p-treeTable>
  25.  

Paginator templates gets the paginator state as an implicit variable that provides the following properties

Sorting

A column can be made sortable by adding the ttSortableColumn directive whose value is the field to sort against and a sort indicator via p-treeTableSortIcon component. For dynamic columns, setting ttSortableColumnDisabled property as true disables sorting for that particular column.

  1. <p-treeTable [value]="files" [columns]="cols">
  2. <ng-template pTemplate="header" let-columns>
  3. <tr>
  4. <th *ngFor="let col of columns" [ttSortableColumn]="col.field">
  5. {{col.header}}
  6. <p-treeTableSortIcon [field]="col.field"></p-treeTableSortIcon>
  7. </th>
  8. </tr>
  9. </ng-template>
  10. <ng-template pTemplate="body" let-rowNode let-rowData="rowData" let-columns="columns">
  11. <tr>
  12. <td *ngFor="let col of columns; let i = index">
  13. <p-treeTableToggler [rowNode]="rowNode" *ngIf="i == 0"></p-treeTableToggler>
  14. {{rowData[col.field]}}
  15. </td>
  16. </tr>
  17. </ng-template>
  18. </p-treeTable>
  19.  

Default sorting is executed on a single column, in order to enable multiple field sorting, set sortMode property to "multiple" and use metakey when clicking on another column.

  1. <p-treeTable [value]="cars" sortMode="multiple">
  2.  

In case you'd like to display the table as sorted by default initially on load, use the sortField-sortOrder properties in single mode.

  1. <p-treeTable [value]="files" [columns]="cols" sortField="year">
  2. <ng-template pTemplate="header" let-columns>
  3. <tr>
  4. <th *ngFor="let col of columns" [ttSortableColumn]="col.field">
  5. {{col.header}}
  6. <p-treeTableSortIcon [field]="col.field"></p-treeTableSortIcon>
  7. </th>
  8. </tr>
  9. </ng-template>
  10. <ng-template pTemplate="body" let-rowNode let-rowData="rowData" let-columns="columns">
  11. <tr>
  12. <td *ngFor="let col of columns; let i = index">
  13. <p-treeTableToggler [rowNode]="rowNode" *ngIf="i == 0"></p-treeTableToggler>
  14. {{rowData[col.field]}}
  15. </td>
  16. </tr>
  17. </ng-template>
  18. </p-treeTable>
  19.  

In multiple mode, use the multiSortMeta property and bind an array of SortMeta objects.

  1. <p-treeTable [value]="files" [columns]="cols" sortMode="multiple" [multiSortMeta]="multiSortMeta">
  2. <ng-template pTemplate="header" let-columns>
  3. <tr>
  4. <th *ngFor="let col of columns" [ttSortableColumn]="col.field">
  5. {{col.header}}
  6. <p-treeTableSortIcon [field]="col.field"></p-treeTableSortIcon>
  7. </th>
  8. </tr>
  9. </ng-template>
  10. <ng-template pTemplate="body" let-rowNode let-rowData="rowData" let-columns="columns">
  11. <tr>
  12. <td *ngFor="let col of columns; let i = index">
  13. <p-treeTableToggler [rowNode]="rowNode" *ngIf="i == 0"></p-treeTableToggler>
  14. {{rowData[col.field]}}
  15. </td>
  16. </tr>
  17. </ng-template>
  18. </p-treeTable>
  19.  
  1. this.multiSortMeta = [];
  2. this.multiSortMeta.push({field: 'year', order: 1});
  3. this.multiSortMeta.push({field: 'brand', order: -1});
  4.  

Instead of using the built-in sorting algorithm a custom sort can be attached by enabling customSort property and defining a sortFunction implementation. This function gets a SortEvent instance that provides the data to sort, sortField, sortOrder and multiSortMeta.

  1. export class TreeTableSortDemo implements OnInit {
  2. files: TreeNode[];
  3. cols: any[];
  4. constructor(private nodeService: NodeService) { }
  5. ngOnInit() {
  6. this.nodeService.getFilesystem().then(files => this.files = files);
  7. this.cols = [
  8. { field: 'name', header: 'Name' },
  9. { field: 'size', header: 'Size' },
  10. { field: 'type', header: 'Type' }
  11. ];
  12. }
  13. customSort(event: SortEvent) {
  14. //event.data = Data to sort
  15. //event.mode = 'single' or 'multiple' sort mode
  16. //event.field = Sort field in single sort
  17. //event.order = Sort order in single sort
  18. //event.multiSortMeta = SortMeta array in multiple sort
  19. event.data.sort((data1, data2) => {
  20. let value1 = data1[event.field];
  21. let value2 = data2[event.field];
  22. let result = null;
  23. if (value1 == null && value2 != null)
  24. result = -1;
  25. else if (value1 != null && value2 == null)
  26. result = 1;
  27. else if (value1 == null && value2 == null)
  28. result = 0;
  29. else if (typeof value1 === 'string' && typeof value2 === 'string')
  30. result = value1.localeCompare(value2);
  31. else
  32. result = (value1 < value2) ? -1 : (value1 > value2) ? 1 : 0;
  33. return (event.order * result);
  34. });
  35. }
  36. }
  37.  
  1. <p-treeTable [value]="files" [columns]="cols" (sortFunction)="customSort($event)" [customSort]="true">
  2. <ng-template pTemplate="header" let-columns>
  3. <tr>
  4. <th *ngFor="let col of columns" [ttSortableColumn]="col.field">
  5. {{col.header}}
  6. <p-treeTableSortIcon [field]="col.field"></p-treeTableSortIcon>
  7. </th>
  8. </tr>
  9. </ng-template>
  10. <ng-template pTemplate="body" let-rowNode let-rowData="rowData" let-columns="columns">
  11. <tr>
  12. <td *ngFor="let col of columns; let i = index">
  13. <p-treeTableToggler [rowNode]="rowNode" *ngIf="i == 0"></p-treeTableToggler>
  14. {{rowData[col.field]}}
  15. </td>
  16. </tr>
  17. </ng-template>
  18. </p-treeTable>
  19.  

For screen reader support of sortable headers, use ariaLabelDesc and ariaLabelAsc properties on p-sortIcon directive.

See the live example.

Filtering

Filtering is enabled by defining the filter elements and calling filter method on the local template variable of the table with value, column field and match mode parameters. Available match modes are "startsWith", "contains", "endsWith", "equals", "notEquals", "in", "lt", "lte", "gt" and "gte".

An optional global filter feature is available to search all fields with the same query, to enable this place an input component and call the filterGlobal function with value and match mode properties on your event of choice.

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.

  1. <p-treeTable #tt [value]="files" [columns]="cols">
  2. <ng-template pTemplate="caption">
  3. <div style="text-align: right">
  4. <i class="pi pi-search" style="margin:4px 4px 0 0"></i>
  5. <input type="text" pInputText size="50" placeholder="Global Filter" (input)="tt.filterGlobal($event.target.value, 'contains')" style="width:auto">
  6. </div>
  7. </ng-template>
  8. <ng-template pTemplate="header" let-columns>
  9. <tr>
  10. <th *ngFor="let col of cols">
  11. {{col.header}}
  12. </th>
  13. </tr>
  14. <tr>
  15. <th *ngFor="let col of cols">
  16. <input pInputText type="text" (input)="tt.filter($event.target.value, col.field, col.filterMatchMode)">
  17. </th>
  18. </tr>
  19. </ng-template>
  20. <ng-template pTemplate="body" let-rowNode let-rowData="rowData">
  21. <tr>
  22. <td *ngFor="let col of cols; let i = index">
  23. <p-treeTableToggler [rowNode]="rowNode" *ngIf="i == 0"></p-treeTableToggler>
  24. {{rowData[col.field]}}
  25. </td>
  26. </tr>
  27. </ng-template>
  28. </p-treeTable>
  29.  
  1. @Component({
  2. templateUrl: './treetablefilterdemo.html'
  3. })
  4. export class TreeTableFilterDemo {
  5. files: TreeNode[];
  6. cols: any[];
  7. constructor(private nodeService: NodeService) { }
  8. ngOnInit() {
  9. this.nodeService.getFilesystem().then(files => this.files = files);
  10. this.cols = [
  11. { field: 'name', header: 'Name' },
  12. { field: 'size', header: 'Size' },
  13. { field: 'type', header: 'Type' }
  14. ];
  15. }
  16. }
  17.  

If you have static columns and need to use global filtering, globalFilterFields property must be defined to configure which fields should be used in global filtering. Another use case of this property is to change the fields to utilize in global filtering with dynamic columns.

  1. <p-treeTable #tt [value]="files" [columns]="cols">
  2. //content
  3. </p-treeTable>
  4.  

See the live example.

Selection

TreeTable provides built-in single, multiple and checkbox selection features where selected rows are bound to the selection property and onRowSelect-onRowUnselect events are provided as optional callbacks. In order to enable this feature, define a selectionMode, bind a selection reference and add ttSelectableRow directive whose value is the rowNode to the rows that can be selected. Additionally if you prefer double click use ttSelectableRowDblClick directive instead and to disable selection events on a particular row use ttSelectableRowDisabled property.

By default each row click adds or removes the row from the selection, if you prefer a classic metaKey based selection approach enable metaKeySelection true so that multiple selection or unselection of a row requires metaKey to be pressed. Note that, on touch enabled devices, metaKey based selection is turned off automatically as there is no metaKey in devices such as mobile phones.

Alternative to the row click, checkbox elements can be used to implement row selection as well.

When resolving if a row is selected, by default TreeTable compares selection array with the datasource which may cause a performance issue with huge datasets that do not use pagination. If available the fastest way is to use dataKey property that identifies a unique row so that Table can avoid comparing arrays as internally a map instance is used instead of looping arrays, on the other hand if dataKey cannot be provided consider using compareSelectionBy property as "equals" which uses reference comparison instead of the default "deepEquals" comparison. Latter is slower since it checks all properties.

In single mode, selection binding is an object reference.

  1. export class TreeTableSelectionDemo {
  2. files: TreeNode[];
  3. selectedNode: TreeNode;
  4. constructor(private carService: CarService) { }
  5. ngOnInit() {
  6. this.nodeService.getFilesystem().then(files => this.files = files);
  7. }
  8. }
  9.  
  1. <p-treeTable [value]="files" [columns]="cols" selectionMode="single" [(selection)]="selectedNode" dataKey="name">
  2. <ng-template pTemplate="header" let-columns>
  3. <tr>
  4. <th *ngFor="let col of columns">
  5. {{col.header}}
  6. </th>
  7. </tr>
  8. </ng-template>
  9. <ng-template pTemplate="body" let-rowNode let-rowData="rowData" let-columns="columns">
  10. <tr [ttSelectableRow]="rowNode">
  11. <td *ngFor="let col of columns; let i = index">
  12. <p-treeTableToggler [rowNode]="rowNode" *ngIf="i == 0"></p-treeTableToggler>
  13. {{rowData[col.field]}}
  14. </td>
  15. </tr>
  16. </ng-template>
  17. </p-treeTable>
  18.  

In multiple mode, selection binding should be an array.

  1. export class TreeTableSelectionDemo {
  2. files: TreeNode[];
  3. selectedNodes: TreeNode[];
  4. constructor(private carService: CarService) { }
  5. ngOnInit() {
  6. this.nodeService.getFilesystem().then(files => this.files = files);
  7. }
  8. }
  9.  
  1. <p-treeTable [value]="files" [columns]="cols" selectionMode="multiple" [(selection)]="selectedNodes" dataKey="name">
  2. <ng-template pTemplate="header" let-columns>
  3. <tr>
  4. <th *ngFor="let col of columns">
  5. {{col.header}}
  6. </th>
  7. </tr>
  8. </ng-template>
  9. <ng-template pTemplate="body" let-rowNode let-rowData="rowData" let-columns="columns">
  10. <tr [ttSelectableRow]="rowNode">
  11. <td *ngFor="let col of columns; let i = index">
  12. <p-treeTableToggler [rowNode]="rowNode" *ngIf="i == 0"></p-treeTableToggler>
  13. {{rowData[col.field]}}
  14. </td>
  15. </tr>
  16. </ng-template>
  17. </p-treeTable>
  18.  

Checkbox selection utilizes p-treeTableCheckbox component whose value should be the rowNode. Optionally p-treeTableHeaderCheckbox is available to select or unselect all the nodes.

  1. <p-treeTable [value]="files" [columns]="cols" selectionMode="checkbox" [(selection)]="selectedNodes">
  2. <ng-template pTemplate="caption">
  3. <div style="text-align:left">
  4. <p-treeTableHeaderCheckbox></p-treeTableHeaderCheckbox>
  5. <span style="margin-left: .25em; vertical-align: middle">Toggle All</span>
  6. </div>
  7. </ng-template>
  8. <ng-template pTemplate="header" let-columns>
  9. <tr>
  10. <th *ngFor="let col of columns">
  11. {{col.header}}
  12. </th>
  13. </tr>
  14. </ng-template>
  15. <ng-template pTemplate="body" let-rowNode let-rowData="rowData" let-columns="columns">
  16. <tr>
  17. <td *ngFor="let col of columns; let i = index">
  18. <p-treeTableToggler [rowNode]="rowNode" *ngIf="i == 0"></p-treeTableToggler>
  19. <p-treeTableCheckbox [value]="rowNode" *ngIf="i == 0"></p-treeTableCheckbox>
  20. {{rowData[col.field]}}
  21. </td>
  22. </tr>
  23. </ng-template>
  24. </p-treeTable>
  25.  

See the live example.

ContextMenu

TreeTable has exclusive integration with contextmenu component. In order to attach a menu to a treetable, add ttContextMenuRow directive to the rows that can be selected with context menu, define a local template variable for the menu and bind it to the contextMenu property of the treetable. This enables displaying the menu whenever a row is right clicked. A separate contextMenuSelection property is used to get a hold of the right clicked row. For dynamic columns, setting ttContextMenuRowDisabled property as true disables context menu for that particular row.

  1. <p-toast [style]="{marginTop: '80px'}"></p-toast>
  2. <p-treeTable [value]="files" [columns]="cols" dataKey="name" [(contextMenuSelection)]="selectedNode" [contextMenu]="cm">
  3. <ng-template pTemplate="header" let-columns>
  4. <tr>
  5. <th *ngFor="let col of columns">
  6. {{col.header}}
  7. </th>
  8. </tr>
  9. </ng-template>
  10. <ng-template pTemplate="body" let-rowNode let-rowData="rowData" let-columns="columns">
  11. <tr [ttContextMenuRow]="rowNode">
  12. <td *ngFor="let col of columns; let i = index">
  13. <p-treeTableToggler [rowNode]="rowNode" *ngIf="i == 0"></p-treeTableToggler>
  14. {{rowData[col.field]}}
  15. </td>
  16. </tr>
  17. </ng-template>
  18. </p-treeTable>
  19. <p-contextMenu #cm [model]="items"></p-contextMenu>
  20.  

See the live example.

Editing

Incell editing is enabled by adding ttEditableColumn directive to an editable cell that has a p:treeTableCellEditor helper component to define the input-output templates for the edit and view modes respectively.

  1. <p-treeTable [value]="files" [columns]="cols">
  2. <ng-template pTemplate="header" let-columns>
  3. <tr>
  4. <th *ngFor="let col of columns">
  5. {{col.header}}
  6. </th>
  7. </tr>
  8. </ng-template>
  9. <ng-template pTemplate="body" let-rowNode let-rowData="rowData" let-columns="columns">
  10. <tr>
  11. <td *ngFor="let col of columns; let i = index" ttEditableColumn>
  12. <p-treeTableToggler [rowNode]="rowNode" *ngIf="i == 0"></p-treeTableToggler>
  13. <p-treeTableCellEditor>
  14. <ng-template pTemplate="input">
  15. <input type="text" [(ngModel)]="rowData[col.field]">
  16. </ng-template>
  17. <ng-template pTemplate="output">
  18. {{rowData[col.field]}}
  19. </ng-template>
  20. </p-treeTableCellEditor>
  21. </td>
  22. </tr>
  23. </ng-template>
  24. </p-treeTable>
  25.  

If you require the edited row data and the field at onEditComplete event, bind the data to the ttEditableColumn directive and the field to the ttEditableColumnField directive

  1. <td [ttEditableColumn]="rowData" [ttEditableColumnField]="'year'">
  2.  

See the live example.

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. onColumnResize is a callback that passes the resized column header as a parameter. For dynamic columns, setting ttResizableColumnDisabled property as true disables resizing for that particular column. 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.

  1. <p-treeTable [value]="files" [columns]="cols" [resizableColumns]="true">
  2. <ng-template pTemplate="header" let-columns>
  3. <tr>
  4. <th *ngFor="let col of columns" ttResizableColumn>
  5. {{col.header}}
  6. </th>
  7. </tr>
  8. </ng-template>
  9. <ng-template pTemplate="body" let-rowNode let-rowData="rowData" let-columns="columns">
  10. <tr>
  11. <td *ngFor="let col of columns; let i = index">
  12. <p-treeTableToggler [rowNode]="rowNode" *ngIf="i == 0"></p-treeTableToggler>
  13. {{rowData[col.field]}}
  14. </td>
  15. </tr>
  16. </ng-template>
  17. </p-treeTable>
  18.  

Note: Scrollable tables require a column group to support resizing.

  1. <p-treeTable [value]="files" [columns]="cols" [scrollable]="true" scrollHeight="200px" [resizableColumns]="true">
  2. <ng-template pTemplate="colgroup" let-columns>
  3. <colgroup>
  4. <col *ngFor="let col of columns" >
  5. </colgroup>
  6. </ng-template>
  7. <ng-template pTemplate="header" let-columns>
  8. <tr>
  9. <th *ngFor="let col of columns" ttResizableColumn>
  10. {{col.header}}
  11. </th>
  12. </tr>
  13. </ng-template>
  14. <ng-template pTemplate="body" let-rowNode let-rowData="rowData" let-columns="columns">
  15. <tr>
  16. <td *ngFor="let col of columns; let i = index">
  17. <p-treeTableToggler [rowNode]="rowNode" *ngIf="i == 0"></p-treeTableToggler>
  18. {{rowData[col.field]}}
  19. </td>
  20. </tr>
  21. </ng-template>
  22. </p-treeTable>
  23.  

See the live example.

Column Reordering

Columns can be reordered using drag drop by setting the reorderableColumns to true and adding ttReorderableColumn directive to the columns that can be dragged. Note that columns should be dynamic for reordering to work. For dynamic columns, setting ttReorderableColumnDisabled property as true disables reordering for that particular column.

  1. <p-treeTable [value]="files" [columns]="cols" [reorderableColumns]="true">
  2. <ng-template pTemplate="header" let-columns>
  3. <tr>
  4. <th *ngFor="let col of columns" ttReorderableColumn>
  5. {{col.header}}
  6. </th>
  7. </tr>
  8. </ng-template>
  9. <ng-template pTemplate="body" let-rowNode let-rowData="rowData" let-columns="columns">
  10. <tr>
  11. <td *ngFor="let col of columns; let i = index">
  12. <p-treeTableToggler [rowNode]="rowNode" *ngIf="i == 0"></p-treeTableToggler>
  13. {{rowData[col.field]}}
  14. </td>
  15. </tr>
  16. </ng-template>
  17. </p-treeTable>
  18.  

See the live example.

Scrolling

TreeTable supports both horizontal and vertical scrolling as well as frozen columns. Additionally, virtualScroll mode enables dealing with large datasets by loading data on demand during scrolling.

Sample below uses vertical scrolling where headers are fixed and data is scrollable.

  1. <p-treeTable [value]="files" [columns]="cols" [scrollable]="true" scrollHeight="200px">
  2. <ng-template pTemplate="header" let-columns>
  3. <tr>
  4. <th *ngFor="let col of columns">
  5. {{col.header}}
  6. </th>
  7. </tr>
  8. </ng-template>
  9. <ng-template pTemplate="body" let-rowNode let-rowData="rowData" let-columns="columns">
  10. <tr>
  11. <td *ngFor="let col of columns; let i = index">
  12. <p-treeTableToggler [rowNode]="rowNode" *ngIf="i == 0"></p-treeTableToggler>
  13. {{rowData[col.field]}}
  14. </td>
  15. </tr>
  16. </ng-template>
  17. </p-treeTable>
  18.  

In horizontal scrolling on the other hand, it is important to give fixed widths to columns. In general when customizing the column widths of scrollable tables, use colgroup as below to avoid misalignment issues as it will apply both the header, body and footer sections which are different separate elements internally.

  1. <p-treeTable [value]="files" [columns]="cols" [scrollable]="true" [style]="{width:'600px'}">
  2. <ng-template pTemplate="colgroup" let-columns>
  3. <colgroup>
  4. <col *ngFor="let col of columns" style="width:350px">
  5. </colgroup>
  6. </ng-template>
  7. <ng-template pTemplate="header" let-columns>
  8. <tr>
  9. <th *ngFor="let col of columns">
  10. {{col.header}}
  11. </th>
  12. </tr>
  13. </ng-template>
  14. <ng-template pTemplate="body" let-rowNode let-rowData="rowData" let-columns="columns">
  15. <tr>
  16. <td *ngFor="let col of columns; let i = index">
  17. <p-treeTableToggler [rowNode]="rowNode" *ngIf="i == 0"></p-treeTableToggler>
  18. {{rowData[col.field]}}
  19. </td>
  20. </tr>
  21. </ng-template>
  22. </p-treeTable>
  23.  

Horizontal and Vertical scrolling can be combined as well on the same table.

  1. <p-treeTable [value]="files" [columns]="cols" [scrollable]="true" scrollHeight="200px" [style]="{width:'600px'}">
  2. <ng-template pTemplate="colgroup" let-columns>
  3. <colgroup>
  4. <col *ngFor="let col of columns" style="width:350px">
  5. </colgroup>
  6. </ng-template>
  7. <ng-template pTemplate="header" let-columns>
  8. <tr>
  9. <th *ngFor="let col of columns">
  10. {{col.header}}
  11. </th>
  12. </tr>
  13. </ng-template>
  14. <ng-template pTemplate="body" let-rowNode let-rowData="rowData" let-columns="columns">
  15. <tr>
  16. <td *ngFor="let col of columns; let i = index">
  17. <p-treeTableToggler [rowNode]="rowNode" *ngIf="i == 0"></p-treeTableToggler>
  18. {{rowData[col.field]}}
  19. </td>
  20. </tr>
  21. </ng-template>
  22. </p-treeTable>
  23.  

Particular columns can be made fixed where others remain scrollable, there are to ways to implement this functionality, either define a frozenColumns property if your frozen columns are dynamic or use frozenbody template. The width of the frozen section also must be defined with frozenWidth property. Templates including header, body and footer apply to the frozen section as well, however if require different content for the frozen section use frozenheader, frozenbody and frozenfooter instead.

  1. <p-treeTable [value]="files" [columns]="scrollableCols" [frozenColumns]="frozenCols" [scrollable]="true" scrollHeight="200px" frozenWidth="200px">
  2. <ng-template pTemplate="colgroup" let-columns>
  3. <colgroup>
  4. <col *ngFor="let col of columns" style="width:250px">
  5. </colgroup>
  6. </ng-template>
  7. <ng-template pTemplate="header" let-columns>
  8. <tr>
  9. <th *ngFor="let col of columns">
  10. {{col.header}}
  11. </th>
  12. </tr>
  13. </ng-template>
  14. <ng-template pTemplate="body" let-rowData="rowData" let-columns="columns">
  15. <tr>
  16. <td *ngFor="let col of columns; let i = index">
  17. {{rowData[col.field]}}
  18. </td>
  19. </tr>
  20. </ng-template>
  21. <ng-template pTemplate="frozenbody" let-rowNode let-rowData="rowData">
  22. <tr>
  23. <td>
  24. <p-treeTableToggler [rowNode]="rowNode"></p-treeTableToggler>
  25. {{rowData.name}}
  26. </td>
  27. </tr>
  28. </ng-template>
  29. </p-treeTable>
  30.  

When frozen columns are enabled, frozen and scrollable cells may have content with varying height which leads to misalignment. To avoid a performance hit, TreeTable avoids expensive calculations to align the row heights as it can be easily done with CSS manually.

  1. .ui-treetable .ui-treetable-frozen-view .ui-treetable-tbody > tr > td,
  2. .ui-treetable .ui-treetable-unfrozen-view .ui-treetable-tbody > tr > td {
  3. height: 24px !important;
  4. }
  5.  

Virtual Scrolling is used with lazy loading to fetch data on demand during scrolling. For smooth scrolling twice the amount of rows property is loaded on a lazy load event. In addition, to avoid performance problems row height is not calculated automatically and should be provided using virtualRowHeight property which defaults to 28px, in your row template also assign the height of the row with the same value for smooth scrolling. Note that variable row height is not supported due to the nature of the virtual scrolling behavior.

  1. <p-treeTable [value]="virtualFiles" [columns]="cols" [scrollable]="true" [rows]="20" scrollHeight="200px"
  2. [virtualScroll]="true" [virtualRowHeight]="34" [lazy]="true" (onLazyLoad)="loadNodes($event)"
  3. [totalRecords]="totalRecords" [loading]="loading" (onNodeExpand)="onNodeExpand($event)">
  4. <ng-template pTemplate="header" let-columns>
  5. <tr>
  6. <th *ngFor="let col of columns">
  7. {{col.header}}
  8. </th>
  9. </tr>
  10. </ng-template>
  11. <ng-template pTemplate="body" let-rowNode let-rowData="rowData" let-columns="columns">
  12. <tr>
  13. <td *ngFor="let col of columns; let i = index">
  14. <p-treeTableToggler [rowNode]="rowNode" *ngIf="i == 0"></p-treeTableToggler>
  15. {{rowData[col.field]}}
  16. </td>
  17. </tr>
  18. </ng-template>
  19. </p-treeTable>
  20.  

Instead of using the built-in loading mask indicator, an special "loadingbody" template is available to provide feedback to the users about the loading status of a scroll event.

  1. <p-treeTable [value]="virtualFiles" [columns]="cols" [scrollable]="true" [rows]="20" scrollHeight="200px"
  2. [virtualScroll]="true" [virtualRowHeight]="34" [lazy]="true" (onLazyLoad)="loadNodes($event)"
  3. [totalRecords]="totalRecords" (onNodeExpand)="onNodeExpand($event)" [loading]="true" [showLoader]="showLoader">
  4. <ng-template pTemplate="header" let-columns>
  5. <tr>
  6. <th *ngFor="let col of columns">
  7. {{col.header}}
  8. </th>
  9. </tr>
  10. </ng-template>
  11. <ng-template pTemplate="body" let-rowNode let-rowData="rowData" let-columns="columns">
  12. <tr>
  13. <td *ngFor="let col of columns; let i = index">
  14. <p-treeTableToggler [rowNode]="rowNode" *ngIf="i == 0"></p-treeTableToggler>
  15. {{rowData[col.field]}}
  16. </td>
  17. </tr>
  18. </ng-template>
  19. <ng-template pTemplate="loadingbody" let-columns="columns">
  20. <tr style="height:34px">
  21. <td *ngFor="let col of columns;">
  22. <div class="loading-text"></div>
  23. </td>
  24. </tr>
  25. </ng-template>
  26. </p-treeTable>
  27.  

See the live example.

Lazy Loading

Lazy mode is handy to deal with large datasets, instead of loading the entire data, small chunks of data is loaded by invoking onLazyLoad callback everytime paging and sorting. To implement lazy loading, enable lazy attribute and provide a method callback using onLazyLoad that actually loads the data from a remote datasource. onLazyLoad gets an event object that contains information about how the data should be loaded. It is also important to assign the logical number of rows to totalRecords by doing a projection query for paginator configuration so that paginator displays the UI assuming there are actually records of totalRecords size although in reality they aren't as in lazy mode, only the records that are displayed on the current page exist.

  1. <p-treeTable [value]="files" [columns]="cols" [paginator]="true" [rows]="10" [lazy]="true"
  2. (onLazyLoad)="loadNodes($event)" [totalRecords]="totalRecords">
  3. <ng-template pTemplate="header" let-columns>
  4. <tr>
  5. <th *ngFor="let col of columns">
  6. {{col.header}}
  7. </th>
  8. </tr>
  9. </ng-template>
  10. <ng-template pTemplate="body" let-rowNode let-rowData="rowData" let-columns="columns">
  11. <tr>
  12. <td *ngFor="let col of columns; let i = index">
  13. <p-treeTableToggler [rowNode]="rowNode" *ngIf="i == 0"></p-treeTableToggler>
  14. {{rowData[col.field]}}
  15. </td>
  16. </tr>
  17. </ng-template>
  18. </p-treeTable>
  19.  
  1. loadNodes(event: LazyLoadEvent) {
  2. //event.first = First row offset
  3. //event.rows = Number of rows per page
  4. //event.sortField = Field name to sort in single sort mode
  5. //event.sortOrder = Sort order as number, 1 for asc and -1 for dec in single sort mode
  6. //event.multiSortMeta: An array of SortMeta objects used in multiple columns sorting. Each SortMeta has field and order properties.
  7. //event.filters: FilterMetadata object having field as key and filter value, filter matchMode as value
  8. //event.globalFilter: Value of the global filter if available
  9. this.files = //do a request to a remote datasource using a service and return the cars that match the lazy load criteria
  10. }
  11.  

Lazy loading applies to the first level nodes in the tree hierarchy, instead if you need to lazy load the children of a node, set leaf as true on that node and use onNodeExpand event to load children when a node is expanded only.

  1. <p-treeTable [value]="files" [columns]="cols" (onNodeExpand)="onNodeExpand($event)">
  2. <ng-template pTemplate="header" let-columns>
  3. <tr>
  4. <th *ngFor="let col of columns">
  5. {{col.header}}
  6. </th>
  7. </tr>
  8. </ng-template>
  9. <ng-template pTemplate="body" let-rowNode let-rowData="rowData" let-columns="columns">
  10. <tr>
  11. <td *ngFor="let col of columns; let i = index">
  12. <p-treeTableToggler [rowNode]="rowNode" *ngIf="i == 0"></p-treeTableToggler>
  13. {{rowData[col.field]}}
  14. </td>
  15. </tr>
  16. </ng-template>
  17. </p-treeTable>
  18.  
  1. onNodeExpand(event) }
  2. //const node = event.node;
  3. //populate node.children
  4. //refresh the data
  5. this.files = [...this.files];
  6. }
  7.  

See the live example.

Responsive

TreeTable does not provide a built-in responsive feature as it is easy to implement as you have full control over the presentation, here is an example with media queries.

  1. @Component({
  2. templateUrl: './treetableresponsivedemo.html',
  3. styles: [`
  4. :host ::ng-deep .priority-2,
  5. :host ::ng-deep .priority-3,
  6. :host ::ng-deep .visibility-sm {
  7. display: none;
  8. }
  9. @media screen and (max-width: 39.938em) {
  10. :host ::ng-deep .visibility-sm {
  11. display: inline;
  12. }
  13. }
  14. @media screen and (min-width: 40em) {
  15. :host ::ng-deep .priority-2 {
  16. display: table-cell;
  17. }
  18. }
  19. @media screen and (min-width: 64em) {
  20. :host ::ng-deep .priority-3 {
  21. display: table-cell;
  22. }
  23. }
  24. `]
  25. })
  26. export class TreeTableResponsiveDemo {
  27. files: TreeNode[];
  28. cols: any[];
  29. constructor(private nodeService: NodeService) { }
  30. ngOnInit() {
  31. this.nodeService.getFilesystem().then(files => this.files = files);
  32. this.cols = [
  33. { field: 'name', header: 'Name' },
  34. { field: 'size', header: 'Size' },
  35. { field: 'type', header: 'Type' }
  36. ];
  37. }
  38. }
  39.  
  1. <p-treeTable [value]="files">
  2. <ng-template pTemplate="header">
  3. <tr>
  4. <th>Name</th>
  5. <th class="priority-2">Size</th>
  6. <th class="priority-3">Type</th>
  7. </tr>
  8. </ng-template>
  9. <ng-template pTemplate="body" let-rowNode let-rowData="rowData">
  10. <tr>
  11. <td>
  12. <p-treeTableToggler [rowNode]="rowNode"></p-treeTableToggler>
  13. {{rowData.name}}
  14. <span class="visibility-sm">
  15. / {{rowData.size}} - {{rowData.type}}
  16. </span>
  17. </td>
  18. <td class="priority-2">{{rowData.size}}</td>
  19. <td class="priority-3">{{rowData.type}}</td>
  20. </tr>
  21. </ng-template>
  22. </p-treeTable>
  23.  

See the live example.

EmptyMessage

When there is no data, emptymessage template can be used to display a message.

  1. <p-treeTable [value]="files">
  2. <ng-template pTemplate="header">
  3. <tr>
  4. <th>Name</th>
  5. <th>Size</th>
  6. <th>Type</th>
  7. </tr>
  8. </ng-template>
  9. <ng-template pTemplate="body" let-rowNode let-rowData="rowData">
  10. <tr>
  11. <td>
  12. <p-treeTableToggler [rowNode]="rowNode"></p-treeTableToggler>
  13. {{rowData.name}}
  14. </td>
  15. <td>{{rowData.size}}</td>
  16. <td>{{rowData.type}}</td>
  17. </tr>
  18. </ng-template>
  19. <ng-template pTemplate="emptymessage" let-columns>
  20. <tr>
  21. <td [attr.colspan]="columns.length">
  22. No records found
  23. </td>
  24. </tr>
  25. </ng-template>
  26. </p-treeTable>
  27.  

Loading Status

TreeTable has a loading property, when enabled a spinner icon is displayed to indicate data load. An optional loadingIcon property can be passed in case you'd like a different loading icon.

  1. <p-treeTable [value]="files" [columns]="cols" [loading]="loading">
  2. <ng-template pTemplate="header" let-columns>
  3. <tr>
  4. <th *ngFor="let col of columns">
  5. {{col.header}}
  6. </th>
  7. </tr>
  8. </ng-template>
  9. <ng-template pTemplate="body" let-rowNode let-rowData="rowData" let-columns="columns">
  10. <tr>
  11. <td *ngFor="let col of columns; let i = index">
  12. <p-treeTableToggler [rowNode]="rowNode" *ngIf="i == 0"></p-treeTableToggler>
  13. {{rowData[col.field]}}
  14. </td>
  15. </tr>
  16. </ng-template>
  17. </p-treeTable>
  18.  
  1. export class TreeTableDemo implements OnInit {
  2. files: TreeNode[];
  3. loading: boolean;
  4. cols: any[];
  5. constructor(private nodeService: NodeService) { }
  6. ngOnInit() {
  7. this.loading = true;
  8. this.nodeService.getFilesystem().then(files => {
  9. this.files = files;
  10. this.loading = false;
  11. {);
  12. this.cols = [
  13. { field: 'name', header: 'Name' },
  14. { field: 'size', header: 'Size' },
  15. { field: 'type', header: 'Type' }
  16. ];
  17. }
  18. }
  19.  

Styling Certain Rows and Columns

Certain rows and cells can easily be styled using templating features. In example below, the row whose vin property is '123' will get the 'success' style class. Example here paint the background of the last cell using a colgroup and highlights rows whose year is older than 2000.

  1. <p-treeTable [value]="files" [columns]="cols">
  2. <ng-template pTemplate="header" let-columns>
  3. <tr>
  4. <th *ngFor="let col of columns">
  5. {{col.header}}
  6. </th>
  7. </tr>
  8. </ng-template>
  9. <ng-template pTemplate="body" let-rowNode let-rowData="rowData" let-columns="columns">
  10. <tr [ngClass]="{'kb-row': rowData.size.endsWith('kb')}">
  11. <td *ngFor="let col of columns; let i = index" [ngClass]="{'kb-cell': col.field === 'size' && rowData.size.endsWith('kb')}">
  12. <p-treeTableToggler [rowNode]="rowNode" *ngIf="i == 0"></p-treeTableToggler>
  13. {{rowData[col.field]}}
  14. </td>
  15. </tr>
  16. </ng-template>
  17. </p-treeTable>
  18.  

See the live example.

Performance Tips

  • When selection is enabled use dataKey to avoid deep checking when comparing objects.
  • Use rowTrackBy to avoid unnecessary dom operations.
  • Prefer lazy loading techniques for large datasets.

Properties

NameTypeDefaultDescription
valuearraynullAn array of objects to display.
columnsarraynullAn array of objects to represent dynamic columns.
stylestringnullInline style of the component.
styleClassstringnullStyle class of the component.
autoLayoutbooleanfalseWhether the cell widths scale according to their content or not.
lazybooleanfalseDefines if data is loaded and interacted with in lazy manner.
paginatorbooleanfalseWhen specified as true, enables the pagination.
rowsnumbernullNumber of rows to display per page.
firstnumber0Index of the first row to be displayed.
totalRecordsnumbernullNumber of total records, defaults to length of value when not defined.
pageLinksnumbernullNumber of page links to display in paginator.
rowsPerPageOptionsarraynullArray of integer values to display inside rows per page dropdown of paginator
alwaysShowPaginatorbooleantrueWhether to show it even there is only one page.
paginatorPositionstringbottomPosition of the paginator, options are "top","bottom" or "both".
paginatorDropdownAppendToanynullTarget element to attach the paginator dropdown overlay, valid values are "body" or a local ng-template variable of another element.
defaultSortOrdernumber1Sort order to use when an unsorted column gets sorted by user interaction.
sortModestringsingleDefines whether sorting works on single column or on multiple columns.
resetPageOnSortbooleantrueWhen true, resets paginator to first page after sorting.
customSortbooleanfalseWhether to use the default sorting or a custom one using sortFunction.
sortFieldstringnullName of the field to sort data by default.
sortOrdernumber1Order to sort when default sorting is enabled.
multiSortMetaarraynullAn array of SortMeta objects to sort the data by default in multiple sort mode.
sortFunctionfunctionnullAn event emitter to invoke on custom sorting, refer to sorting section for details.
filtersarraynullAn array of FilterMetadata objects to provide external filters.
filterDelaynumber300Delay in milliseconds before filtering the data.
globalFilterFieldsarraynullAn array of fields as string to use in global filtering.
filterModestringlenientMode for filtering valid values are "lenient" and "strict". Default is lenient.
selectionModestringnullSpecifies the selection mode, valid values are "single" and "multiple".
selectionanynullSelected row in single mode or an array of values in multiple mode.
contextMenuSelectionanynullSelected row with a context menu.
dataKeystringnullA property to uniquely identify a record in data.
metaKeySelectionbooleantrueDefines whether metaKey is should be considered for the selection. On touch enabled devices, metaKeySelection is turned off automatically.
compareSelectionBystringdeepEqualsAlgorithm to define if a row is selected, valid values are "equals" that compares by reference and "deepEquals" that compares all fields.
rowHoverbooleanfalseAdds hover effect to rows without the need for selectionMode.
loadingbooleanfalseDisplays a loader to indicate data load is in progress.
loadingIconstringfa-circle-o-notchThe icon to show while indicating data load is in progress.
showLoaderbooleantrueWhether to show the loading mask when loading property is true.
scrollablebooleanfalseWhen specifies, enables horizontal and/or vertical scrolling.
scrollHeightstringnullHeight of the scroll viewport in fixed pixels, percentage or a calc expression.
virtualScrollbooleanfalseWhether the data should be loaded on demand during scroll.
virtualScrollDelaynumber150Delay in virtual scroll before doing a call to lazy load.
virtualRowHeightnumber28Height of a row to use in calculations of virtual scrolling.
frozenWidthstringnullWidth of the frozen columns container.
frozenColumnsarraynullAn array of objects to represent dynamic columns that are frozen.
resizableColumnsbooleanfalseWhen enabled, columns can be resized using drag and drop.
columnResizeModestringfitDefines whether the overall table width should change on column resize, valid values are "fit" and "expand".
reorderableColumnsbooleanfalseWhen enabled, columns can be reordered using drag and drop.
contextMenuContextMenunullLocal ng-template varilable of a ContextMenu.
rowTrackByFunctionnullFunction to optimize the dom operations by delegating to ngForTrackBy, default algoritm checks for object identity.

Events

event.filters: FilterMetadata object having field as key and filter value, filter matchMode as value event.globalFilter: Value of the global filter if available

NameParametersDescription
onNodeExpandevent.originalEvent: Browser event node: Expanded node.Callback to invoke when a node is expanded.
onNodeCollapseevent.originalEvent: Browser event node: Collapsed node.Callback to invoke when a node is collapsed.
onPageevent.first: Index of first record in page event.rows: Number of rows on the pageCallback to invoke when pagination occurs.
onSortevent.field: Field name of the sorted column event.order: Sort order as 1 or -1 event.multisortmeta: Sort metadata in multi sort mode. See multiple sorting section for the structure of this object.Callback to invoke when a column gets sorted.
onFilterevent.filters: Filters object having a field as the property key and an object with value, matchMode as the property value. event.filteredValue: Filtered data after running the filtering.Callback to invoke when data is filtered.
onLazyLoadevent.first = First row offset event.rows = Number of rows per page event.sortField = Field name to sort with event.sortOrder = Sort order as number, 1 for asc and -1 for dec event.multiSortMeta: An array of SortMeta objects used in multiple columns sorting. Each SortMeta has field and order properties.Callback to invoke when paging, sorting or filtering happens in lazy mode.
onColResizeevent.element: Resized column header event.delta: Change of width in number of pixelsCallback to invoke when a column is resized.
onColReorderevent.dragIndex: Index of the dragged column event.dropIndex: Index of the dropped column event.columns: Columns array after reorder.Callback to invoke when a column is reordered.
onNodeSelectevent.originalEvent: Browser event event.nıde: Selected node Callback to invoke when a node is selected.
onNodeUnselectevent.originalEvent: Browser event event.data: Unselected nodeCallback to invoke when a node is unselected.
onContextMenuSelectevent.originalEvent: Browser event event.node: Selected nodeCallback to invoke when a node is selected with right click.
onHeaderCheckboxToggleevent.originalEvent: Browser event event.checked: State of the header checkboxCallback to invoke when state of header checkbox changes.
onEditInitevent.column: Column object of the cell event.data: Node dataCallback to invoke when a cell switches to edit mode.
onEditCompleteevent.column: Column object of the cell event.data: Node dataCallback to invoke when cell edit is completed.
onEditCancelevent.column: Column object of the cell event.data: Node dataCallback to invoke when cell edit is cancelled with escape key.

Methods

NameParametersDescription
reset-Clears the sort and paginator state.

Styling

Following is the list of structural style classes, for theming classes visit theming page.

NameElement
ui-treetableContainer element.
ui-treetable-captionCaption element.
ui-treetable-summarySection section.
ui-sortable-columnSortable column header.
ui-treetable-scrollable-headerContainer of header in a scrollable table.
ui-treetable-scrollable-bodyContainer of body in a scrollable table.
ui-treetable-scrollable-footerContainer of footer in a scrollable table.
ui-treetable-loadingLoader mask.
ui-treetable-loading-contentLoader content.
ui-treetable-wrapperLoader content.
ui-treetable-scrollable-wrapperLoader content.
ui-treetable-resizer-helperVertical resize indicator bar.
ui-treetable-reorder-indicator-topTop indicator of column reordering.
ui-treetable-reorder-indicator-topBottom indicator of column reordering.

Dependencies

None.

Source

View on GitHub

  1. <h3 class="first">Basic</h3>
  2. <p-treeTable [value]="files1">
  3. <ng-template pTemplate="header">
  4. <tr>
  5. <th>Name</th>
  6. <th>Size</th>
  7. <th>Type</th>
  8. </tr>
  9. </ng-template>
  10. <ng-template pTemplate="body" let-rowNode let-rowData="rowData">
  11. <tr [ttRow]="rowNode">
  12. <td>
  13. <p-treeTableToggler [rowNode]="rowNode"></p-treeTableToggler>
  14. {{rowData.name}}
  15. </td>
  16. <td>{{rowData.size}}</td>
  17. <td>{{rowData.type}}</td>
  18. </tr>
  19. </ng-template>
  20. </p-treeTable>
  21. <h3>Dynamic Columns</h3>
  22. <p-treeTable [value]="files2" [columns]="cols">
  23. <ng-template pTemplate="header" let-columns>
  24. <tr [ttRow]="rowNode">
  25. <th *ngFor="let col of columns">
  26. {{col.header}}
  27. </th>
  28. </tr>
  29. </ng-template>
  30. <ng-template pTemplate="body" let-rowNode let-rowData="rowData" let-columns="columns">
  31. <tr>
  32. <td *ngFor="let col of columns; let i = index">
  33. <p-treeTableToggler [rowNode]="rowNode" *ngIf="i == 0"></p-treeTableToggler>
  34. {{rowData[col.field]}}
  35. </td>
  36. </tr>
  37. </ng-template>
  38. </p-treeTable>
  39.  
  1. export class TreeTableDemo implements OnInit {
  2. files1: TreeNode[];
  3. files2: TreeNode[];
  4. cols: any[];
  5. constructor(private nodeService: NodeService) { }
  6. ngOnInit() {
  7. this.nodeService.getFilesystem().then(files => this.files1 = files);
  8. this.nodeService.getFilesystem().then(files => this.files2 = files);
  9. this.cols = [
  10. { field: 'name', header: 'Name' },
  11. { field: 'size', header: 'Size' },
  12. { field: 'type', header: 'Type' }
  13. ];
  14. }
  15. }
  16.