2

I'm trying to hide whole column with all elements in that column when the checkbox is clicked. I'm wondering what's the best approach to solve that using Angular.

 <table class="table">
                    <thead>
                    <tr>
                        <th scope="col">Name</th>
                        <th scope="col">Email</th>
                        <th scope="col">Date</th>
                    </tr>
                    </thead>
                    <tbody>
                    <tr *ngFor="let user of Items" >
                        <td>{{user.Name}}</td>
                        <td>{{user.Email}}</td>
                        <td>{{user.Date}}</td>
                    </tr>
                    </tbody>
                </table>

Above the table I have 3 checkboxes:

Name | Email | Date

I want to press one of them and then whole column disappears including <th> element and <td> element. What could be the best idea for this problem?

2
  • Are you asking about column? Commented Apr 15, 2020 at 19:17
  • Of course, my fault, column Commented Apr 15, 2020 at 19:18

3 Answers 3

2

To hide columns when a checkbox is selected.

In your .ts create 3 variables set to true for each column.

showName = true;
showEmail = true;
showDate = true;

in your respective checkboxes you need to add checked and change calls for each and match it to the 3 booleans above:

  <input type="checkbox" [checked]="!showName" (change)="showName=!showName"/>

  <input type="checkbox" [checked]="!showEmail" (change)="showEmail=!showEmail"/>

  <input type="checkbox" [checked]="!showDate " (change)="showDate =!showDate "/>

And then add *ngIf in each related th and td for example for the name td and th:

<th scope="col" *ngIf="showName">Name</th>
<td *ngIf="showName">{{user.Name}}</td>
Sign up to request clarification or add additional context in comments.

Comments

2

declare class

  export class ColumnVisible{
       public nameVisible:boolean=true;
       public emailVisible:boolean=true;
       public dateVisible:boolean=true;
       constructor(){}
    }

call it in component

columnVisible:ColumnVisible;

in costructor initialize it with

this.columnVisible=new ColumnVisible();

inhtml write as class and give click event

<input [(ngModel)]="columnVisible.nameVisible" type="checkbox"(change)="columnVisible.nameVisible=!columnVisible.nameVisible" /> 
<input [(ngModel)]="columnVisible.emailVisible" type="checkbox"(change)="columnVisible.emailVisible=!columnVisible.emailVisible" /> 
<input [(ngModel)]="columnVisible.dateVisible" type="checkbox"(change)="columnVisible.dateVisible=!columnVisible.dateVisible" /> 
    <table class="table">
        <thead>
        <tr>
            <th  ngIf="columnVisible.nameVisible" scope="col">Name</th>
            <th  ngIf="columnVisible.emailVisible" scope="col">Email</th>
            <th  ngIf="columnVisible.dateVisible" scope="col">Date</th>
        </tr>
        </thead>
        <tbody>
        <tr *ngFor="let user of Items" class="{{user.IsShown}}" >
            <td ngIf="columnVisible.nameVisible" >{{user.Name}}</td>
            <td ngIf="columnVisible.emailVisible">{{user.Email}}</td>
            <td ngIf="columnVisible.dateVisible">{{user.Date}}</td>
        </tr>
        </tbody>
    </table>

Comments

0

Here's a little bit of code. stackblitz. You just need three different models for different checkboxes or use 1 property but with different types

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.