【问题标题】:Unable to hide the column header based on the boolean value in the component无法根据组件中的布尔值隐藏列标题
【发布时间】:2019-10-13 21:23:40
【问题描述】:

我正在尝试根据组件中的布尔值显示和隐藏标题和行。我能够隐藏特定的 td 但无法隐藏特定的列。在下面的示例中,我试图隐藏包含 th 和 td 元素的 Last Edited 行。如果我将布尔变量设置为 false,则不会显示该列的 td,但我如何隐藏标题。如果我提出以下条件,它似乎不起作用

  <th *ngIf="c == ColumnNames[1] && showCol""

HTML

 <div *ngIf="LegalFundClasses && LegalFundClasses.LegalFundClassDetailsViewModel && ColumnNames">
     <table class="fundClassesTable table-striped">
      <tr *ngFor="let c of ColumnNames">
        <th [ngClass]="c != ColumnNames[2] ? 'tableItem bold' : 'cellbgcolor'" >{{ c }}</th>
        <ng-container *ngFor="let f of LegalFundClasses.LegalFundClassDetailsViewModel; let i=index">
          <td class="tableItem" *ngIf="c == ColumnNames[0]">{{f.Description}}</td>
          <td class="tableItem" *ngIf="c == ColumnNames[1] && showCol">{{f.AuditSummary}}</td>
          <td class="tableItem" *ngIf="c == ColumnNames[2]">{{f.Id}}</td
        </ng-container>
      </tr>
    </table>
  </div>

组件

 public showCol: boolean = false;
 public ColumnNames: string[] = ['Legal Class Name',
                                    'Last Edited',
                                    'Legal Class ID'
                                  ];

这是小提琴

https://jsfiddle.net/zyk9xhd1/9/

【问题讨论】:

    标签: angular


    【解决方案1】:

    您可以在 th 和 td 标签中添加条件

    <table class="fundClassesTable table-striped" border="1">
          <tr *ngFor="let c of ColumnNames" >
            <th *ngIf="c == ColumnNames[1] && showCol" [ngClass]="c != ColumnNames[2] ? 'tableItem bold' : 'cellbgcolor'" >{{ c }}</th>
            <ng-container *ngFor="let f of data">
              <td class="tableItem" *ngIf="c == ColumnNames[0]">{{f.Description}}</td>
              <td class="tableItem" *ngIf="c == ColumnNames[1] && showCol">{{f.AuditSummary}}</td>
              <td class="tableItem" *ngIf="c == ColumnNames[2]">{{f.Id}}</td>
            </ng-container>
          </tr>
          </table>
    

    https://jsfiddle.net/viethien/w5rq3ghv/16/

    【讨论】:

    • 错了吗?如果您注意到其他两行没有显示并且列标题也没有显示
    【解决方案2】:

    你最好把条件放在整体上tr

    使用下面的代码,它只会显示基于组件中声明的布尔数组的行

      <tr *ngFor="let c of ColumnNames; let rowIndex = index">
      <ng-container *ngIf="rowVisibility[rowIndex]">
        <th [ngClass]="c != ColumnNames[2] ? 'tableItem bold' : 'cellbgcolor'" >{{ c }}</th>
        <ng-container *ngFor="let f of data">
          <td class="tableItem" *ngIf="c == ColumnNames[0]">{{f.Description}}</td>
          <td class="tableItem" *ngIf="c == ColumnNames[1]">{{f.AuditSummary}}</td>
          <td class="tableItem" *ngIf="c == ColumnNames[2]">{{f.Id}}</td>
        </ng-container>
     </ng-container>
    </tr>
    

    component.ts

    constructor(){
    
        //decide which row ro show
        public rowVisibility = [true, //1strow
         false, //2nd row
         true//3rd row
    ];
    

    Example on jsfiddle

    【讨论】:

    • 我想你误解了我的问题。根据我分享的示例,共有三行。我需要根据布尔值隐藏和显示上次编辑的中间行。我有用于切换和设置布尔值的复选框。为简单起见,我刚刚在组件中声明了一个布尔值。如果变量的值为 false,那么我需要隐藏整行,否则将其设置为 true。
    • @Tom 这就是为什么我说你必须添加你想要的条件。检查这个更新的例子:jsfiddle.net/nm10Lr83/4
    • 它可以工作,但如果第三行基于另一个布尔值,说明它如何适应条件。假设另一个变量是 showLastRow 并且该列是 columnName[2]。我还需要根据 showLastRow 的真假值而不是 showMiddleRow 显示这一行
    • @Tom 我修改了我的答案
    • 我不确定这在我的场景中将如何工作,因为并非所有行都具有 true 或 falso 值。目前我有大约 57 个列名和值。 ColumnName[44] 上有一个复选框,可将值设置为 true 或 false。基于此,我根据布尔值隐藏了三行,即 45,46,47,因此我的条件是
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-10
    • 2014-03-27
    • 1970-01-01
    相关资源
    最近更新 更多