Skip to content

Commit

Permalink
refactor(igx-grid): cell selection; filtering performance improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
Lipata committed Apr 12, 2018
1 parent 485ff79 commit ec09e18
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 22 deletions.
28 changes: 14 additions & 14 deletions src/core/selection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,14 @@ export class IgxSelectionAPIService {
return selection;
}

public append_items(componentID: string, itemIDs: any[]): any[] {
let selection = this.get_selection(componentID);
if (selection === undefined) {
selection = [];
}
return [...selection, ...itemIDs];
}

public deselect_item(componentID: string, itemID, currSelection?: any[]) {
if (!currSelection) {
currSelection = this.get_selection(componentID);
Expand All @@ -53,9 +61,14 @@ export class IgxSelectionAPIService {
return selection;
}

public subtract_items(componentID: string, itemIDs: any[]) {
const selection = this.get_selection(componentID);
return selection.filter((selectedItemID) => itemIDs.indexOf(selectedItemID) === -1);
}

public is_item_selected(componentID: string, itemID) {
const selection = this.get_selection(componentID);
if (selection && selection.find((item) => this.compare(item, itemID)) !== undefined) {
if (selection && selection.find((item) => item === itemID) !== undefined) {
return true;
} else {
return false;
Expand All @@ -73,17 +86,4 @@ export class IgxSelectionAPIService {
public are_none_selected(componentID: string): boolean {
return this.get_selection_length(componentID) === 0;
}

public compare(item1, item2) {
if (item1 !== Object(item1)) {
return item1 === item2;
} else {
for (const prop in item1) {
if (item1[prop] !== item2[prop]) {
return false;
}
}
return true;
}
}
}
25 changes: 20 additions & 5 deletions src/grid/cell.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,10 +133,10 @@ export class IgxGridCellComponent implements IGridBus, OnInit {
@HostBinding("style.flex-basis")
@HostBinding("class.igx-grid__td--fw")
get width() {
const visibleCols = this.grid.visibleColumns;
const isLastVisibleColumn = visibleCols[visibleCols.length - 1].field === this.column.field;
const hasVerticalScroll = !this.grid.verticalScrollContainer.dc.instance.notVirtual;
return isLastVisibleColumn && hasVerticalScroll ? (parseInt(this.column.width, 10) - 18) + "px" : this.column.width;
const visibleCols = this.grid.visibleColumns;
const isLastVisibleColumn = visibleCols[visibleCols.length - 1].field === this.column.field;
const hasVerticalScroll = !this.grid.verticalScrollContainer.dc.instance.notVirtual;
return isLastVisibleColumn && hasVerticalScroll ? (parseInt(this.column.width, 10) - 18) + "px" : this.column.width;
}

@HostBinding("class.igx-grid__td--editing")
Expand Down Expand Up @@ -174,7 +174,7 @@ export class IgxGridCellComponent implements IGridBus, OnInit {
}

get selected() {
return this.isSelected = this.selectionApi.is_item_selected(this.cellSelectionID, this.cellID);
return this.isSelected = this.isCellSelected();
}

@HostBinding("attr.aria-selected")
Expand Down Expand Up @@ -214,6 +214,21 @@ export class IgxGridCellComponent implements IGridBus, OnInit {
}
this.selectionApi.set_selection(this.cellSelectionID, this.selectionApi.select_item(this.cellSelectionID, this.cellID));
}

public isCellSelected() {
const selection = this.selectionApi.get_selection(this.cellSelectionID);
if (selection) {
const selectedCellID = selection[0];
for (const prop in selectedCellID) {
if (selectedCellID[prop] !== this.cellID[prop]) {
return false;
}
}
return true;
}
return false;
}

@autoWire(true)
public ngOnInit() {
this.cellSelectionID = this.gridID + "-cells";
Expand Down
6 changes: 3 additions & 3 deletions src/grid/grid.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -889,17 +889,17 @@ export class IgxGridComponent implements OnInit, OnDestroy, AfterContentInit, Af
const newSelection =
event.checked ?
this.filteredData ?
this.selectionAPI.select_items(this.id, this.selectionAPI.get_all_ids(this._filteredData, this.primaryKey)) :
this.selectionAPI.append_items(this.id, this.selectionAPI.get_all_ids(this._filteredData, this.primaryKey)) :
this.selectionAPI.get_all_ids(this.data, this.primaryKey) :
this.filteredData ?
this.selectionAPI.deselect_items(this.id, this.selectionAPI.get_all_ids(this._filteredData, this.primaryKey)) :
this.selectionAPI.subtract_items(this.id, this.selectionAPI.get_all_ids(this._filteredData, this.primaryKey)) :
[];
this.triggerRowSelectionChange(newSelection, null, event, event.checked);
this.checkHeaderChecboxStatus(event.checked);
}

get headerCheckboxAriaLabel() {
return this._filteringExpressions ? "Select all filtered" : "Select all";
return this._filteringExpressions.length > 0 ? "Select all filtered" : "Select all";
}

public checkHeaderChecboxStatus(headerStatus?: boolean) {
Expand Down

0 comments on commit ec09e18

Please sign in to comment.