Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 

176 wiersze
6.9 KiB

  1. import {AfterViewInit, Component, Input, OnInit, ViewChild} from '@angular/core';
  2. import {MatSort, Sort} from "@angular/material/sort";
  3. import {PagingComponent} from "@app/_components/paging/paging.component";
  4. import {MatTableDataSource} from "@angular/material/table";
  5. import {ListColDefinition} from "@app/_components/list/list-col-definition";
  6. import {AppHelperService} from "@app/_helpers/app-helper.service";
  7. import {ListGetDataFunctionType} from "@app/_components/list/list-get-data-function-type";
  8. @Component({
  9. selector: 'app-list',
  10. templateUrl: './list.component.html',
  11. styleUrl: './list.component.scss'
  12. })
  13. export class ListComponent implements OnInit, AfterViewInit {
  14. @Input() public getDataFunction!: ListGetDataFunctionType;
  15. @Input() public onSortFunction!: Function;
  16. @Input() public onNavigateToDetailsFunction!: Function;
  17. @Input() public onRemoveItemFunction!: Function;
  18. @Input() public onEditFunction!: Function;
  19. @Input() public onDownloadFunction!: Function;
  20. @Input() public onRowSelectedFunction!: Function;
  21. @Input() public searchable: boolean;
  22. @Input() public showDetailButton: boolean;
  23. @Input() public showPosition: boolean;
  24. @Input() public listColDefinitions!: ListColDefinition[];
  25. @Input() public hidePageSize: boolean;
  26. @ViewChild(MatSort) sort;
  27. @ViewChild("pagingComponent", { static: false }) protected pagingComponent!: PagingComponent;
  28. public static COLUMN_TYPE_ADDRESS: string = 'address';
  29. public static COLUMN_TYPE_BTN_DOWNLOAD: string = 'btn_download';
  30. public static COLUMN_TYPE_BTN_EDIT: string = 'btn_edit';
  31. public static COLUMN_TYPE_BTN_REMOVE: string = 'btn_remove';
  32. public static COLUMN_TYPE_CURRENCY: string = 'euro';
  33. public static COLUMN_TYPE_DATE: string = 'date';
  34. public static COLUMN_TYPE_DETAIL: string = 'detail';
  35. public static COLUMN_TYPE_EMAIL: string = 'email';
  36. public static COLUMN_TYPE_IMAGE: string = 'image';
  37. public static COLUMN_TYPE_POSITION: string = 'position';
  38. public static COLUMN_TYPE_TEXT: string = 'text';
  39. public static COLUMN_TYPE_TEXT_LINKED: string = 'text_linked';
  40. public static COLUMN_TYPE_WEBSITE: string = 'website';
  41. get COLUMN_TYPE_ADDRESS(): string { return ListComponent.COLUMN_TYPE_ADDRESS; }
  42. get COLUMN_TYPE_BTN_DOWNLOAD(): string { return ListComponent.COLUMN_TYPE_BTN_DOWNLOAD; }
  43. get COLUMN_TYPE_BTN_EDIT(): string { return ListComponent.COLUMN_TYPE_BTN_EDIT; }
  44. get COLUMN_TYPE_BTN_REMOVE(): string { return ListComponent.COLUMN_TYPE_BTN_REMOVE; }
  45. get COLUMN_TYPE_CURRENCY(): string { return ListComponent.COLUMN_TYPE_CURRENCY; }
  46. get COLUMN_TYPE_DATE(): string { return ListComponent.COLUMN_TYPE_DATE; }
  47. get COLUMN_TYPE_DETAIL(): string { return ListComponent.COLUMN_TYPE_DETAIL; }
  48. get COLUMN_TYPE_EMAIL(): string { return ListComponent.COLUMN_TYPE_EMAIL; }
  49. get COLUMN_TYPE_POSITION(): string { return ListComponent.COLUMN_TYPE_POSITION; }
  50. get COLUMN_TYPE_IMAGE(): string { return ListComponent.COLUMN_TYPE_IMAGE; }
  51. get COLUMN_TYPE_TEXT(): string { return ListComponent.COLUMN_TYPE_TEXT; }
  52. get COLUMN_TYPE_TEXT_LINKED(): string { return ListComponent.COLUMN_TYPE_TEXT_LINKED; }
  53. get COLUMN_TYPE_WEBSITE(): string { return ListComponent.COLUMN_TYPE_WEBSITE; }
  54. protected displayedColumns!: string[];
  55. protected selectedRowIndex: number | null = null;
  56. protected dataSource;
  57. constructor(
  58. protected appHelperService: AppHelperService,
  59. ) {
  60. this.searchable = true;
  61. this.showDetailButton = true;
  62. this.showPosition = true;
  63. this.sort = new MatSort();
  64. this.hidePageSize = false;
  65. this.dataSource = new MatTableDataSource<any>();
  66. }
  67. ngOnInit(): void {
  68. if (this.showPosition) {
  69. this.listColDefinitions.unshift(ListComponent.getDefaultColPosition());
  70. }
  71. if (this.showDetailButton) {
  72. this.listColDefinitions.unshift(ListComponent.getDefaultColDetailBtn());
  73. }
  74. this.displayedColumns = [];
  75. this.listColDefinitions.forEach((value, index) => {
  76. this.displayedColumns.push(value.name);
  77. });
  78. }
  79. ngAfterViewInit(): void {
  80. }
  81. getData = (): void => {
  82. this.getDataFunction(
  83. this.pagingComponent.getPageIndex(),
  84. this.pagingComponent.getPageSize(),
  85. this.pagingComponent.getSearchValue()
  86. ).subscribe(
  87. data => {
  88. this.dataSource = new MatTableDataSource<any>(data['hydra:member']);
  89. this.pagingComponent.setDataLength(data["hydra:totalItems"]);
  90. }
  91. )
  92. }
  93. onSortChange = (sortState: Sort) => {
  94. this.pagingComponent.resetPageIndex();
  95. this.onSortFunction(sortState);
  96. this.getData();
  97. }
  98. onRowSelected(row: any, index: number) {
  99. this.selectedRowIndex = index;
  100. if (this.onRowSelectedFunction !== undefined) {
  101. this.onRowSelectedFunction(row, index);
  102. }
  103. }
  104. getElementValue(element: any, column: ListColDefinition): string | null {
  105. element = column.subResource !== undefined ? element[column.subResource]: element;
  106. if (element === undefined) {
  107. return null;
  108. }
  109. if (column.field !== undefined) {
  110. if (column.displayedLength !== undefined) {
  111. return element[column.field]?.slice(0, column.displayedLength) + '...';
  112. }
  113. return element[column.field];
  114. }
  115. if (column.address !== undefined) {
  116. const field = column.address;
  117. return `${element[field.street]} ${element[field.streetNo]}<br/> ${element[field.zip]} ${element[field.city]} <br/> ${element[field.country]}`;
  118. }
  119. return null;
  120. }
  121. getElementImage(element: any, column: ListColDefinition): any {
  122. let elementValue = this.getElementValue(element, column);
  123. if (elementValue !== undefined && elementValue !== null) {
  124. return elementValue;
  125. }
  126. return "/assets/images/icons/dummy-product.png"
  127. }
  128. getColCssClass(column: ListColDefinition): string {
  129. switch (column.type) {
  130. case ListComponent.COLUMN_TYPE_DETAIL:
  131. return "spt-button-td";
  132. case ListComponent.COLUMN_TYPE_BTN_REMOVE:
  133. return "spt-button-td text-end";
  134. default:
  135. return "";
  136. }
  137. }
  138. public getPageIndex() {
  139. return this.pagingComponent.getPageIndex();
  140. }
  141. public getPageSize() {
  142. return this.pagingComponent.getPageSize();
  143. }
  144. public static getDefaultColDetailBtn(): ListColDefinition {
  145. return {
  146. name: 'detail',
  147. text: 'overview.details',
  148. type: ListComponent.COLUMN_TYPE_DETAIL
  149. } as ListColDefinition;
  150. }
  151. public static getDefaultColPosition(): ListColDefinition {
  152. return {
  153. name: 'pos',
  154. text: 'overview.number',
  155. type: ListComponent.COLUMN_TYPE_POSITION
  156. } as ListColDefinition;
  157. }
  158. }