JavaScript Angular - Hide a table row in ngFor if it meets condition angular hide table column,angular hide table if empty,angular

I have an angular app that is rendering a table of employee details. There is a datapoint called Optional that determines if it should show up by default.

My goal here is to hide these rows by default and then be able to toggle them on/off with a click of a button.

Example:

<table class="table table-condensed table-striped" *ngIf="s.fields">
   <tbody>
      <tr *ngFor="let f of s.fields.field" [class.hidden]="f.Optional == 1">
        <td>{{ f.FieldTitle }}</td>
        <td>{{ f.Value }}</td>
      </tr>
    </tbody>
</table>

So far, I have it hiding the rows correctly that are optional by adding a class to them.

I ultimately need to be able to click a button to show/hide these hidden rows but I am unsure how to go about this approach.

Do I use CSS for this or somehow create a 2-way binding or model for it?

Answer:1

Here is my solution for you, using ng-container and a function to determine the state based on optional and toggle state.

TS

state: boolean = false;

isAllowed = (optional) => {
  return optional === 0 ? true : this.state;
}

changeState = () => {
  this.state = !this.state;
}

HTML

<table class="table table-condensed table-striped" *ngIf="s.fields">
   <tbody>
      <tr *ngFor="let f of s.fields.field">
        <ng-container *ngIf="isAllowed(f.Optional)">
          <td>{{ f.FieldTitle }}</td>
          <td>{{ f.Value }}</td>
        </ng-container>
      </tr>
    </tbody>
</table>
<br>
<button (click)="changeState()">Show/Hide</button>
Answer:2

actually i am working on a Project wich requires to observe very large JavaScript Objects with many child objects (Client side). My aim is to syncronize all property changes using socket.io in ...

actually i am working on a Project wich requires to observe very large JavaScript Objects with many child objects (Client side). My aim is to syncronize all property changes using socket.io in ...

I have this html: <tr id="Estuches_1"> <td> <div class="group"> <input name="VariableDataList[0].VariableDataForLevel[0].ProductionOrderId" id="...

I have this html: <tr id="Estuches_1"> <td> <div class="group"> <input name="VariableDataList[0].VariableDataForLevel[0].ProductionOrderId" id="...

I have the following css strings that I am trying to parse using a RegExp. I understand how to split on the comma. How do I work with the result to return an array or string? I guess I'm missing ...

I have the following css strings that I am trying to parse using a RegExp. I understand how to split on the comma. How do I work with the result to return an array or string? I guess I'm missing ...

  1. regex pattern group
  2. regex pattern group java
  3. regex group pattern matching
  4. regex repeating pattern group
  5. python regex pattern group
  6. java regex pattern group example
  7. regex pattern optional group
  8. java regex pattern group name
  9. awk regex pattern group
  10. c# regex pattern group
  11. javascript regex pattern group
  12. regex get pattern group
  13. java.util.regex.pattern group
  14. java regex pattern matcher group example
  15. scala regex pattern matching groups
  16. java regex pattern match group
  17. java regex pattern matcher group
  18. java regex pattern capture group
  19. java.util.regex.pattern$grouptail.match
  20. java.util.regex.pattern$grouphead.match

I'm coding out a temperature app with React and I'm on the part where I'm displaying the different temperatures within the element. Now I'm trying to do &deg;F to return Fahrenheit, but then it ...

I'm coding out a temperature app with React and I'm on the part where I'm displaying the different temperatures within the element. Now I'm trying to do &deg;F to return Fahrenheit, but then it ...