Skip to content

Commit 4dbd51a

Browse files
Rafaellysamir-ayoub
authored andcommitted
feat(table): adiciona evento para ordenação de colunas.
Definido novo evento `p-sort-by` para controlar a ordenação e adicionado parâmetro de ordenação ao evento `p-show-more`. Fixes DTHFUI-1380
1 parent 1b7444c commit 4dbd51a

File tree

9 files changed

+334
-126
lines changed

9 files changed

+334
-126
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/**
2+
* @usedBy PoTableComponent
3+
*
4+
* @description
5+
* Tipos de ordenação das colunas da tabela.
6+
*/
7+
export enum PoTableColumnSortType {
8+
9+
/** Ordenação ascendente */
10+
Ascending = 'ascending',
11+
12+
/** Ordenação descendente */
13+
Descending = 'descending'
14+
}

projects/ui/src/lib/components/po-table/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
export * from './enums/po-table-column-sort-type.enum';
12
export * from './interfaces/po-table-action.interface';
23
export * from './interfaces/po-table-boolean.interface';
34
export * from './interfaces/po-table-column.interface';
5+
export * from './interfaces/po-table-column-sort.interface';
46
export * from './interfaces/po-table-literals.interface';
57
export * from './po-table-column-icon/po-table-column-icon.interface';
68
export * from './po-table-column-label/po-table-column-label.interface';
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { PoTableColumn } from './po-table-column.interface';
2+
import { PoTableColumnSortType } from '../enums/po-table-column-sort-type.enum';
3+
4+
/**
5+
* @usedBy PoTableComponent
6+
*
7+
* @description
8+
*
9+
* Interface para ordenação das colunas do componente table.
10+
*/
11+
export interface PoTableColumnSort {
12+
13+
/** Coluna pela qual a tabela está ordenada. */
14+
column?: PoTableColumn;
15+
16+
/** Tipo da ordenação. */
17+
type: PoTableColumnSortType;
18+
}

projects/ui/src/lib/components/po-table/po-table-base.component.spec.ts

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { Observable } from 'rxjs';
2+
13
import * as utilsFunctions from '../../utils/util';
24
import { expectPropertiesValues, expectSettersMethod } from '../../util-test/util-expect.spec';
35
import { PoDateService } from '../../services/po-date/po-date.service';
@@ -827,6 +829,56 @@ describe('PoTableBaseComponent:', () => {
827829
expect(column).toEqual([]);
828830
});
829831

832+
it(`sortColumn: should emit sortBy twice toggling the object parameter value between
833+
'ascending', 'descending' and not call 'sortArray'`, () => {
834+
const column = component.columns[1];
835+
component.sort = true;
836+
component.sortBy.observers = <any>[{ next: () => { }}];
837+
838+
spyOn(component.sortBy, 'emit').and.callThrough();
839+
spyOn(component, 'sortArray').and.callThrough();
840+
841+
component.sortColumn(column);
842+
expect(component.sortBy.emit).toHaveBeenCalledWith({column, type: 'ascending'});
843+
expect(component.sortArray).not.toHaveBeenCalled();
844+
845+
component.sortColumn(column);
846+
expect(component.sortBy.emit).toHaveBeenCalledWith({column, type: 'descending'});
847+
expect(component.sortArray).not.toHaveBeenCalled();
848+
});
849+
850+
it(`onShowMore: 'showMore' should emit an object parameter containing 'ascending' as value of property 'type'` , () => {
851+
const column = component.columns[1];
852+
component.sortedColumn.property = column;
853+
spyOn(component.showMore, 'emit');
854+
855+
component.onShowMore();
856+
857+
expect(component.showMore.emit).toHaveBeenCalledWith({ column: component.sortedColumn.property, type: 'ascending'});
858+
});
859+
860+
it(`onShowMore: 'showMore' should emit an object parameter containing 'descending' as value of property 'type'` , () => {
861+
const column = component.columns[1];
862+
component.sortedColumn.property = column;
863+
component.sortedColumn.ascending = false;
864+
865+
spyOn(component.showMore, 'emit');
866+
867+
component.onShowMore();
868+
869+
expect(component.showMore.emit).toHaveBeenCalledWith({ column: component.sortedColumn.property, type: 'descending'});
870+
});
871+
872+
it(`onShowMore: 'showMore' should emit an object parameter containing 'undefined' if 'sortedColumn.property' is 'undefined'` , () => {
873+
component.sortedColumn.property = undefined;
874+
spyOnProperty(component, <any>'isSortBy');
875+
876+
spyOn(component.showMore, 'emit');
877+
878+
component.onShowMore();
879+
880+
expect(component.showMore.emit).toHaveBeenCalledWith(undefined);
881+
});
830882
});
831883

832884
describe('Properties:', () => {
@@ -950,6 +1002,30 @@ describe('PoTableBaseComponent:', () => {
9501002
expect(component['showContainer']).toHaveBeenCalled();
9511003
});
9521004

1005+
it('isSortBy: should return `true` if `observers.length` is greater than 0.', () => {
1006+
component.sortBy.observers.length = 1;
1007+
1008+
expect(component['isSortBy']).toBe(true);
1009+
});
1010+
1011+
it('isSortBy: should return `false` if `observers.length` is 0.', () => {
1012+
component.sortBy.observers.length = 0;
1013+
1014+
expect(component['isSortBy']).toBe(false);
1015+
});
1016+
1017+
it('sortType: should return `ascending` if `sortedColumn.ascending` is `true`.', () => {
1018+
component.sortedColumn.ascending = true;
1019+
1020+
expect(component['sortType']).toBe('ascending');
1021+
});
1022+
1023+
it('sortType: should return `descending` if `sortedColumn.ascending` is `false`.', () => {
1024+
component.sortedColumn.ascending = false;
1025+
1026+
expect(component['sortType']).toBe('descending');
1027+
});
1028+
9531029
});
9541030

9551031
});

projects/ui/src/lib/components/po-table/po-table-base.component.ts

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import { PoDateService } from '../../services/po-date/po-date.service';
55

66
import { PoTableAction } from './interfaces/po-table-action.interface';
77
import { PoTableColumn } from './interfaces/po-table-column.interface';
8+
import { PoTableColumnSort } from './interfaces/po-table-column-sort.interface';
9+
import { PoTableColumnSortType } from './enums/po-table-column-sort-type.enum';
810
import { PoTableLiterals } from './interfaces/po-table-literals.interface';
911

1012
export const poTableContainer = ['border', 'shadow'];
@@ -383,8 +385,23 @@ export abstract class PoTableBaseComponent implements OnChanges {
383385
/**
384386
* Recebe uma ação de clique para o botão "Carregar mais resultados", caso nenhuma ação for definida o mesmo
385387
* não é visível.
388+
*
389+
* Recebe um objeto `{ column, type }` onde:
390+
*
391+
* - column (`PoTableColumn`): objeto da coluna que está ordenada.
392+
* - type (`PoTableColumnSortType`): tipo da ordenação.
393+
*/
394+
@Output('p-show-more') showMore?: EventEmitter<PoTableColumnSort> = new EventEmitter<PoTableColumnSort>();
395+
396+
/**
397+
* Ação executada ao ordenar colunas da tabela. Se houver ação definida, a ordenação padrão da tabela não será executada.
398+
*
399+
* Recebe um objeto `{ column, type }` onde:
400+
*
401+
* - column (`PoTableColumn`): objeto da coluna que foi clicada/ordenada.
402+
* - type (`PoTableColumnSortType`): tipo da ordenação.
386403
*/
387-
@Output('p-show-more') showMore?: EventEmitter<any> = new EventEmitter<any>();
404+
@Output('p-sort-by') sortBy?: EventEmitter<PoTableColumnSort> = new EventEmitter<PoTableColumnSort>();
388405

389406
/**
390407
* Ação executada ao desmarcar a seleção de uma linha do `po-table`.
@@ -394,6 +411,14 @@ export abstract class PoTableBaseComponent implements OnChanges {
394411
selectAll = false;
395412
sortedColumn = { property: <PoTableColumn>null, ascending: true };
396413

414+
private get isSortBy(): boolean {
415+
return this.sortBy.observers.length > 0;
416+
}
417+
418+
private get sortType(): PoTableColumnSortType {
419+
return this.sortedColumn.ascending ? PoTableColumnSortType.Ascending : PoTableColumnSortType.Descending;
420+
}
421+
397422
constructor(private poDate: PoDateService) { }
398423

399424
ngOnChanges(): void {
@@ -511,7 +536,8 @@ export abstract class PoTableBaseComponent implements OnChanges {
511536

512537
this.sortedColumn.ascending = this.sortedColumn.property === column ? !this.sortedColumn.ascending : true;
513538

514-
this.sortArray(column, this.sortedColumn.ascending);
539+
this.isSortBy ? this.sortBy.emit({ column, type: this.sortType}) : this.sortArray(column, this.sortedColumn.ascending);
540+
515541
this.sortedColumn.property = column;
516542
}
517543

@@ -530,7 +556,9 @@ export abstract class PoTableBaseComponent implements OnChanges {
530556
}
531557

532558
onShowMore(): void {
533-
this.showMore.emit(null);
559+
const sort = this.sortedColumn.property ? { column: this.sortedColumn.property, type: this.sortType } : undefined;
560+
561+
this.showMore.emit(sort);
534562
}
535563

536564
protected getDefaultColumns(item: any) {

projects/ui/src/lib/components/po-table/po-table.component.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ import { PoTableSubtitleColumn } from './po-table-subtitle-footer/po-table-subti
4949
* <example name="po-table-components" title="Portinari Table - Po Field Components">
5050
* <file name="sample-po-table-components/sample-po-table-components.component.ts"> </file>
5151
* <file name="sample-po-table-components/sample-po-table-components.component.html"> </file>
52+
* <file name="sample-po-table-components/sample-po-table-components.service.ts"> </file>
5253
* </example>
5354
*/
5455
@Component({

projects/ui/src/lib/components/po-table/samples/sample-po-table-components/sample-po-table-components.component.html

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@
55
<po-table
66
p-container="shadow"
77
[p-columns]="columns"
8-
[p-items]="items">
8+
[p-items]="items"
9+
[p-show-more-disabled]="showMoreDisabled"
10+
[p-sort]="true"
11+
(p-show-more)="showMore($event)"
12+
(p-sort-by)="sort($event)">
913
</po-table>
1014

1115
<po-modal

0 commit comments

Comments
 (0)