当用户单击li时,在其中添加新元素

时间:2018-10-15 04:49:06

标签: angular list html-lists expandablelistview

我在每个li上都有一个带有删除按钮的列表。当用户单击删除按钮时,那个li应该展开并显示确认消息和两个按钮,仅用于那个li。当前,当我单击任何删除按钮时,将显示所有li的确认消息

我将代码粘贴在stackblitz下面。 https://stackblitz.com/edit/angular-oxnnhv

预先感谢

3 个答案:

答案 0 :(得分:1)

请参见updated stackblitz。像这样对每个show使用item变量:

fieldViewList = [
  {
      title: 'field1',
      show: false
  },
  {
      title: 'field2',
      show: false
  },
  {
      title: 'field3',
      show: false
  },
  {
      title: 'field4',
      show: false
  },
  {
      title: 'field5',
      show: false
  }
];

这使您可以分别切换每个项目。

不要忘记像这样修改视图:*ngIf="eachColumn.show"{{eachColumn.title}},并使用组件中的column.show = true;访问列。

答案 1 :(得分:0)

显示变量在所有项目之间共享。因此,所有li ui均已启用。 使用使用索引进行修复,如下所示

<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>

应该像这样修改组件

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);
  }
}

答案 2 :(得分:0)

修改以下代码,它应该可以工作。现在,您可以指定要显示的项目,而不必设置显示。

  showDeleteConfirmation(e, eachColumn) {
    e.stopPropagation();
    console.log('showDeleteConfirmation');
    this.show = eachColumn;
  }
 <div fxLayout="column" *ngIf="show === eachColumn">