Web Application Development Tutorial - Part 3: Creating, Updating and Deleting Books
Creating a New Book
In this section, you will learn how to create a new modal dialog form to create a new book.
BookComponent
Open /src/app/book/book.component.ts
and replace the content as below:
- We defined a property called
isModalOpen
and a method calledcreateBook
.
Open /src/app/book/book.component.html
and make the following changes:
- Added a
New book
button to the card header.. - Added the
abp-modal
which renders a modal to allow user to create a new book.abp-modal
is a pre-built component to show modals. While you could use another approach to show a modal,abp-modal
provides additional benefits.
You can open your browser and click the New book button to see the new modal.
Create a Reactive Form
Reactive forms provide a model-driven approach to handling form inputs whose values change over time.
Open /src/app/book/book.component.ts
and replace the content as below:
- Imported
FormGroup
,FormBuilder
andValidators
from@angular/forms
. - Added a
form: FormGroup
property. - Added a
bookTypes
property as a list ofBookType
enum members. That will be used in form options. - Injected
FormBuilder
into the constructor. FormBuilder provides convenient methods for generating form controls. It reduces the amount of boilerplate needed to build complex forms. - Added a
buildForm
method to the end of the file and executed thebuildForm()
in thecreateBook
method. - Added a
save
method.
Open /src/app/book/book.component.html
and replace <ng-template #abpBody> </ng-template>
with the following code part:
Also replace <ng-template #abpFooter> </ng-template>
with the following code part:
Datepicker
We've used NgBootstrap datepicker in this component. So, we need to arrange the dependencies related to this component.
Open /src/app/book/book.module.ts
and replace the content as below:
- We imported
NgbDatepickerModule
to be able to use the date picker.
Open /src/app/book/book.component.ts
and replace the content as below:
- Imported
NgbDateNativeAdapter
andNgbDateAdapter
. - We added a new provider
NgbDateAdapter
that converts the Datepicker value toDate
type. Check out the datepicker adapters for more details.
Now, you can open your browser to see the changes:
Updating a Book
Open /src/app/book/book.component.ts
and replace the content as shown below:
- We declared a variable named
selectedBook
asBookDto
. - We added an
editBook
method. This method fetches the book with the givenid
and sets it toselectedBook
object. - We replaced the
buildForm
method so that it creates the form with theselectedBook
data. - We replaced the
createBook
method so it setsselectedBook
to an empty object. - We changed the
save
method to handle both of create and update operations.
Add "Actions" Dropdown to the Table
Open /src/app/book/book.component.html
and add the following ngx-datatable-column
definition as the first column in the ngx-datatable
:
Added an "Actions" dropdown as the first column of the table that is shown below:
Also, change the ng-template #abpHeader
section as shown below:
This template will show the Edit text for edit record operation, New Book for new record operation in the title.
Deleting a Book
Open the /src/app/book/book.component.ts
file and inject the ConfirmationService
.
Replace the constructor as below:
- We imported
ConfirmationService
. - We injected
ConfirmationService
to the constructor. - Added a
delete
method.
Check out the Confirmation Popup documentation for more about this service.
Add a Delete Button
Open /src/app/book/book.component.html
and modify the ngbDropdownMenu
to add the delete button as shown below:
The final actions dropdown UI looks like below:
Clicking the "Delete" action calls the delete
method which then shows a confirmation popup as shown below: