1 import { Component, EventEmitter, Input, OnChanges, OnInit, Output } from '@angular/core';
3 import * as _ from 'lodash';
5 import { CellTemplate } from '../../enum/cell-template.enum';
6 import { CdTableColumn } from '../../models/cd-table-column';
9 * Display the given data in a 2 column data table. The left column
10 * shows the 'key' attribute, the right column the 'value' attribute.
11 * The data table has the following characteristics:
12 * - No header and footer is displayed
13 * - The relation of the width for the columns 'key' and 'value' is 1:3
14 * - The 'key' column is displayed in bold text
17 selector: 'cd-table-key-value',
18 templateUrl: './table-key-value.component.html',
19 styleUrls: ['./table-key-value.component.scss']
21 export class TableKeyValueComponent implements OnInit, OnChanges {
23 columns: Array<CdTableColumn> = [];
26 @Input() autoReload: any = 5000;
34 * The function that will be called to update the input data.
36 @Output() fetchData = new EventEmitter();
45 cellTransformation: CellTemplate.bold
55 ngOnChanges(changes) {
62 return; // Wait for data
63 } else if (_.isArray(this.data)) {
64 const first = this.data[0];
65 if (_.isPlainObject(first) && _.has(first, 'key') && _.has(first, 'value')) {
66 temp = [...this.data];
68 if (_.isArray(first)) {
69 if (first.length === 2) {
70 temp = this.data.map(a => ({
75 throw new Error('Wrong array format');
79 } else if (_.isPlainObject(this.data)) {
80 temp = Object.keys(this.data).map(k => ({
85 throw new Error('Wrong data format');
87 this.tableData = temp.map(o => {
88 if (_.isArray(o.value)) {
89 o.value = o.value.join(', ');
90 } else if (_.isObject(o.value)) {
94 }).filter(o => o); // Filters out undefined
98 // Forward event triggered by the 'cd-table' datatable.
99 this.fetchData.emit();