]> git.apps.os.sepia.ceph.com Git - ceph-ci.git/commitdiff
mgr/dashboard: Add 'forceIdentified' attribute to datatable
authorVolker Theile <vtheile@suse.com>
Wed, 18 Apr 2018 10:41:50 +0000 (12:41 +0200)
committerVolker Theile <vtheile@suse.com>
Thu, 19 Apr 2018 07:23:58 +0000 (09:23 +0200)
Signed-off-by: Volker Theile <vtheile@suse.com>
src/pybind/mgr/dashboard/frontend/src/app/shared/datatable/table/table.component.spec.ts
src/pybind/mgr/dashboard/frontend/src/app/shared/datatable/table/table.component.ts

index 92f6ec905c376c6710d2675426ddbf0b61dd5a50..a3da13b2f4411b572edbdedce110a6ca85e9a405 100644 (file)
@@ -134,6 +134,15 @@ describe('TableComponent', () => {
     expect(component.rows.length).toBe(100);
   });
 
+  it('should force an identifier', () => {
+    component.identifier = 'x';
+    component.forceIdentifier = true;
+    component.ngOnInit();
+    expect(component.identifier).toBe('x');
+    expect(component.sorts[0].prop).toBe('a');
+    expect(component.sorts).toEqual(component.createSortingDefinition('a'));
+  });
+
   describe('after ngInit', () => {
     const toggleColumn = (prop, checked) => {
       component.toggleColumn({
index 4e76195e0e178e011ffb5efca25306416ac8749a..7264799099a0eaf8b031efb0543c64e1c6b7264e 100644 (file)
@@ -9,7 +9,6 @@ import {
   OnInit,
   Output,
   TemplateRef,
-  Type,
   ViewChild
 } from '@angular/core';
 import {
@@ -64,12 +63,15 @@ export class TableComponent implements AfterContentChecked, OnInit, OnChanges, O
    */
   @Input() autoReload: any = 5000;
 
-  // Which row property is unique for a row
+  // Which row property is unique for a row. If the identifier is not specified in any
+  // column, then the property name of the first column is used. Defaults to 'id'.
   @Input() identifier = 'id';
+  // If 'true', then the specified identifier is used anyway, although it is not specified
+  // in any column. Defaults to 'false'.
+  @Input() forceIdentifier = false;
   // Allows other components to specify which type of selection they want,
   // e.g. 'single' or 'multi'.
   @Input() selectionType: string = undefined;
-
   // If `true` selected item details will be updated on table refresh
   @Input() updateSelectionOnRefresh = true;
 
@@ -124,10 +126,17 @@ export class TableComponent implements AfterContentChecked, OnInit, OnChanges, O
   ngOnInit() {
     this._addTemplates();
     if (!this.sorts) {
-      this.identifier = this.columns.some(c => c.prop === this.identifier) ?
-        this.identifier :
-        this.columns[0].prop + '';
-      this.sorts = this.createSortingDefinition(this.identifier);
+      // Check whether the specified identifier exists.
+      const exists = _.findIndex(this.columns, ['prop', this.identifier]) !== -1;
+      // Auto-build the sorting configuration. If the specified identifier doesn't exist,
+      // then use the property of the first column.
+      this.sorts = this.createSortingDefinition(exists ?
+        this.identifier : this.columns[0].prop + '');
+      // If the specified identifier doesn't exist and it is not forced to use it anyway,
+      // then use the property of the first column.
+      if (!exists && !this.forceIdentifier) {
+        this.identifier = this.columns[0].prop + '';
+      }
     }
     this.columns.map(c => {
       if (c.cellTransformation) {