I am using p-autoComplete in a table, once a row is selected my auto complete should preselect the current value..I tried using [(ngModel)]="row.bsaName" but it is not working. (Once I click on my dropwdown I see all the values, I confirmed the values do exist on my p-autoComplete dropdown)
also Note I used p-calendar which I was able to pre-select the current date once click on edit and not sure why auto complete is different
hTML
<p-table #dt [value]="iBsaFollowup" dataKey="id" [paginator]="true" [rowsPerPageOptions]="[10,50,100]"
[rows]="10">
<ng-template pTemplate="header">
<tr>
<th>ID</th>
<th>Followup DT</th>
<th>Comment</th>
<th>BSA Name</th>
<th>Action</th>
</tr>
</ng-template>
<ng-template pTemplate="body" let-row>
<tr>
<td>{{row.ID}}</td>
<td>
<div *ngIf="!row.isBSAEditable">{{row.followupDate}}</div>
<div *ngIf="row.isBSAEditable">
<p-calendar name="followupDate" [(ngModel)]="row.followupDate" [showIcon]="true"></p-calendar> <span style="margin-left:35px"></span>
<span *ngIf="isEmpty(row.comment)" style="color:crimson; font-size:8pt">Required</span>
</div>
</td>
<td>
<div *ngIf="!row.isBSAEditable">{{row.comment}}</div>
<div *ngIf="row.isBSAEditable">
<input type="text" [(ngModel)]="row.comment" style="width:95%" maxlength="200">
<span *ngIf="isEmpty(row.comment)" style="color:crimson; font-size:8pt">Required</span>
</div>
</td>
<td>
<div *ngIf="!row.isBSAEditable">{{row.bsaName}}</div>
<div *ngIf="row.isBSAEditable">
<p-autoComplete name="bsaNameList" [(ngModel)]="row.bsaName" [suggestions]="iBsaList" (completeMethod)="searchBsaList($event)" [style]="{'width':'85%'}" [inputStyle]="{'width':'85%'}" field="name" dataKey="id" [dropdown]="true"></p-autoComplete>
<span *ngIf="isEmpty(row.comment)" style="color:crimson; font-size:8pt">Required</span>
</div>
</td>
<td>
<div>
<modal [row]="row" [disableEditSaveButton]='isEmpty(row.comment)' (deleteRow)="onDeleteToDoList(row)" [showModal]="!row.isBSAEditable" (selectedRow)="onSelectedFollowupdRow(row)" (cancelEdit)="onCancelEdit(row)" (save)="onSave(row)"></modal>
</div>
<!--<button (click)="editRow(row)">Edit</button>-->
</td>
</tr>
</ng-template>
</p-table>
component
bsaListVal: IBsaList;
iBsaList: IBsaList[];
originalBsaList: IBsaList[];
searchBsaList(event) {
this.iBsaList = this.originalBsaList;
this.iBsaList = this.iBsaList
.filter(data => data.name.toString()
.toLowerCase()
.indexOf(event.query.toString().toLowerCase()) !== -1);
}
GetBsaList() {
this._dashboardService.getBSAList()
.subscribe(
data => {
this.iBsaList = data.result;
this.originalBsaList = data.result;
},
error => console.log('xx Method: ' + 'alert alert-danger'));
}
interface
export interface IBsaList {
id: string,
name: string
}
********************************************UPDATE********************************************** HTML
<p-autoComplete name="bsaNameList" [(ngModel)]="row.bsaName" [suggestions]="bsaNameArr" (completeMethod)="searchBsaList($event)" [style]="{'width':'85%'}" [inputStyle]="{'width':'85%'}" field="name" dataKey="id" [dropdown]="true"></p-autoComplete>
Component
bsaListVal: IBsaList;
iBsaList: IBsaList[];
originalBsaList: string[];
bsaNameArr: string[];
searchBsaList(event) {
this.bsaNameArr = this.originalBsaList;
this.bsaNameArr = this.bsaNameArr
.filter(data => data
.toLowerCase()
.indexOf(event.query.toString().toLowerCase()) !== -1);
}
GetBsaList() {
this._dashboardService.getBSAList()
.subscribe(
data => {
this.bsaNameArr = data.result.map(e => e.name);// data.result;
this.originalBsaList = data.result.map(e => e.name);
},
error => console.log('GetAllAccessFor Method: ' + 'alert alert-danger'));
}
row.bsaName
is probably astring
while theautoComplete
is populated withArray of Object
and you indicate the field to bename
. Binding does not work because of this. – Chau TranIBsaList
to juststring[]
. Something like:this.bsaNameArr = this.iBsaList.map(bsa => bsa.name)
then usebsaNameArr
as the[suggestions]
for your autoComplete – Chau Trandata.result
return? – Chau Tran