Angular 8/7 | Edit/ Add/ Delete Rows in Material Table with using Dialogs inline Row Operation

In this guide, we are going to create a datatable grid with Angular Material Table component which will have some basic operations like Add, Update and Delete rows using Dialog component.

Angular Material Table component is full of features and a wide variety of options are available to create data-rich grids in a very optimized way. It provides a lot basic to advance functionalities like filter, sorting, pagination, expand-collapse, custom templating, etc.

But discussion of these features is out of the scope of this article, so let’s stick to some basic operations we are going to implement like Add, Delete and Update table rows.

To do these operations on table rows, we will use Dialog component on Angular material. A dialog is a popup layout which is displayed to a user above all content in the center of the screen, which can easily catch user eyeballs. Dialog component can be used in many areas like getting prompt responses, showing alert messages, notifying users, getting other user inputs, etc.

To keep this simple we will have two fields in table i.e ID and Name with an extra Action column to have Edit and Delete control links.

So, let’s get to work…

First of all, we are going to make this work in Angular’s latest version which is 8, there are not many changes from version 7 so, you can continue with the previous version as well.

Create a new Angular Project

We will start with a new Angular project, using Angular CLI tool is very easy and quick to start with a project. The current version of Angular CLI is  8.0.3

 

Add Angular Material in Project

To use Angular material, we need to install and configure an Angular project to use its components. let’s go through some quick steps to plug in.

Install packages

Run following NPM command to install @angular/material, @angular/material and @angular/material required as dependencies.

The version of material and dependencies should be at the same level, you can check the version of @angular/core in package.json if its something like 7.2.15 then install packages with @7 otherwise some console errors will creep in as shown below :\

Import BrowserAnimationsModule in Module

Angular Material is known for beautiful animations like ripple effects 😀 so just go to app’s main module file then import BrowserAnimationsModule then add in imports array.

Add Material Theme

Components used in the project are styles using CSS available in the material CSS theme file, so just add the following theme file link in styles.css available at the project root.

you can use any one of these available themes.

That’s it … we are done with Angular material configuration.

Import Material Modules

We are going to use Table, Dialog, FormFields, Input, Buttons as Material components in our tutorial. So we need to import these in the app.module.ts file and also add in the imports array to use them in the application.

Also, import FormsModule as we will use [(ngModel)] in our Dialog.

 

Create Material Table

Next, add Material Table directive in app.component.html which will take rows data object in [dataSource]

There is also a button with openDialog method called on click event.

#mytable is a template variable which we will use to refresh table data by calling

renderRows() method.
*matHeaderRowDef takes an array of columns we want to show in table.
matColumnDef property on the column is the same as a key in the JSON data object.
*matHeaderCellDef have the text of columns in the table header.
Now replace the following code in app.component.ts file

App component is opening a Dialog box with DialogBoxComponent. Just run following npm command to create the component in ng CLI.

–skipTests=true will create component without spec.ts test files

Now open the app.module.ts file again then add DialogBoxComponent in

declarations (added by default if created in CLI) as well as entryComponents otherwise it will throw the following error as Dialog is added dynamically.

 

ERROR Error: No component factory found for EditRowComponent. Did you add it to @NgModule.entryComponents?

So our final app.module.ts file will look like this

 

let’s back to app.component.ts explanation. Here we have Table data to populate and @ViewChild to get table reference with new Static option introduces in Angular 8 and is required.

The openDialog method is getting action string for Add, Update and Delete and obj as row object to pass in open method

Also subscribed to Dialog close event using afterClosed() method calling addRowData, updateRowData and deleteRowData as per event sent back from DialogBoxComponent close() method

In the dialog-box.component.ts file we following code

MAT_DIALOG_DATA is used to get data passed in the open method’s data parameter in app.component.ts
In the dialog-box.component.html file add following code with a heading, input field and buttons to perform action based operation.

That’s it now run the application using

it will look something like this

Conclusion: So here we used Angular Material with latest version 8 to build a table with Edit, Delete and Add actions to operate on table rows using Dialog. We also get to know How to pass data from the parent component to the Dialog box and get back a response in the parent component.

Thanks for visiting :) Let me know if you have some suggestions or feedback for this post in the comment section. Looking for any tutorial or demo on FreakyJolly, just drop me a message here

8
Leave a Reply

avatar
6 Comment threads
2 Thread replies
3 Followers
 
Most reacted comment
Hottest comment thread
7 Comment authors
fahimSachin JohnsonSanaMike B.ayaaz mayet Recent comment authors
  Subscribe  
newest oldest most voted
Notify of
aravind
Guest
aravind

How to do pagination, sorting in the example you have provided.

ayaaz mayet
Guest
ayaaz mayet

why is this required because it stops my code from working – {static:true} ?

Mike B.
Guest
Mike B.

I’m confused – I don’t see any reference to mytable in the ts code. how does the code call renderRows on it?

Sana
Guest
Sana

I get the error “TypeError: this.dataSource.filter is not a function
at AppComponent” when I try to update a value from the dialog box. I read somewhere that this is not support for Angular 6 and above versions. any idea about this?

Sachin Johnson
Guest
Sachin Johnson

try using this.dataSource.data.filter

Sachin Johnson
Guest
Sachin Johnson

Great Example!

fahim
Guest
fahim

Do I need to create the dialog-box.component.ts and dialog-box.component.html file? Can I add the conetnt of dialog-box.component.ts into app.module.ts? I get error on line 16 and 24 of file dialog-box.component.ts.

In ng generate component dialog-boxx --skipTests=true command, is there typo boxx?

In the browser, clicking the buttons only shows message that the ‘dialog-box works!’.