Skip to content

Commit a9718f6

Browse files
jhonyeduardojhosefmarks
authored andcommitted
feat(page-dynamic-table): inclui coluna de ordenação na requisição
Inclui coluna de ordenação nas requsições de paginação dos resultados. Caso for decrescente será enviada: order=-name Caso for ascendente será enviada: order=name Fixes DTHFUI-1743
1 parent f924060 commit a9718f6

File tree

6 files changed

+84
-7
lines changed

6 files changed

+84
-7
lines changed

projects/templates/src/lib/components/po-page-dynamic-table/po-page-dynamic-list-base.component.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,15 @@ export class PoPageDynamicListBaseComponent {
6565
*
6666
* Endpoint usado pelo template para requisição dos recursos que serão exibidos.
6767
*
68+
* Ao realizar requisições de busca, podem ser enviados os parâmetros (caso possuam valor): `page`, `pageSize`, `search` e `order`.
69+
*
70+
* Caso a coluna estiver ordenada descendentemente será enviada da seguinte forma: `-name`, se for ordenada
71+
* ascendentemente será enviada apenas com o nome da coluna, por exemplo: `name`.
72+
*
73+
* Exemplo de uma requisição de busca:
74+
*
75+
* > `GET {end-point}/{resource}?page=1&pageSize=10&search=components&order=-name`
76+
*
6877
* Caso a ação `remove` estiver configurada, será feito uma requisição de exclusão nesse mesmo endpoint passando os campos
6978
* setados como `key: true`.
7079
*

projects/templates/src/lib/components/po-page-dynamic-table/po-page-dynamic-table.component.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
[p-columns]="columns"
1515
[p-items]="items"
1616
[p-show-more-disabled]="!hasNext"
17-
(p-show-more)="showMore()">
17+
(p-show-more)="showMore()"
18+
(p-sort-by)="onSort($event)">
1819
</po-table>
1920

2021
</po-page-dynamic-search>

projects/templates/src/lib/components/po-page-dynamic-table/po-page-dynamic-table.component.spec.ts

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { RouterTestingModule } from '@angular/router/testing';
66

77
import { Observable } from 'rxjs';
88

9-
import { PoDialogModule, PoNotificationModule } from '@portinari/portinari-ui';
9+
import { PoDialogModule, PoNotificationModule, PoTableColumnSort, PoTableColumnSortType } from '@portinari/portinari-ui';
1010

1111
import * as utilsFunctions from '../../utils/util';
1212
import { configureTestSuite, expectPropertiesValues } from '../../util-test/util-expect.spec';
@@ -573,6 +573,45 @@ describe('PoPageDynamicTableComponent:', () => {
573573
expect(component.tableActions).toEqual(tableActions);
574574
});
575575

576+
it('getOrderParam: should return { order: `column.property` } of PoTableColumnSort object if sort type is Ascending', () => {
577+
const sortedColumn: PoTableColumnSort = { column: { property: 'name' }, type: PoTableColumnSortType.Ascending };
578+
const expectedValue = { order: 'name' };
579+
580+
const orderParam = component['getOrderParam'](sortedColumn);
581+
582+
expect(orderParam).toEqual(expectedValue);
583+
});
584+
585+
it('getOrderParam: should return { order: `-column.property` } of PoTableColumnSort object if sort type is Descending', () => {
586+
const sortedColumn: PoTableColumnSort = { column: { property: 'name' }, type: PoTableColumnSortType.Descending };
587+
const expectedValue = { order: '-name' };
588+
589+
const orderParam = component['getOrderParam'](sortedColumn);
590+
591+
expect(orderParam).toEqual(expectedValue);
592+
});
593+
594+
it('getOrderParam: should return {} if PoTableColumnSort.column is undefined', () => {
595+
const expectedValue = {};
596+
597+
component['sortedColumn'] = undefined;
598+
599+
const orderParam = component['getOrderParam']();
600+
601+
expect(orderParam).toEqual(expectedValue);
602+
});
603+
604+
it('onSort: should set sortedColumn property', () => {
605+
const sortedColumn = { column: { property: 'name' }, type: PoTableColumnSortType.Ascending };
606+
const expectedValue: PoTableColumnSort = { ...sortedColumn };
607+
608+
component['sortedColumn'] = undefined;
609+
610+
component.onSort(sortedColumn);
611+
612+
expect(component['sortedColumn']).toEqual(expectedValue);
613+
});
614+
576615
});
577616

578617
});

projects/templates/src/lib/components/po-page-dynamic-table/po-page-dynamic-table.component.ts

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
1-
import { Component, Input, OnInit } from '@angular/core';
21
import { ActivatedRoute, Route, Router } from '@angular/router';
2+
import { Component, Input, OnInit } from '@angular/core';
33

4-
import { PoDialogConfirmOptions, PoDialogService, PoNotificationService, PoPageAction, PoTableAction } from '@portinari/portinari-ui';
4+
import {
5+
PoDialogConfirmOptions,
6+
PoDialogService,
7+
PoNotificationService,
8+
PoPageAction,
9+
PoTableAction,
10+
PoTableColumnSort,
11+
PoTableColumnSortType
12+
} from '@portinari/portinari-ui';
513

614
import * as util from '../../utils/util';
715

@@ -93,6 +101,7 @@ export class PoPageDynamicTableComponent extends PoPageDynamicListBaseComponent
93101

94102
private page: number = 1;
95103
private params = {};
104+
private sortedColumn: PoTableColumnSort;
96105

97106
hasNext = false;
98107
items = [];
@@ -164,6 +173,10 @@ export class PoPageDynamicTableComponent extends PoPageDynamicListBaseComponent
164173
this.params = filter ? { search: filter } : {};
165174
}
166175

176+
onSort(sortedColumn: PoTableColumnSort) {
177+
this.sortedColumn = sortedColumn;
178+
}
179+
167180
showMore() {
168181
this.loadData({ page: ++this.page, ...this.params });
169182
}
@@ -206,14 +219,29 @@ export class PoPageDynamicTableComponent extends PoPageDynamicListBaseComponent
206219
return util.valuesFromObject(keys).join('|');
207220
}
208221

222+
private getOrderParam(sortedColumn: PoTableColumnSort = { type: undefined }) {
223+
const { column, type } = sortedColumn;
224+
225+
if (!column) {
226+
return {};
227+
}
228+
229+
if (type === PoTableColumnSortType.Descending) {
230+
return { order: `-${column.property}` };
231+
}
232+
233+
return { order: `${column.property}` };
234+
}
235+
209236
private loadData(params: { page?: number, search?: string } = {}) {
210237
if (!this.serviceApi) {
211238
this.poNotification.error(this.literals.loadDataErrorNotification);
212239
return;
213240
}
214241

242+
const orderParam = this.getOrderParam(this.sortedColumn);
215243
const defaultParams: any = { page: 1, pageSize: 10 };
216-
const fullParams: any = { ...defaultParams, ...params };
244+
const fullParams: any = { ...defaultParams, ...params, ...orderParam };
217245

218246
this.poPageDynamicService.getResources(fullParams).toPromise().then((response: any) => {
219247
this.items = fullParams.page === 1 ? response.items : [...this.items, ...response.items];
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
<po-page-dynamic-table
22
p-title="Po Page Dynamic Table"
3-
p-service-api="https://thf.totvs.com.br/sample/api/po-metadata/v1/people">
3+
p-service-api="https://thf.totvs.com.br/sample/api/thf-metadata/v1/people">
44
</po-page-dynamic-table>

projects/templates/src/lib/components/po-page-dynamic-table/samples/sample-po-page-dynamic-table-users/sample-po-page-dynamic-table-users.component.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { PoPageDynamicTableActions } from '@portinari/portinari-templates';
99
})
1010
export class SamplePoPageDynamicTableUsersComponent {
1111

12-
public readonly serviceApi = 'https://thf.totvs.com.br/sample/api/po-metadata/v1/people';
12+
public readonly serviceApi = 'https://thf.totvs.com.br/sample/api/thf-metadata/v1/people';
1313

1414
public readonly actions: PoPageDynamicTableActions = {
1515
detail: 'dynamic-detail/:id',

0 commit comments

Comments
 (0)