Search code examples
angularlisthtml-listsexpandablelistview

Expand li with new elements in it when user clicks on it


I have a list with delete button on each li. When the user clicks on delete button then that li should expand and show a confirmation message and two buttons only for that li. Currently when i click on any delete button then the confirmation msg is shown for all li's

I have pasted my code in below stackblitz. https://stackblitz.com/edit/angular-oxnnhv

Thanks in Advance


Solution

  • Show variable is shared between all the items. So all the li ui is enabled. Use use index for the fix, like below

    <li fxLayout="column" class="field-list-item filters" *ngFor="let eachColumn of fieldViewList;let i = index"
                        (click)="fieldSelect(eachColumn)">
                        <div fxLayout="row">
                        <div class="title-container">
                            <span class="title">{{eachColumn}}</span>
                        </div>
                        <div class="delete-field hide" (click)="showDeleteConfirmation(i)">Del</div>
                        </div>
                        <div fxLayout="column" *ngIf="show === i" id="{{i}}">
                        <span> Please confirm if you want to delete this field view. This operations can not be undone. </span>
                        <div class="confirmation-button-container">
                            <button (click)="cancelConfirmation($event)">Cancel</button>
                            <button (click)="deleteField($event,eachColumn)">Delete</button>
                        </div>
                        </div>
                    </li>
    

    Component should be modified like this

    export class AppComponent  {
      name = 'Angular';
      fieldViewList = ['field1','field2','field3','field4','field5'];
       public show: number;
    
       deleteField(e, column) {
        e.stopPropagation();
        console.log('deleteField() : ', e);
        console.log('column : ', column);
    
      }
    
      showDeleteConfirmation(index: number) {
        console.log('showDeleteConfirmation');
        this.show = index;
      }
    
      cancelConfirmation(index: number) {
        console.log('cancelConfirmation');
        this.show = index;
      }
    
      fieldSelect(column) {
        console.log('FieldSelect() : ', column);
      }
    }