HTML隐藏按钮隐藏所有按钮而不是一个

时间:2018-08-09 19:08:25

标签: javascript html angular typescript hide

我试图在单击时隐藏一个按钮。

component.ts:

import { Component, Input, OnInit, ChangeDetectionStrategy, ChangeDetectorRef } from '@angular/core';
import { DataService } from '../../shared/service/data.service';
import { TreeNode } from '../../shared/dto/TreeNode';

import html from './rightside.component.html';
import css from './rightside.component.css';

@Component({
  selector: 'rightside-component',
  template: html,
  providers: [DataService],
  styles: [css],
  changeDetection: ChangeDetectionStrategy.OnPush
})

export class RightSideComponent implements OnInit {
  selections: string[];
  @Input() treeNode: TreeNode<string>[];
  hide: boolean = false;

  constructor(private cd: ChangeDetectorRef) {}

  ngOnInit() {
  }

  getSelections() : TreeNode<string>[] {
    if (typeof(this.treeNode) == "undefined" || (this.treeNode) === null) {
      return [];
    }
    return this.treeNode;
  }

  deselect(item: TreeNode<string>): void {
    this.hide = true;
    if((item.children) !== null) {
      item.children.forEach(element => {
        this.deselect(element);
      });
    }
    item.selected = false;
  }

}

component.html:

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<div>
  <ul class="selection-list">
    <li *ngFor="let item of getSelections()">
      <button class="btn" (click)="deselect(item)" *ngIf="!hide">
        <i class="fa fa-close"> {{ item.displayName }} </i>
      </button> 
    </li>
  </ul>
</div>

当我单击任何按钮时,所有项目都消失了。我只希望单击的项目消失。当我再次选择该复选框时,该项目应重新出现。我想实现与我发现的这个plunkr类似的东西,但是对于我的数据结构:

http://next.plnkr.co/edit/1Fr83XHkY0bWd9IzOwuT?p=preview&utm_source=legacy&utm_medium=worker&utm_campaign=next&preview

我该如何解决?让我知道是否需要其他代码。

3 个答案:

答案 0 :(得分:1)

在每个项目中都使用hide属性,而不是使用公用的selected变量,因为取消选择时会将其设为false。

<button class="btn" (click)="deselect(item)" *ngIf="item.selected">
   <i class="fa fa-close"> {{ item.displayName }} </i>
</button> 

答案 1 :(得分:0)

尝试一下

  hide= [];

    <div>
      <ul class="selection-list">
        <li *ngFor="let item of getSelections();let i=index">
          <button class="btn" (click)="deselect(item: 
           TreeNode<string>);hide[i]=!hide[i]" *ngIf="hide[i]">
            <i class="fa fa-close"> {{ item.displayName }} </i>
          </button> 
        </li>
      </ul>
    </div>

答案 2 :(得分:0)

您的所有项目都消失了,因为它们都共享相同的“隐藏”属性,因此,当您单击其中之一时,所有项目都会更改。您应该拥有的是每个项目的属性(此属性应初始化为true)

component.html

<div>
  <ul class="selection-list">
    <li *ngFor="let item of getSelections()">
      <button class="btn" (click)="deselect(item)" *ngIf="item.selected">
        <i class="fa fa-close"> {{ item.displayName }} </i>
      </button> 
    </li>
  </ul>
</div>

component.ts

    deselect(item: TreeNode<string>): void {
        item.selected = false;
        if((item.children) !== null) {          
            item.children.forEach(element => {
            this.deselect(element);
          });
        }
    }