Questions Management – Frontend – Build display update question using Angular, Bootstrap

Check out the full series of Questions Management tutorial here.

To update a question in the Frontend section of the Questions Management application, I will first need to add a new method in the QuestionsService class that will allow us to call the API update question of API Question Service to pass the information of the question which we need to update.

Specifically, this method is as follows:

Because we are passing the content of the question in JSON format, as you can see, I used a variable headers with the declaration:

to do this.

Next we will build the form to update the question.

On the All Questions page, when the user clicks on the Edit button of each line corresponding to a question in the table, I will popup a window using Bootstrap Modal that allows the user to update the information of this question. There is 2 information in this window that is the description of the question and the category that this question belongs to.

To do this, in the all_questions_component.html file in the src/app/questions/all_questions directory, I will add this new Bootstrap Modal after “<!- /.panel ->” with the following content. :

To display this popup, I will modify the code for the “Edit” button, from:

to:

As you can see, this Modal Bootstrap id is “editQuestionModal”. I’ve defined the currentEditDescription variable in the AllQuestionsComponent class to keep the description of the question we want to update:

For the category, we also define a currentEditCategory variable to hold the category information of the question to be updated:

When the user clicks on the “Edit” button, our application will call the editQuestion() method in the AllQuestionsComponent class with the parameter CompositeQuestion object of the question to update. Inside the editQuestion() method, we need to initialize a Question object to hold the new information for the question that needs to be updated and assign the value to currentEditDescription, currentEditCategory as follows:

As you can see, I have also defined a variable as categories:

and get all the categories in the system to display using the CategoriesService.

To keep the category information we choose for this new question, I use the selectedCategory variable in the AllQuestionsComponent class.

When the popup pops up, we need select the current category that this question belongs to so I declared using the Angular compareWith directive in the <select> tag with the value of the byCategory() method name defined in the AllQuestionsComponent class as follows:

When the user clicks on the “Update” button, the updateQuestion() method in the AllQuestionsComponent class will be called to update the question as follows:

with the destroyDatatable() and handleError() methods as follows:

OK, so we have completed the implementation for updating question in the Frontend of the Question Management application. Let’s try it out!

On the All Questions page, click the “Edit” button of any question, a window will appear as follows:

Questions Management – Frontend – Build display update question using Angular, Bootstrap

After editing the contents of this question, click the Update button, the results will be as follows:

Questions Management – Frontend – Build display update question using Angular, Bootstrap

Add Comment