Refreshing the Mat Dialog content when removing items in Angular Material

I have successfully implemented a mat dialog table with 3 columns - name, email, and delete icon. When the user clicks on the delete icon, it prompts a confirmation message to confirm the deletion. Upon confirming, the item is removed from the database. However, I am facing an issue where the changes are not reflecting immediately. I have to close the dialog box and reopen it to see the updated data. How can I ensure that the changes are updated instantly?

TS CODE:
// The parent component contains a master table. On clicking the name, a mat table with nested user list opens up for deletion
delete(name){ 
    //Backend logic
           if(response.status === 400){
               //Show error message
          } else {
            //Open dialog table
            this.openDialog(userData);
          }} })}
   
   openDialog(user, c){
   // Some logic
     const dialogRef = this.dialog.open(DialogExample, {
         width: '700px',
         height: '500px',
         data: {arrayDialog: this.allUsers, c}
       });
     dialogRef.afterClosed().subscribe(result => {
       console.log('The dialog was closed');
       this.allUsers = [];
     });}}

// Child component to open mat dialog table
  @Component({
    selector: 'dialog--example',
    templateUrl: 'dialog.html',
  })
  export class DialogExample {
    readonly userColumns: string[] = ['name', 'email', 'delete'];
   
    delete(email, uName){ 
     //Some logic
    confirmationDialog(e,c) {
      const dialogRef = this.dialog.open(ConfirmationDialog,{
        data:{
          message: 'Please confirm the deletion',
          buttonText: {
            ok: 'Delete',
            cancel: 'Cancel'
          }} });
        dialogRef.afterClosed().subscribe((confirmed: boolean) => {
        if (confirmed) {
          //After closing the confirmation dialog, here I want to update the mat dialog table to show updated data excluding the deleted user details.
        } });}}

  @Component({
    selector: 'deleteconfirm',
    templateUrl: 'deleteconfirm.html',
  })
  export class ConfirmationDialog {
   
returns true if confirmed 
returns false if cancelled

How can I trigger the update? If I try to call the delete(name) function directly, I receive an error stating that it cannot be found. Can someone guide me on how to proceed? Thank you.

Answer №1

passing back the deleted record id to the parent component when the dialog is closed, then filtering data based on that deleted record id

parent.component.ts

  openDialog(user, column){
      // some logic
      const dialogRef = this.dialog.open(DialogExample, {
          width: '700px',
          height: '500px',
          data: {arrayDialog: this.allUsers, column}
        });
      dialogRef.afterClosed().subscribe(result => {
          console.log('the dialog was closed');
          this.allUsers = this.allUsers.filter(item => item.id !== result.id);
      });
  }}

dialogExample.component.ts

export class DialogExample {

constructor(private dialogExampleRef: MatDialogRef<DialogExample>) { }
readonly userColumns: string[] = ['name', 'email', 'delete'];

delete(email, userName){ 
 //some logic
confirmationDialog(email, column) {
  const dialogRef = this.dialog.open(ConfirmationDialog,{
    data:{
      message: 'Please confirm the deletion',
      buttonText: {
        ok: 'Delete',
        cancel: 'Cancel'
      }} });
    dialogRef.afterClosed().subscribe((confirmed: boolean) => {
      if (confirmed) {
        //After closing the confirmation dialog, here I want to update the mat dialog table to show updated data excluding the deleted user details.
          this.dialogExampleRef.close({ email })
    } });}}

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

Using Angular to inject various service implementations into a nested module

In my app, I have a class that implements ErrorHandler and is provided at the root level. There are 3 modules in my app, two of which can use the ErrorHandler at the root level, but one of them requires a different version of the ErrorHandler. I attempted ...

Attempting to implement a typeguard in Typescript that relies on the presence of specific content within an element

Currently, I am attempting to develop a Typescript conditional that verifies if a particular word is already present in the name. The function in question is as follows: isOrganic() { for (let i = 0; i < this.items.length; i++) { if(this.ite ...

Exploring the JSON Array in Angular5 with the power of ngFor

Currently, I am working on a project using Angular5 and encountering an issue with the *ngFor directive. The model class I have defined looks like this: export class FetchApi { value: Array<String>; api_status: string; api_version: string; d ...

Guide to simulating a function using .then within a hook

I am using a function that is called from a hook within my component. Here is my component: ... const handleCompleteContent = (contentId: string) => { postCompleteContent(contentId, playerId).then(data => { if (data === 201) { ... The caller ...

The upcoming construction of 'pages/404' page will not permit the use of getInitialProps or getServerSideProps, however, these methods are not already implemented in my code

Despite my efforts to search for a solution, I have not found anyone facing the same issue as me. When I execute next build, an error occurs stating that I cannot use getInitalProps/getServerSideProps, even though these methods are not used in my 404.tsx f ...

Exploring the functionality of multiple checkboxes in Next.js 14, the zod library, shadcn/ui components, and react-hook

I'm currently working on a form for a client where one of the questions requires the user to select checkboxes (or multiple checkboxes). I'm still learning about zod's schema so I'm facing some challenges in implementing this feature. I ...

Undefined error when refreshing Angular page

One particular page on my forum-like website is causing issues with refreshing. In my project, users can log in, view their profiles as well as others'. However, when I refresh a profile page, no data loads from the server and an error appears in the ...

Share edited collection with Observer

The challenge Imagine creating an Angular service that needs to expose an Observable<number[]> to consumers: numbers: Observable<number[]>; Our requirements are: Receive the latest value upon subscription Receive the entire array every tim ...

The tab title will be invisible if it is located within the second Angular component

Currently, I am utilizing the material css framework in combination with Angular (both latest versions). Everything works perfectly fine when I create tabs within one component. However, when I decide to make each tab a separate component, an issue arises ...

Retrieve the parent injector passed to the component during testing

My approach to obtaining an injector is as follows: constructor( private injector: Injector, ) {} Afterwards, I proceed to create a new injector and utilize the current one as its parent (specifically when opening a material dialog, though this detail ...

What is the best way to link multiple select tags in an HTML document?

I am working with a data grid that contains student information such as Name, Class, and Score. Each row has a checkbox for selection. The requirement is that when the user selects one or more rows and clicks on the "show information" Button, a new windo ...

Angular 10: A Guide to Utilizing RouterModule

I'm working on enhancing one of my components by adding a button that will navigate the page to a different component : <input type="button" value="Shops List" [routerLink]="['shops-list']" class="btn&qu ...

Retrieve an image from Azure Blob storage and directly deliver it to the client through a Node server without the need to store it

Is there a way to directly fetch an image from Azure blob storage and send it to the client without saving it locally? I have managed to retrieve the image from the blob storage and save it as a local file, but I am struggling to send it to the client with ...

A guide on combining multiple arrays within the filter function of arrays in Typescript

Currently, I am incorporating Typescript into an Angular/Ionic project where I have a list of users with corresponding skill sets. My goal is to filter these users based on their online status and skill proficiency. [ { "id": 1, ...

Trying to toggle between two Angular components within the app component using a pair of buttons

Currently, I am developing an application that requires two buttons to display different nested apps. Unfortunately, I am unable to use angular routing for this particular design. These two buttons will be placed within the app.component. When Button A i ...

Dealing with Angular package.json and unresolved peer dependencies

As I embark on the journey of developing an Angular project using CLI, I am also exploring additional components like angular handsontable and primeng. Amidst this exploration, a wave of confusion hit me when I attempted to check the versions of various pa ...

A Guide to Performing Dual API Calls within Angular for a Single Component

Is there a way to make two separate API calls within the same Angular component? For instance, I have an order component that is rendered twice in a tabular manager on a page. Using ngif condition, I display different data for TAB1 and TAB2. The issue is ...

Retrieve the observable value and store it in a variable within my Angular 13 component

Incorporating Angular 13, my service contains the following observable: private _user = new BehaviorSubject<ApplicationUser | null>(null); user$ = this._user.asObservable(); The ApplicationUser model is defined as: export interface ...

The functionality of the Javascript window.print() method is limited to a single use

I've been working on an Angular project and I have the following code snippet implemented in one of the components. Everything works fine when I try to print for the first time using the onClickPrint() method, but it doesn't seem to trigger when ...

Accessing items in a list generated by ngFor in Angular 6 using checkboxes

I need help with displaying an array using ngFor in Angular (specifically Angular 6). I want to be able to select certain cars by checking a checkbox, and then purchase the selected cars when I press a button. Although the list and checkboxes display corr ...