Skip to content

Commit f0d03db

Browse files
authored
Merge pull request swimlane#553 from deeg/sort-fix
fix: show first page after sorting
2 parents c143a79 + 7ae0b1b commit f0d03db

File tree

2 files changed

+35
-7
lines changed

2 files changed

+35
-7
lines changed

src/components/datatable.component.spec.ts

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ describe('Datatable component', () => {
55

66
beforeEach(() => {
77
TestBed.configureTestingModule({
8-
imports: [ NgxDatatableModule ]
8+
imports: [NgxDatatableModule]
99
});
1010
});
1111

@@ -18,9 +18,9 @@ describe('Datatable component', () => {
1818
it('should return a new array', () => {
1919
let fixture = TestBed.createComponent(DatatableComponent);
2020
let initialRows = [
21-
{ id: 1 },
22-
{ id: 2 },
23-
{ id: 3 }
21+
{id: 1},
22+
{id: 2},
23+
{id: 3}
2424
];
2525

2626
let columns = [
@@ -41,16 +41,42 @@ describe('Datatable component', () => {
4141
expect(fixture.componentInstance.rows).toBe(initialRows);
4242

4343
fixture.componentInstance.onColumnSort({
44-
sorts: [{ prop: 'foo', dir: 'desc' }]
44+
sorts: [{prop: 'foo', dir: 'desc'}]
4545
});
4646

4747
fixture.componentInstance.sort
4848
.subscribe(() => {
49-
console.log('sorted event');
5049
});
5150

5251
expect(fixture.componentInstance.rows).not.toBe(initialRows);
5352
});
5453
});
5554

55+
it('should set offset to 0 when sorting by a column', () => {
56+
let fixture = TestBed.createComponent(DatatableComponent);
57+
let initialRows = [
58+
{id: 1},
59+
{id: 2},
60+
{id: 3}
61+
];
62+
63+
let columns = [
64+
{
65+
prop: 'id'
66+
}
67+
];
68+
69+
fixture.componentInstance.rows = initialRows;
70+
fixture.componentInstance.columns = columns;
71+
fixture.componentInstance.offset = 1;
72+
73+
fixture.detectChanges();
74+
75+
fixture.componentInstance.onColumnSort({
76+
sorts: [{prop: 'id', dir: 'desc'}]
77+
});
78+
79+
expect(fixture.componentInstance.offset).toBe(0);
80+
});
81+
5682
});

src/components/datatable.component.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -964,7 +964,9 @@ export class DatatableComponent implements OnInit, AfterViewInit, DoCheck {
964964
}
965965

966966
this.sorts = sorts;
967-
this.bodyComponent.updateOffsetY(0);
967+
// Always go to first page when sorting to see the newly sorted data
968+
this.offset = 0;
969+
this.bodyComponent.updateOffsetY(this.offset);
968970
this.sort.emit(event);
969971
}
970972

0 commit comments

Comments
 (0)