The cell that is currently been clicked should be the only cell opened and focus, and when I click other cell the previous one should close, right now I can open and click multiple sunsetDate cells at a time which is wrong.
How do we handle this in angular?
#screenshot
#html code
JavaScript
x
31
31
1
<td (click)="editStatus(data, 'sunsetDate' , i)"
2
[ngClass]="indexEditing==i && selectedCell === 'sunsetDate' ? 'editing-cell' : ''"
3
[ngClass]="!data.isRequired ? 'blank-cell' : ''" class="date-text-right">
4
<mat-form-field appearance="fill" *ngIf="indexEditing==i && selectedCell === 'sunsetDate'; else viewSunsetDate">
5
<mat-label>Choose a date</mat-label>
6
<input (blur)="blur()" [(ngModel)]="data.sunsetDate" (keyup.enter)="dateChange(data,'parent','sunsetDate')"
7
(keydown.tab)="dateChange(data,'parent','sunsetDate')" matInput (dateChange)="dateChange(data,'parent','sunsetDate')"
8
[matDatepicker]="picker" #inputField>
9
<mat-hint>MM/DD/YYYY</mat-hint>
10
<mat-datepicker-toggle matSuffix [for]="picker"></mat-datepicker-toggle>
11
<mat-datepicker #picker></mat-datepicker>
12
</mat-form-field>
13
<ng-template #viewSunsetDate>
14
<p> {{data.sunsetDate | date:'MM/dd/YYYY'}} </p>
15
</ng-template>
16
</td>
17
<td (click)="editStatus(data, 'status' , i)"
18
[ngClass]="{'editing-cell':indexEditing==i && selectedCell === 'status', 'blank-cell': !data.isRequired && !data.isHeaderCategory , 'uneditable-cell' : !data.isRequired && data.isHeaderCategory}"
19
style="text-align: center;">
20
<mat-form-field style="width: 100px; height: 30px;" appearance="fill"
21
*ngIf="indexEditing==i && selectedCell === 'status'; else viewStatus" autofocus>
22
<mat-select (blur)="blur()" (openedChange)="openedChange($event, data, 'parent' , 'status')" [(value)]="data.status">
23
<mat-option *ngFor="let status of statuses" [value]="status.viewValue"> {{status.viewValue}}
24
</mat-option>
25
</mat-select>
26
</mat-form-field>
27
<ng-template #viewStatus>
28
<p> {{data.status}} </p>
29
</ng-template>
30
</td>
31
#ts code
JavaScript
1
25
25
1
@ViewChild('editDatePickerInputCell') editDatePickerInputCell:any;
2
3
editDateTest(value: EntitlementTransactionTemplateDto, columnName: string) {
4
value.editing = false
5
value.editing = !value.editing
6
this.selectedCell = columnName
7
8
console.log('columnName' , columnName)
9
console.log('Value' , value)
10
11
12
if (value.editing) {
13
setTimeout(() => {
14
this.focusOnEditCell();
15
}, 0)
16
}
17
}
18
19
focusOnEditCell() {
20
setTimeout(()=>{
21
if(this.editDatePickerInputCell)
22
this.editDatePickerInputCell.nativeElement.focus();
23
},500);
24
}
25
#sample object being passsed when cell is clicked
JavaScript
1
10
10
1
{
2
"id": 99,
3
"transactionId": 13517,
4
"name": "",
5
"isHeaderCategory": false,
6
"isRequired": true,
7
"order": 3,
8
"editing": true
9
}
10
Advertisement
Answer
Instead use “data.editing” use a variable
JavaScript
1
2
1
indexEditing:number=-1;
2
So, you use:
JavaScript
1
6
1
<!--you check agains indexEditing-->
2
<mat-form-field *ngIf="indexEditing==index &&
3
selectedCell === 'sunsetDate'; else viewSunsetDate">
4
5
</mat-form-field>
6
In your function
JavaScript
1
5
1
editDateTest(value:EntitlementTransactionTemplateDto,index:number, columnName: string){
2
this.indexEditing=index;
3
4
}
5
When you end editing, simply
JavaScript
1
5
1
dateChange( ){
2
this.indexEditing=-1;
3
4
}
5
(*)If you has a *ngFor you simplly use
JavaScript
1
3
1
<ng-container *ngFor="let data of yourList;let index=index">
2
</ng-container>
3
If you’re using mat-table, see that you use
JavaScript
1
4
1
<td mat-cell *matCellDef="let element; let index = index;">
2
3
</td>
4