Creating Simple Inline Editing With AngularJS

Introduction

Inline editing is the tool we often need in nearly all of our applications. These days with modern web technologies no one wants multiple pages for displaying and editing contents, like earlier we had viewprofile and editprofile pages. It gives an amazing user experience when the user wants to edit some content and upon his click the display content instantly changes to an editable one and it can be saved and refreshed instantly without going to another page.

This tutorial explains the creation of an inline editing system using AngularJs. Here I assume you have the basic knowledge of AngularJs.

Basic Idea

Let's have a basic idea of what we will do first.

inline

Every time the content is loaded I will check if it is set to be displayed or edited and on the basis of this value I will call my script that contains nothing but the HTML for display and edit respectively.

Example

Let us see an example that will help us understand the situation better.

I have an array of customers, we will list them in a table using ng-repeat and then add some functions for the inline editing functionality.

Code

Let us first display all the customers using ng-repeat.

  1. <table class="table table-striped table-bordered">  
  2.  <thead>  
  3.   <th>Employee Name</th>  
  4.   <th>Employee Email</th>  
  5.   <th>Employee Salary</th>  
  6.   <th>Active</th>  
  7.   <th>Edit</th>  
  8.  </thead>  
  9.  <tbody>  
  10.   <tr ng-repeat="employee in employees">  
  11.    <td>{{employee.empName}}</td>  
  12.    <td>{{employee.empEmail}}</td>  
  13.    <td>{{employee.empSalary | currency:"₹"}}</td>  
  14.    <td>{{employee.active | active}}</td>  
  15.    <td>  
  16.     <button type="button" class="btn btn-primary">Edit</button>  
  17.     <button type="button" class="btn btn-danger">Delete</button>  
  18.    </td>  
  19.   </tr>  
  20.  </tbody>  
  21. <table>  

Here employees is an array of employee data and we are iterating each employee using ng-repeat directive. This is a simple use of ng-repeat, we will modify it depending on our needs to build an in-line editing system, but first let's have a look at how it looks.

table using ng-repeat

Table using ng-repeat

Here I have two buttons, edit and delete. The delete button will simply delete the entry from the database using a HTTP request. Our main focus here is on the edit button that will turn this display mode onto an editing mode. Let us proceed and make some changes to the code to make it functional.

The first change we will make is to add a ng-include directive that will call a function getTemplate() and this function will take a parameter that is nothing but a single employee from the employees array.

  1.    <tr ng-repeat="employee in employees" ng-include="getTemplate(employee)">    

getTemplate() is very simple and will check if the employee passed in the parameter is set to be displayed or editeed and accordingly we will call a script of the HTML content.

  1. $scope.getTemplate = function (employee) {  
  2.     if (employee.empID === $scope.selected.empID){  
  3.         return 'edit';  
  4.     }  
  5.     else return 'display';  
  6. };

As you can see, the preceding code is very simple. The thing that might confuse you is the if condition here. Let me make it more clear here; employee is the value passed as a parameter and $scope.selected is a JSON object that will hold the employee data upon being selected to edit and is initially set to null.

  1. $scope.selected = {};  

So let's have a look at the function that is called on the clicking the edit button and what happens to this $scope.selected object.

  1. $scope.editEmployee = function (employee) {  
  2.     $scope.selected = angular.copy(employee);  
  3. };  

Here I have made use of Angular function to copy and that will copy the contents from one to another and here we are copying the contents of the parameter passed(employee) to $scope.selected object, so now the $scope.selected object is not null and the getTemplate() function returns edit for one employee that will call the edit script instead of display script.

edit functionality

Edit functionality

I have said a couple of times now that we will call various scripts for edit and display, so let's have a look at how these scripts look. The following is the complete code for the HTML part.

  1. <table class="table table-striped table-bordered">  
  2.  <thead>  
  3.   <th>Employee Name</th>  
  4.   <th>Employee Email</th>  
  5.   <th>Employee Salary</th>  
  6.   <th>Active</th>  
  7.   <th>Edit</th>  
  8.  </thead>  
  9.  <tbody>  
  10.   <tr ng-repeat="employee in employees" ng-include="getTemplate(employee)">  
  11.    <script type="text/ng-template" id="display">  
  12.     <td>{{employee.empName}}</td>  
  13.     <td>{{employee.empEmail}}</td>  
  14.     <td>{{employee.empSalary | currency:"₹"}}</td>  
  15.     <td>{{employee.active | active}}</td>  
  16.     <td>  
  17.      <button type="button" class="btn btn-primary" ng-click="editEmployee(employee)">Edit</button>  
  18.      <button type="button" class="btn btn-danger" ng-click="deleteEmployee(employee)">Delete</button>  
  19.     </td>  
  20.    </script>  
  21.    <script type="text/ng-template" id="edit">  
  22.     <td><input type="text" ng-model=employee.empName class="form-control input-sm"/></td>  
  23.     <td><input type="text" ng-model=employee.empEmail class="form-control input-sm"/></td>  
  24.     <td><input type="text" ng-model=employee.empSalary class="form-control input-sm"/></td>  
  25.     <td>  
  26.       <select class="form-control input-sm" ng-model=employee.active>  
  27.         <option value='1'>Yes</option>  
  28.         <option value='0'>No</option>  
  29.       </select>  
  30.     </td>  
  31.     <td>  
  32.      <button type="button" class="btn btn-primary" ng-click="updateEmployee(employee)">Save</button>  
  33.      <button type="button" class="btn btn-danger" ng-click="reset()">Cancel</button>  
  34.     </td>  
  35.    </script>  
  36.   </tr>  
  37.  </tbody>  
  38. <table>

The preceding code doesn't many much changes to it, we have simply added two script tags and given them a unique id display and edit, so the value we return from the getTemplate() is the actually the id of the script that we will call and the contents of that script will be displayed.

Let us see how the screen looks when we click on the edit button.

edit mode table


Edit mode table

Again you can see two buttons, save and cancel. The Save button will save the changes that can be done by making an HTTP request to the database and the cancel button should cancel the changes. As we know, a specific employee is editable because it exists in $scope.selected so upon cancel we can clear the $scope.selected object.

  1. $scope.reset = function () {  
  2.    $scope.selected = {};  
  3. };

This is how you can create a simple in-line editing system, for a live demo of this you can view the recorded webinar here: