Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 
 

151 строка
5.4 KiB

  1. import {AfterViewInit, Component, ElementRef, EventEmitter, Input, OnInit, Output, ViewChild} from '@angular/core';
  2. import {FormGroup} from "@angular/forms";
  3. import {ListComponent} from "@app/_components/list/list.component";
  4. import {ListColDefinition} from "@app/_components/list/list-col-definition";
  5. import {Observable} from "rxjs";
  6. import {Sort} from "@angular/material/sort";
  7. import {FilterBarComponent} from "@app/_components/filter-bar/filter-bar.component";
  8. @Component({
  9. selector: 'app-search-select',
  10. templateUrl: './search-select.component.html',
  11. styleUrl: './search-select.component.scss'
  12. })
  13. export class SearchSelectComponent implements OnInit, AfterViewInit {
  14. @Input() public formId!: string;
  15. @Input() public resultField!: string;
  16. @Input() public formLabelLangKey!: string;
  17. @Input() public documentForm!: FormGroup;
  18. @Input() public documentFormField!: string;
  19. @Input() public getDataFunction!: (index: number, pageSize: number, term?: string) => Observable<any>;
  20. @Input() public onRowSelectedFunction!: Function;
  21. @Input() public dataSet!: any;
  22. @Input() public displayedDataField!: string;
  23. @Input() public displayedDataSubResource!: string;
  24. @Input() public listColDefinitions!: ListColDefinition[];
  25. @Input() public displayAsButton: boolean;
  26. @Input() public showHeader: boolean;
  27. @Output() rowSelected = new EventEmitter<any>();
  28. @ViewChild('paragraphRef', {static: false}) paragraphRef!: ElementRef;
  29. @ViewChild("listComponent", {static: false}) listComponent!: ListComponent;
  30. protected readonly SearchSelectComponent = SearchSelectComponent;
  31. protected selectedRowIndex: number | null = null;
  32. protected searchBoxOpen: boolean;
  33. protected searchBoxInitialized: boolean;
  34. protected searchBoxFilled: boolean;
  35. constructor() {
  36. this.searchBoxOpen = false;
  37. this.searchBoxInitialized = false;
  38. this.searchBoxFilled = false;
  39. this.displayAsButton = false;
  40. this.showHeader = false;
  41. }
  42. ngOnInit(): void {
  43. if (this.dataSet !== undefined) {
  44. this.searchBoxFilled = true;
  45. }
  46. }
  47. ngAfterViewInit(): void {
  48. if (this.dataSet !== undefined) {
  49. this.paragraphRef.nativeElement.textContent = this.dataSet[this.displayedDataField];
  50. }
  51. }
  52. onRowSelected = (row: any, index: number) => {
  53. if (this.onRowSelectedFunction !== undefined) {
  54. this.onRowSelectedFunction(row, index);
  55. } else {
  56. this.selectedRowIndex = index;
  57. const value = this.resultField !== undefined ? row[this.resultField] : row.id;
  58. this.documentForm.get(this.formId)?.setValue(value);
  59. if (this.displayedDataSubResource !== undefined) {
  60. this.paragraphRef.nativeElement.textContent = row[this.displayedDataSubResource][this.displayedDataField];
  61. } else {
  62. this.paragraphRef.nativeElement.textContent = row[this.displayedDataField];
  63. }
  64. this.searchBoxFilled = true;
  65. this.searchBoxOpen = false;
  66. }
  67. }
  68. openSearchBox() {
  69. this.searchBoxOpen = !this.searchBoxOpen;
  70. if (this.searchBoxOpen && !this.searchBoxInitialized) {
  71. this.listComponent.getData();
  72. this.searchBoxInitialized = true;
  73. }
  74. }
  75. clearSearch() {
  76. this.paragraphRef.nativeElement.textContent = '';
  77. this.searchBoxFilled = false;
  78. this.documentForm.get(this.formId)?.setValue(null);
  79. }
  80. public getPageIndex() {
  81. return this.listComponent.getPageIndex();
  82. }
  83. public getPageSize() {
  84. return this.listComponent.getPageSize();
  85. }
  86. onSortChange = (sortState: Sort) => {
  87. }
  88. public static getDefaultColDefZones(subResource?: string): ListColDefinition[] {
  89. return [
  90. {
  91. name: 'name',
  92. text: 'common.name',
  93. type: ListComponent.COLUMN_TYPE_TEXT,
  94. field: 'name',
  95. sortable: true,
  96. filterType: FilterBarComponent.FILTER_TYPE_TEXT,
  97. } as ListColDefinition,
  98. {
  99. name: 'createdAt',
  100. text: 'common.created_at',
  101. type: ListComponent.COLUMN_TYPE_DATE,
  102. field: 'createdAt',
  103. sortable: true,
  104. filterType: FilterBarComponent.FILTER_TYPE_DATE,
  105. } as ListColDefinition,
  106. ];
  107. }
  108. public static getDefaultColDefShippingCompanies(subResource?: string): ListColDefinition[] {
  109. return [
  110. {
  111. name: 'name',
  112. text: 'common.name',
  113. type: ListComponent.COLUMN_TYPE_TEXT,
  114. field: 'name',
  115. sortable: true,
  116. filterType: FilterBarComponent.FILTER_TYPE_TEXT,
  117. } as ListColDefinition,
  118. {
  119. name: 'code',
  120. text: 'common.code',
  121. type: ListComponent.COLUMN_TYPE_TEXT,
  122. field: 'code',
  123. sortable: true,
  124. filterType: FilterBarComponent.FILTER_TYPE_TEXT,
  125. } as ListColDefinition,
  126. {
  127. name: 'createdAt',
  128. text: 'common.created_at',
  129. type: ListComponent.COLUMN_TYPE_DATE,
  130. field: 'createdAt',
  131. sortable: true,
  132. filterType: FilterBarComponent.FILTER_TYPE_DATE,
  133. } as ListColDefinition,
  134. ];
  135. }
  136. }