Skip to content

Commit

Permalink
fix(stark-ui): fix table refresh issue when data changes.
Browse files Browse the repository at this point in the history
ISSUES CLOSED: #1003
  • Loading branch information
SuperITMan authored and ageorges-nbb committed Jan 6, 2019
1 parent 149ac2d commit dd2eac0
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -138,28 +138,16 @@ describe("TableComponent", () => {
});

describe("on change", () => {
it("should trigger resetFilterValueOnDataChange and applyFilter methods when data changes", () => {
it("should trigger resetFilterValueOnDataChange and sortData methods when data changes", () => {
spyOn(component, "resetFilterValueOnDataChange");
spyOn(component, "applyFilter");
spyOn(component, "sortData");

(<Spy>component.resetFilterValueOnDataChange).and.returnValue(false);
(<Spy>component.resetFilterValueOnDataChange).calls.reset();
(<Spy>component.sortData).calls.reset();
hostComponent.dummyData = [{ name: "test-data" }];
hostFixture.detectChanges();

(<Spy>component.resetFilterValueOnDataChange).and.returnValue(false);
(<Spy>component.resetFilterValueOnDataChange).calls.reset();
hostComponent.dummyData = [{ name: "test-data-2" }];
hostFixture.detectChanges();
expect(component.resetFilterValueOnDataChange).toHaveBeenCalledTimes(1);
expect(component.applyFilter).not.toHaveBeenCalled();

(<Spy>component.resetFilterValueOnDataChange).and.returnValue(true);
(<Spy>component.resetFilterValueOnDataChange).calls.reset();
hostComponent.dummyData = [{ name: "test-data-1" }];
hostFixture.detectChanges();
expect(component.resetFilterValueOnDataChange).toHaveBeenCalledTimes(1);
expect(component.applyFilter).toHaveBeenCalledTimes(1);
expect(component.sortData).toHaveBeenCalledTimes(1);
});

it("should assign right value to isFixedHeaderEnabled when fixedHeader changes", () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -341,8 +341,13 @@ export class StarkTableComponent extends AbstractStarkUiComponent implements OnI
*/
// tslint:disable-next-line:cognitive-complexity
public ngOnChanges(changes: SimpleChanges): void {
if (changes["data"] && !changes["data"].isFirstChange() && this.resetFilterValueOnDataChange()) {
this.applyFilter();
if (changes["data"] && !changes["data"].isFirstChange()) {
this.resetFilterValueOnDataChange();

// When data changes, we should sort it again in any case because:
// * orderProperties could contain some sort rules
// * every column could have a sort rule defined
this.sortData();
}

if (changes["orderProperties"] && !changes["orderProperties"].isFirstChange()) {
Expand Down Expand Up @@ -644,6 +649,8 @@ export class StarkTableComponent extends AbstractStarkUiComponent implements OnI
.filter((columnToFilter: StarkTableColumnComponent) => columnToFilter.sortDirection)
.sort((column1: StarkTableColumnComponent, column2: StarkTableColumnComponent) => column1.sortPriority - column2.sortPriority);

// FIXME If "multiSort" is empty or equal to "true", isMultiSorting is true. Otherwise, "isMultisorting" should stay false.
// Should remove this condition ?
this.isMultiSorting = sortableColumns.length > 1;

this.data.sort((row1: object, row2: object) => {
Expand Down

0 comments on commit dd2eac0

Please sign in to comment.