In the previous part, we have learned how to create and listen to events. We have implemented functionality for all three buttons which emit custom events named details, update and delete and now we are going to implement functionalities for these events.

For the complete series navigation visit: Vue.js Series.

To download the source code for this part, visit Details and Deleting Owner Entity Source Code.

This post is divided into several sections:

Creating Owner Details Component

In the previous part, we have created the custom details event and an event listener for that event. In the event handler, we’ve used the id parameter from the owner object we have clicked on, and now we are going to use that id to get the owner’s details. To accomplish that, we need to create a new component in which we are going to render the owner’s data.

Let’s do exactly that.

Creating a Component

Let’s Navigate to the src/components/owner directory and create the OwnerDetails.vue file:

The code snippet is pretty self-explanatory but however, let’s analyze it.

Component’s Code Explanation

We have the template with some interpolations to display owner’s name, birthday, etc. There is a conditional rendering as well, which will conditionally display Beginner user or Advanced user text based on the number of accounts related to the owner entity. In the script tag, we have the created lifecycle hook in which we fetch an owner’s id from a query string with the this.$router.currentRoute.params.id expression and then fetch a data from the backend for that owner.

Implementing an API Service and Routing to the Component

Now, let’s create the getAccounts method in our OwnerService:

We still need to create the route for this component.

To do that, we are going to edit the src/router/index.js file:

We have just defined a new route which will accept id as a parameter through the query string. We can access the route parameters in our components through the this.$router.currentRoute.params object.

Finally, we need to navigate to this component when we click on the Details button in the owner list table.

Let’s edit the src/components/owner/OwnerList.vue file:

Great!

Now, we are going to open a terminal and type the npm run dev command to view results:
Owner Account Details - Begginer User - ref keyword

Owner Account Details - Advanced User - ref keyword

Everything looks perfect.

Now let’s implement the Delete functionality.

Deleting the Owner Entity

When a user clicks on the Delete button, the confirmation dialog needs to show up. If we confirm the delete action, a request will be sent to the server. The server may respond with 204 (No Content) status code which means that we have successfully deleted the owner entity.

Otherwise, the server may respond with one of the error messages, depending on what went wrong. In both outcomes, we will display a modal which will inform us if our action was successful or not.

So, let’s summarize it.

We are going to create two modals. One is confirmation modal which has a static content and the second one is a modal which will inform us about the response of the delete request. So, in the second one, we are going to implement the data binding because we need to display different messages depending on the server response.

Creating a Component

Let’s navigate to the OwnerList.vue component and implement that data binding action:

First, let’s analyze the script part.

Component’s Code Explanation

We have moved a fetch owner logic from the created lifecycle hook to the method named fetchOwners. That’s because we need to fetch all the owners again after we successfully delete one owner. Now that we’ve moved the logic to the method, we can reuse our fetch owner code.

Next up, we create some data variables. In the selectedOwnerId variable we are going to store the id parameter from the owner entity we want to delete. In the remaining two variables we will store the title and the content for the second modal.

Let’s take a look at the this.$refs statement. We use it for the first time and we are going to explain it right now.

In the template part, we have two modals. On both modals, we have the ref=”something” attribute. Just as we can get an element in a vanilla JavaScript with the document.getElementById() expression, we can get a reference to the element (or another vue component, b-modal in this case) in the Vue.js by using the ref and this.$refs statements. We can use these references to the modals to trigger the show() method inside them. The rest of the template is also self-explanatory.

Inspecting the Results

Let’s again open a terminal and run the npm run dev command to view results.

If we try to delete Anna Bosh, the confirmation modal will show up:
Confirm your action - ref keyword

When we click on the OK button, the error appears because Anna Bosh have the related accounts:
Error modal - ref keyword

If we try to delete Nick Somion, the confirmation dialogue shows up again:
Confirm your action - ref keyword

And the successfully message shows up:
Successfully modal - ref keyword

Conclusion

By reading this post you’ve learned:

  • How to handle server errors
  • How to reference elements or components from the template
  • And how to create a confirmation dialogue

In the next part of the series, we are going to implement creating and deleting the Account Owner and with that, we are finalizing or application development.