Hiding or showing content inside mat-table mat-cells in Angular

I'm working on a simple mat-table where the user can accept or reject rows within the table.

<ng-container matColumnDef="accept">
  <mat-header-cell *matHeaderCellDef mat-sort-header>accept </mat-header-cell>
  <mat-cell *matCellDef="let row">
    <button (click)="accept(row)" mat-raised-button id="accept">accept</button>
  </mat-cell>
</ng-container>

<ng-container matColumnDef="reject">
  <mat-header-cell *matHeaderCellDef mat-sort-header>reject </mat-header-cell>
  <mat-cell *matCellDef="let row">
    <button (click)="reject(row)" mat-raised-button id="reject">reject</button> </mat-cell>
</ng-container>

My goal is to hide the accept and reject buttons when a row has already been accepted or rejected.

I attempted to achieve this using:

 document.getElementById("accept").style.visibility = "hidden';

and also tried:

 document.getElementById("accept").style.display = 'none';

However, both methods resulted in an error "Cannot read property 'style' of null".

If more information is needed to help answer my question, please let me know.

Thank you.

Answer ā„–1

To define the style in TypeScript, consider placing it within the ngAfterViewInit hook. This is important especially if you want to set styles for elements that are not yet part of the DOM.

Answer ā„–2

Here are a couple of ways to accomplish this:

<button #acceptButton (click)="accept(row, acceptButton)" mat-raised-button id="accept">accept</button>

In the accept function, you can access the native properties of the element like this:

accept(row, buttonRef){
    buttonRef._elementRef.nativeElement.style.display = 'none';
    // additional logic here
}

This code will hide the button by changing its display property to none.

Another option is to use *ngIf to conditionally remove the button from the DOM. You can find an example here.

Answer ā„–3

Include the following code in your Tab's container:

[ngClass]="{ 'invisible': !displayTab}"

    .invisible * {
    visibility: hidden !important;
    transition: 0s !important;
}
    toggleTabVisibility(): void{
     this.displayTab = !this.displayTab;
   }

Toggle the tab by adding a

(click)="toggleTabVisibility()"
.

If you need further assistance, check out this resource: How to hide tab, but keep content of tab displayed in angular material in angular2?

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Is it possible to utilize two distinct data sources for the primary chart and dataZoom feature within Apache ECharts?

Working with Apache ECharts requires using two different data sources: one for the main chart and the other for the dataZoom preview. To reduce data sent to clients, I have decreased the resolution over longer time periods. However, when a user zooms in o ...

Unlock the full potential of Angular by taking advantage of {N} application lifecycle events

I recently explored the documentation page for Application Management which covers various application lifecycle events and their usage in JavaScript. However, I encountered a roadblock when attempting to apply these concepts to my NativeScript {N} applica ...

cdkVirtualFor failing to display complete list of items

Having a minor issue that I can't seem to figure out the cause of. My goal is to display a list from an observable (it contains 3 objects) However, only the first two items are showing up. When I use a regular list, all three of them appear. What cou ...

Moving information from two modules to the service (Angular 2)

Recently in my Angular2 project, I created two components (users.component and tasks.component) that need to pass data to a service for processing before sending it to the parent component. Code snippet from users.component.ts: Import { Component } fro ...

HTML5 Drag and Drop: How to Stop Drag and Drop Actions from Occurring Between a Browser and Browser Windows

Is it possible to restrict HTML5 Drag & Drop functionality between different browsers or windows? I am currently working on an Angular2 app that utilizes native HTML5 Drag and Drop feature. Is there a way to prevent users from dragging items out of th ...

When the Mat menu radio button inside a Bootstrap popover is clicked, it will automatically close the popover

I have a popover from Bootstrap with the autoclose attribute set to "outside" in order to close the popover when clicked outside of it. The issue I'm facing is that inside the popover, there is a material menu that opens outside of the popover. The pr ...

The try-catch statement in Typescript is generating an inconsistent return error

I've encountered an issue with a TypeScript function that is flagging inconsistent return error. The function includes a try block that returns a value and a catch block that throws an error, resulting in the inconsistency. I am struggling to find a w ...

Generating a Key/Value pair using a factory limits the properties and their corresponding data types to those defined within a class

Imagine I have a hypothetical vehicle: public class Car { ManufacturedYear: number; Manufacturer: string; } I am aiming to construct a well-defined tree of criteria that can be serialized to JSON, used on the client side for filtering, or on the ser ...

Error: Could not inject CookieService - No provider found for CookieService

I am currently working on an ASP.NET Core 2.0 project that incorporates an Angular 5.1.0 ClientApp in Visual Studio 2017 v15.4.5. My goal is to utilize the ngx-cookie-service within this setup. After closely following the provided instructions for importi ...

Issue with Angular 6 Material2 mat-table MatRipple causing errors

When I try to use MatTable with a data source in Angular 6 and add sorting or pagination, I encounter the following error: ERROR Error: Uncaught (in promise): Error: Can't resolve all parameters for MatRipple: ([object Object], [object Object], [ob ...

Make the width of a table column match the width of the header

I am currently using Primeng2 datatable and I am looking to adjust the column width to match the header size. This is my HTML code: <p-dataTable #dtselfcollectmonthly [exportFilename]="exportname" [rows]="10" [value]="rawdatatrucks"> <ng-con ...

Undefined error encountered in the Google Places library

I'm new to working with the Ionic framework and was experimenting with implementing an autocomplete search feature using the Google Maps API. After going through several resources, I included the following code: <script type="text/javascript" src ...

Tour Of Heroes offers a unique and versatile http create feature

I am currently making my way through the Tour of Heroes tutorial and finding it quite enjoyable. Everything is functioning properly, except for one aspect that has me puzzled - the http adding a hero feature. It seems to automatically know what id to ass ...

A guide on dynamically showcasing/summoning icons in react by utilizing array data

QUESTION: 1 (SOLVED!) https://i.stack.imgur.com/1M1K7.png What is the best way to display icons based on an array of data codes? const data = [{ img: '01d' }, { img: '02d' }] if(data) { data.map((item) => ( <img src={`./ ...

Troubleshooting the error message "Encountering issues with Module build failed (from ./node_modules/postcss-loader/src/index.js)"

Running ng serve results in compilation failure after the chunks are generated. The same codebase is functioning on a co-worker's computer with identical versions as listed below: Node version: 10.16.3 NPM version: 6.9.0 @angular/cli: 7.3.9 Tried ...

How can I uniquely combine a code with an existing CSS class and make modifications to it?

I am using ngx-skeleton-loader and I would like to change the color, but I am facing some difficulties. Here is an image that illustrates the issue. When looking at the developer tools, you can see the styles action in the styles action bar. .loader ...

Angular2 is experiencing issues with child routing not functioning properly unless useAsDefault is set to true

I am encountering an issue with my Angular 2 routing program. When I remove useAsDefault: true, the routing stops working and I receive the following error message: EXCEPTION: Link "["Pen"]" does not resolve to a terminal instruction. in [['Pen&a ...

The functionality of Angular 2's AsyncPipe seems to be limited when working with an Observable

Encountering an Error: EXCEPTION: Unable to find a support object '[object Object]' in [files | async in Images@1:9] Here's the relevant part of the code snippet: <img *ngFor="#file of files | async" [src]="file.path"> Shown is the ...

The Angular checked functionality is not working as expected due to the presence of ngModel

Just getting started with Angular here. Iā€™m working on a checkbox table that compares to another table and automatically checks if it exists. The functionality is all good, but as soon as I add ngModel to save the changes, the initial check seems to be ...

Unable to bring the styles.scss file from angular-notifier into my project

I am having trouble importing the styles.scss file from angular-notifier into my project. Below is the error message that I encountered: Module build failed (from ./node_modules/sass-loader/dist/cjs.js): SassError: Unable to locate stylesheet for import. ...