fix autocomplete con workaround

This commit is contained in:
Flavio Bontà 2025-11-13 16:27:42 +01:00
parent 4c7998f349
commit 0e6560c66d
2 changed files with 39 additions and 10 deletions

View File

@ -31,23 +31,31 @@
optionLabel="descrizione" optionLabel="descrizione"
optionValue="codice" optionValue="codice"
[filter]="true"></p-dropdown> [filter]="true"></p-dropdown>
<!-- {{vm.filteredLuoghiEsteso | json}}
<p-autoComplete formControlName="luogo" {{cercaStruttureForm.controls['luogo'].value | json}} -->
<!-- <p-autocomplete [(ngModel)]="modelLuogo"
[ngModelOptions]="{ standalone: true }"
[suggestions]="vm.filteredLuoghiEsteso"
(completeMethod)="fetchLuogo($event, 1)"
optionLabel="tipo" /> -->
<p-autoComplete #acSt formControlName="luogo"
[suggestions]="vm.filteredLuoghiEsteso" [suggestions]="vm.filteredLuoghiEsteso"
(completeMethod)="fetchLuogo($event, 1)" (completeMethod)="fetchLuogo($event, 1)"
(onSelect)="state.set({ filteredLuoghiEsteso: [] })" (onSelect)="state.set({ filteredLuoghiEsteso: [] })"
(onHide)="checkLuogo()"
styleClass="w-full" styleClass="w-full"
inputStyleClass="w-full" inputStyleClass="w-full"
placeholder="Cerca per città, provincia, stato o regione" placeholder="Cerca per città, provincia, stato o regione"
appendTo="body" appendTo="body"
minLength="3" minLength="1"
delay="500" [delay]="500"
[forceSelection]="true" emptyMessage="Nessun luogo trovato"
[showClear]="false" [showClear]="true"
class="flex-auto lg:flex-1 lg:mt-0 w-full mr-0 lg:mr-1 text-surface-900 dark:text-surface-0 lg:w-72" class="flex-auto lg:flex-1 lg:mt-0 w-full mr-0 lg:mr-1 text-surface-900 dark:text-surface-0 lg:w-72"
field="luogo"> dataKey="dataKey"
optionLabel="luogo">
<ng-template let-luogo <ng-template let-luogo
pTemplate="item"> #item>
<div class="flex align-items-center"> <div class="flex align-items-center">
@if (luogo.tipo === 'comune') { @if (luogo.tipo === 'comune') {
<fa-icon class="fs-16 mr-3 text-color-secondary" <fa-icon class="fs-16 mr-3 text-color-secondary"

View File

@ -66,7 +66,7 @@ export type cercaStruttureFormValue = cercaStruttureForm['value'];
export interface CercaStruttureComponentState { export interface CercaStruttureComponentState {
strutture: StrutturePubblicheResDto[]; strutture: StrutturePubblicheResDto[];
tipologieStrutture: TipoStruttura[]; tipologieStrutture: TipoStruttura[];
filteredLuoghiEsteso: LuogoRes[]; filteredLuoghiEsteso: Array<LuogoRes & { dataKey: string }>;
cercaStruttureFormSubmitted: boolean; cercaStruttureFormSubmitted: boolean;
tipologieStruttureAreLoading: boolean; tipologieStruttureAreLoading: boolean;
@ -125,6 +125,8 @@ export class StrutturePubblicheComponent {
faHospital = faHospital; faHospital = faHospital;
struttureConCoords: StrutturePubblicheResDto[] = []; struttureConCoords: StrutturePubblicheResDto[] = [];
modelLuogo!: LuogoRes | null;
cols: TableColumn[] = [ cols: TableColumn[] = [
{ {
header: 'Strutture', header: 'Strutture',
@ -185,13 +187,23 @@ export class StrutturePubblicheComponent {
event: { originalEvent: Event; query: string }, event: { originalEvent: Event; query: string },
flagAttivo?: number, flagAttivo?: number,
) { ) {
if (!event.query || event.query.length < 3) {
this.state.set({ filteredLuoghiEsteso: [] });
return;
}
const fetchCities$ = this.strutturePubblicheService const fetchCities$ = this.strutturePubblicheService
.getLuoghiEsteso(event.query, flagAttivo) .getLuoghiEsteso(event.query, flagAttivo)
.pipe( .pipe(
catchError((err) => { catchError((err) => {
return of(null); return of(null);
}), }),
map((res) => ({ filteredLuoghiEsteso: res?.rows })), map((res) => ({
filteredLuoghiEsteso:
res?.rows.map((res) => ({
...res,
dataKey: res.tipo + ' → ' + res.luogo,
})) ?? [],
})),
); );
this.state.connect(fetchCities$); this.state.connect(fetchCities$);
} }
@ -422,4 +434,13 @@ export class StrutturePubblicheComponent {
url = `${url}&travelmode=${travelMode}`; url = `${url}&travelmode=${travelMode}`;
window.open(url, '_blank'); window.open(url, '_blank');
} }
checkLuogo() {
//Hack: rimosso dall'autocomplete forceSelection perchè non funzionava come previsto, selezionando ad esempio "Milano" (comune), selezionava "Milano" (comune).
const luogo = this.cercaStruttureForm.controls['luogo'].value;
console.log('luogo', luogo);
if (typeof luogo === 'string') {
this.cercaStruttureForm.controls['luogo'].setValue(null);
}
}
} }