How to conceal certain columns in Angular Material when in mobile view

I am currently utilizing an Angular Material table:

    <div class="table-container">
    <table mat-table [dataSource]="dataSource" class="child">
        <mat-divider></mat-divider>

        <ng-container matColumnDef="title" >
            <th mat-header-cell *matHeaderCellDef> {{ 'GENERAL.TITLE' | translate }} </th>
            <td mat-cell *matCellDef="let element"> {{ (element.title | slice:0:20 )+ '...' }} </td>
        </ng-container>

        <ng-container matColumnDef="writerFullName">
            <th mat-header-cell *matHeaderCellDef> {{ 'POST.WRITER_ID' | translate }} </th>
            <td mat-cell *matCellDef="let element"> {{element.writerFullName}} </td>
        </ng-container>

        <ng-container matColumnDef="paytype">
            <th mat-header-cell *matHeaderCellDef> {{ 'POST.PAY_TYPE' | translate }} </th>
            <td mat-cell *matCellDef="let element"> {{'ENUM.PAY_TYPE.'+element.payType | translate }} </td>
        </ng-container>

        <ng-container matColumnDef="likeCount">
            <th mat-header-cell *matHeaderCellDef> {{ 'POST.LIKE_COUNT' | translate }} </th>
            <td mat-cell *matCellDef="let element"> {{element.likeCount | translate }} </td>
        </ng-container>

        <ng-container matColumnDef="createdOnUtc">
            <th mat-header-cell *matHeaderCellDef> {{ 'POST.CREATE_DATE' | translate }} </th>
            <td mat-cell *matCellDef="let element">
                <span *ngIf="lang=='fa'">{{ element.createdOnUtc | jalali }}</span>
                <span *ngIf="lang!='fa'"> {{element.createdOnUtc | date: 'dd/MM/yyyy hh:mm'}} </span>
            </td>
        </ng-container>

        
        <!-- actions -->
        <ng-container matColumnDef="actions">
            <th mat-header-cell *matHeaderCellDef>
                {{ 'GENERAL.ACTIONS' | translate }}
            </th>

            <td mat-cell *matCellDef="let row; let i=index;">
                
                <a mat-icon-button [matTooltip]="'TOOLTIP.EDIT' | translate" color="primary" uaccess
                    [permission]="':Post:Put'" [routerLink]="['/post/',row.id,'edit']">
                    <mat-icon aria-label="Edit">edit</mat-icon>
                </a>
                <a mat-icon-button (click)="showDetail(row)" [matTooltip]="'TOOLTIP.DETAIL' | translate">
                    <i class="far fa-eye" ></i>
                </a>


               
                  <button mat-icon-button [matTooltip]="'TOOLTIP.DELETE' | translate" color="accent" uaccess
                     [permission]="':Post:Delete'" (click)="delete(row.id)">
                      <mat-icon aria-label="Delete">delete</mat-icon>
                </button>
            </td>
        </ng-container>
        <tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
        <tr mat-row *matRowDef="let row; columns: displayedColumns;"></tr>
    </table>
    <mat-progress-bar *ngIf="dataSource.loading$ | async" mode="indeterminate"></mat-progress-bar>
    <mat-paginator [length]="dataSource.length$ | async" [pageSize]="pageSize" [pageSizeOptions]="pageSizeOptions"
        showFirstLastButtons></mat-paginator>
</div>

Now, I am looking to customize the table by hiding some unnecessary columns. How can I achieve this?

/*****************************/*****************************/**************************/************************/*************************/ /*****************************/*****************************/******************

Answer №1

To customize the values of displayedColumns based on the user agent, you can use the following approach:

ngOnInit() {
  if (isMobileBrowser) this.displayedColumns = ['mobile', 'column', 'set'];
  else this.displayedColumns = ['desktop', 'column', 'big', 'set'];

You can refer to a simple function for detecting mobile browsers in this thread.

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

Data has not been submitted to the MongoDB database

I attempted to save form data in my mongodb database (mongodb compass). However, after submitting the form, I checked the database and found that it was empty. When I clicked the sign-up button, it only displayed curly brackets. Main file - App.js co ...

Are the jQuery selectors provided by the client safe to use?

Is it secure to let users input jQuery selectors for selection? I am considering taking a selector string submitted by users, which could potentially be malicious, and using it in $('') to target elements on the webpage. ...

Is it a good idea to resize images sourced from other websites before displaying them on your

I am currently working on a project that involves fetching images from various websites and displaying a thumbnail or resized version of the original image. Since I will be fetching many images at once, loading the original image files can take longer. ...

Tips for retrieving a string instead of an Observable in @angular/http

I'm currently integrating Angular 4 with .NET Core Web API. The Web API is providing a CustomerName as a string based on the Id given. Here is the service method in Angular 4. I know that angular/http needs to return an Observable due to it being an ...

The module 'AppModule' is encountering an unexpected value of '[object Object]' during import. To resolve this issue, please include a @NgModule annotation

Currently, I am facing an error when trying to import the 'PopupModule' Module from 'ng2-opd-popup' into my application. Despite successful compilation using Webpack, the browser throws the following error message and fails to load: Un ...

Encountering NullInjectorError during Angular 5 Upgrade: InjectionToken LocaleId Provider Not Found

Recently, I made the transition from Angular 4 to Angular 5 using CLI 1.5.4 for my app. Along with that, I have a shared library set up via npm link. To successfully build my project after the upgrade, I had to make changes in my tsconfig.app.json file as ...

What is the process by which Elastic Beanstalk executes Node.js application code?

While my application runs smoothly on localhost, encountering no issues, deploying it to other platforms such as AWS or MCS presents a challenge. Specifically, the problem lies in recognizing the file paths required by the consign library to load the route ...

Disabling the scrollTop() effect following a click event with onClick()

Utilizing the scrollTop() event to toggle the visibility of a div at a specific position and also using the onClick() event to permanently hide the div. While the events are functioning properly, an issue arises when scrolling the page causing the div to r ...

How can I retrieve items from an object that are contained within a nested array with a specific value

I am working with a nested array of objects, and I am trying to extract matching items based on a specific value stored in the nested object within these objects, which also contain nested arrays. Example: Sample data: const items = [ { name: & ...

Tips on how to retrieve the current value of a scope variable in jQuery

When making a $http request to save data on the server and receiving a json response, I want to show an Android-style message using Toast for either success or failure based on the response. Initially, I set a scope variable to false $scope.showSuccessToa ...

Issues arise when trying to use Prettier and ESlint in conjunction with one another

It appears that prettier is not formatting the code as desired. Here is my current ESLint configuration: "eslintConfig": { "root": true, "env": { "node": true }, "extends": [ &q ...

Guide on adjusting shadow intensity with a Directional Light on a BoxGeometry in Three.js

I've been working on adjusting the light and shadow settings for Box Geometry. Specifically, I am using a Directional light with a certain intensity level. However, I've noticed that when I decrease the intensity, the plane appears darker while t ...

Using the Ajax method from a separate class in TypeScript: A step-by-step guide

Recently, I started learning about typescript and ajax. One of the challenges I encountered was while creating a method in typescript for making ajax calls that can be used across classes: myFunc(value: string): JQueryPromise<any> { var dfd = $. ...

Best practice for importing ts files from an npm package

When faced with the need to divide a ts project into multiple repositories/packages for creating microservices, the challenge arises in combining these packages efficiently. Some packages are required in one microservice, others in another, and some in all ...

Exploring Objects within an Array in Ionic 2 and AngularJS 2

I am currently working on displaying reviews obtained from a Laravel API, showcasing feedback on various meals. The goal is to create a slideshow of review messages along with additional data as presented in the array of Objects below: { "2": { ...

Tips for integrating Apache Solr with Cassandra without using DataStax Enterprise (DSE)

Embarking on a new project, I find myself utilizing Cassandra as the chosen DBMS, with Apache Solr serving as the search engine and Node.js powering the server scripting language. While I am well-versed in Node.js, Cassandra and Solr are unfamiliar territ ...

What could be causing my CSS navigation toggle button to malfunction?

My attempt at creating a toggle button for tablets and phones seems to be failing. Despite the javascript class being triggered when I click the toggle button, it is not functioning as expected... https://i.stack.imgur.com/j5BN8.png https://i.stack.imgur. ...

concealing the port number in node.js and express using .htaccess

Currently, I am running two separate node servers on my website. One server is a socket.io server responsible for live streaming data on the main site, while the other server is streaming JSON data to a sub-domain called api.site.com. The main node server ...

Struggling with displaying nested JSON data in React components

I have a nested JSON structure running on my local server, and I'm using React to practice displaying this nested data. Currently, I am focusing on showing the day and the available time slots for that day. I have managed to extract the "days" (mon, t ...

Steps for adding an input box to an md-menu item

I'm looking for an option that allows me to either add an item to an existing list or create a new one, similar to YouTube's 'Add to playlist' feature. The current implementation sort of works, but the menu disappears as soon as the inp ...