Continuing with my Node.js Express website on IBM i theme, in this article I’ll show you how easy it is to update employee records from our database.
Building on the previous article, on how to delete records, we will use the same structure to update, by using a popup window to allow the user to amend records.
The stages we need to go through to develop this functionality is as follows:
- Allow user to select an employee to update
- Show a popup window with the current employee details
- Ask the user to input new details
- If the user confirms the update, then update the record
- Send a status message back to the user, letting them know if the update was successful, or not
- Refresh the browser window, showing the current list of employees.
In pictorial form, it will look like this.
The user will select an employee to update.
A popup window will ask the user to input any new details of the selected employee.
A status message will show the user the results of the update request.
So, how do we achieve all this?
The first step we have to do, is to modify our employee.pug file to show an update icon in the employee table.
We add another column, with the font-awesome icon for update.
We have to make this an HTML link. This can be seen in the coding below.
Also, within the employee.pug file we have to code the popup update window.
This is very similar to the delete popup window we coded in my last article.
This is added to the end of the employee.pug file.
Once we have all the above in place, we need the AJAX script that is going to perform the employee update.
In the last article, I called this script ajax_employee.js We will add the update function to this script to keep all our database access within one script. This makes life easier for maintenance.
Let me show you what happens when the SQL delete fails, for whatever reason.
The popup window, after the AJAX call, will display a standard error message.
No great clues there as to what went wrong, but if we look at the console log, we see the SQL error messages that are returned from our server.
Ah a typo, should be EMPLOYEE not EMPLOYEEX. Not the first time my typing has let me down!
As with all my previous articles on this topic, these changes have now been published to my GitHub repository, which can be found at this link. Hopefully, this will provide you with a template to experiment with Node.js on IBM i.
Hopefully in this article I’ve shown how easy it is to select an employee record, display a confirmation pop-up window and then update that employee record.
Now we have all the main functionality in our employee website, in the next article, we will piece all the bits together to make a fully functional application for our employees.
If you have any questions, either on this article, or any other open source, use the comments below, or send me a message on twitter @AndyYouens
Andy Youens is an IBM i consultant/instructor at Milton Keynes, UK-based FormaServe Systems with over 40 years IBM midrange experience.
Leave a Reply