9 } from '@angular/core';
11 import * as _ from 'lodash';
13 import { CellTemplate } from '../../enum/cell-template.enum';
14 import { CdTableColumn } from '../../models/cd-table-column';
15 import { TableComponent } from '../table/table.component';
23 * Display the given data in a 2 column data table. The left column
24 * shows the 'key' attribute, the right column the 'value' attribute.
25 * The data table has the following characteristics:
26 * - No header and footer is displayed
27 * - The relation of the width for the columns 'key' and 'value' is 1:3
28 * - The 'key' column is displayed in bold text
31 selector: 'cd-table-key-value',
32 templateUrl: './table-key-value.component.html',
33 styleUrls: ['./table-key-value.component.scss']
35 export class TableKeyValueComponent implements OnInit, OnChanges {
36 @ViewChild(TableComponent)
37 table: TableComponent;
42 autoReload: any = 5000;
44 renderObjects = false;
45 // Only used if objects are rendered
47 appendParentKey = true;
51 // If set, the classAddingTpl is used to enable different css for different values
53 customCss?: { [css: string]: number | string | ((any) => boolean) };
55 columns: Array<CdTableColumn> = [];
59 * The function that will be called to update the input data.
62 fetchData = new EventEmitter();
71 cellTransformation: CellTemplate.bold
79 this.columns[1].cellTransformation = CellTemplate.classAdding;
81 // We need to subscribe the 'fetchData' event here and not in the
82 // HTML template, otherwise the data table will display the loading
83 // indicator infinitely if data is only bound via '[data]="xyz"'.
84 // See for 'loadingIndicator' in 'TableComponent::ngOnInit()'.
85 if (this.fetchData.observers.length > 0) {
86 this.table.fetchData.subscribe(() => {
87 // Forward event triggered by the 'cd-table' data table.
88 this.fetchData.emit();
94 ngOnChanges(changes) {
100 return; // Wait for data
102 this.tableData = this._makePairs(this.data);
105 _makePairs(data: any): Item[] {
108 return; // Wait for data
109 } else if (_.isArray(data)) {
110 temp = this._makePairsFromArray(data);
111 } else if (_.isObject(data)) {
112 temp = this._makePairsFromObject(data);
114 throw new Error('Wrong data format');
116 temp = temp.map((v) => this._convertValue(v)).filter((o) => o); // Filters out undefined
117 return _.sortBy(this.renderObjects ? this.insertFlattenObjects(temp) : temp, 'key');
120 _makePairsFromArray(data: any[]): Item[] {
122 const first = data[0];
123 if (_.isArray(first)) {
124 if (first.length === 2) {
125 temp = data.map((a) => ({
130 throw new Error('Wrong array format: [string, any][]');
132 } else if (_.isObject(first)) {
133 if (_.has(first, 'key') && _.has(first, 'value')) {
137 (previous: any[], item) => previous.concat(this._makePairsFromObject(item)),
145 _makePairsFromObject(data: object): Item[] {
146 return Object.keys(data).map((k) => ({
152 private insertFlattenObjects(temp: Item[]): any[] {
153 return _.flattenDeep(
155 const value = item.value;
156 const isObject = _.isObject(value);
157 if (!isObject || _.isEmpty(value)) {
163 return this.splitItemIntoItems(item);
169 * Split item into items will call _makePairs inside _makePairs (recursion), in oder to split
170 * the object item up into items as planned.
172 private splitItemIntoItems(v: { key: string; value: object }): Item[] {
173 return this._makePairs(v.value).map((item) => {
174 if (this.appendParentKey) {
175 item.key = v.key + ' ' + item.key;
181 _convertValue(v: Item): Item {
182 if (_.isArray(v.value)) {
183 v.value = v.value.map((item) => (_.isObject(item) ? JSON.stringify(item) : item)).join(', ');
185 const isEmpty = _.isEmpty(v.value) && !_.isNumber(v.value);
186 if ((this.hideEmpty && isEmpty) || (_.isObject(v.value) && !this.renderObjects)) {
188 } else if (isEmpty && !this.hideEmpty && v.value !== '') {