import { Component } from '@angular/core';
import { CdTableColumn } from '../../../shared/models/cd-table-column';
+import { CdTableSelection } from '../../../shared/models/cd-table-selection';
import { CephShortVersionPipe } from '../../../shared/pipes/ceph-short-version.pipe';
import { RgwDaemonService } from '../services/rgw-daemon.service';
});
}
- beforeShowDetails(selected: Array<object>) {
- return selected.length === 1;
+ beforeShowDetails(selection: CdTableSelection) {
+ return selection.hasSingleSelection;
}
}
import { Observable } from 'rxjs/Observable';
import { CdTableColumn } from '../../models/cd-table-column';
+import { CdTableSelection } from '../../models/cd-table-selection';
import { TableDetailsDirective } from '../table-details.directive';
@Component({
*/
@Output() fetchData = new EventEmitter();
+ /**
+ * Use this variable to access the selected row(s).
+ */
+ selection = new CdTableSelection();
+
cellTemplates: {
[key: string]: TemplateRef<any>
} = {};
selectionType: string = undefined;
search = '';
rows = [];
- selected = [];
loadingIndicator = true;
paginationClasses = {
pagerLeftArrow: 'i fa fa-angle-double-left',
this.updating = false;
}
+ onSelect() {
+ this.selection.update();
+ this.toggleExpandRow();
+ }
+
toggleExpandRow() {
- if (this.selected.length > 0) {
- this.table.rowDetail.toggleExpandRow(this.selected[0]);
+ if (this.selection.hasSelection) {
+ this.table.rowDetail.toggleExpandRow(this.selection.first());
} else {
this.detailTemplate.viewContainerRef.clear();
}
return;
}
if (_.isFunction(this.beforeShowDetails)) {
- if (!this.beforeShowDetails(this.selected)) {
+ if (!this.beforeShowDetails(this.selection)) {
return;
}
}
const cmpRef = this.detailTemplate.viewContainerRef.createComponent(
this.componentFactoryResolver.resolveComponentFactory(factoryClass)
);
- cmpRef.instance.selected = this.selected;
+ cmpRef.instance.selected = this.selection.selected;
}
updateFilter(event?) {
--- /dev/null
+export class CdTableSelection {
+ selected: any[] = [];
+ hasMultiSelection: boolean;
+ hasSingleSelection: boolean;
+ hasSelection: boolean;
+
+ constructor() {
+ this.update();
+ }
+
+ /**
+ * Recalculate the variables based on the current number
+ * of selected rows.
+ */
+ update() {
+ this.hasSelection = this.selected.length > 0;
+ this.hasSingleSelection = this.selected.length === 1;
+ this.hasMultiSelection = this.selected.length > 1;
+ }
+
+ first() {
+ return this.hasSelection ? this.selected[0] : null;
+ }
+}