Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular table - display hidden element onClick

I have a column in a table that has an input field, I only want to show the input when the button is clicked. I was using ng-if to display when clicked, however when the button is clicked, all of the input elements display. I only want the input for a row to display.

HTML

    <td *ngIf="!hideStockLevels && row.">
      <div>
        <button class="btn btn-primary" (click)="showInput(row)">Add unit</button>
      </div>
    </td>

    <td *ngIf="!hideStockLevels && !this.isButtonVisible" [hidden]="this.isButtonVisible">
     <input (focusout)="updateUnit(row)" class="inputWidth" [(ngModel)]="unitMap[row.id]" type="text"  value="{{row.Unit}}" [disabled]="dateReceived !== null">
     </td>
like image 369
DZF Avatar asked Jun 27 '26 14:06

DZF


1 Answers

Each row needs to have an unique identifier so we know which row to show/hide. You are using just on variable. Add another (boolean) property to each object, which you toggle on click, for example:

<td>
   <button (click)="row.hidden = !row.hidden">Add unit</button>
</td>

<td [hidden]="row.hidden">
  <input .... />
</td>

I don't know how you toggle when input should be hidden, but then just switch the hidden property to false, when you want to do that.

like image 51
AT82 Avatar answered Jun 29 '26 20:06

AT82



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!