angular

Part 2 : Simple list example Add/Edit and Delete with Angular

In this tutorial, we will learn add, edit and delete functionality into angularjs application.We will add functionality of add record, edit record and delete record on grid listing.Angular provide very simple logic on behind add/edit and delete functionality, If you have knowledge of JSON object/Array then you can do it with in minute, you learn within five minute to add/edit and delete functionality on angular.

Step 1: We will add add,edit and delete icon on table list.I am using bootstrap3 lib so here i am using class of bootstrap.

<div class="btn-group">
                <button type="button" class="btn btn-default btn"><i class="glyphicon glyphicon-pencil"></i></button>  
                <button type="button" class="btn btn-default btn"><i class="glyphicon glyphicon-trash"></i></button> 
</div>









Step 2: Now We will create add,edit and delete function in controller.

Related Post
$scope.edit = function(id) {
     //search user and update it
      $scope.objectIndex = id;
   $scope.userObject = angular.copy($scope.userList[id]);
   console.log($scope.objectIndex);
  }
  
  $scope.save = function() {
   console.log($scope.objectIndex);
   if($scope.userList[$scope.objectIndex] == null) {
    //this is new record
    $scope.userList.push($scope.userObject);
   } else {
    //for existing record
    //and update it.
     $scope.userList[$scope.objectIndex] = $scope.userObject;               
   }
    
   //clear the add record form
   $scope.userObject = {};
   $scope.objectIndex = '';
  }
  
  $scope.delete = function(id) {
   //search record with given id and delete it
   for(i in $scope.userList) {
    if($scope.userList[i].id == id) {
     $scope.userList.splice(i,1);
     $scope.newcontact = {};
    }
   }
    
  }

Step 3: Now we will call these method on click of icon on table list.

<div class="btn-group">
                <button type="button" class="btn btn-default btn" ng-click="edit($index);"><i class="glyphicon glyphicon-pencil"></i></button>  
                <button type="button" class="btn btn-default btn" ng-click="delete();"><i class="glyphicon glyphicon-trash"></i></button> 
</div>









I hope this help you.You can download source code and see Demo from below link.

Recent Posts

What is the Purpose of php_eol in PHP?

in this quick PHP tutorial, We'll discuss php_eol with examples. PHP_EOL is a predefined constant in PHP and represents an… Read More

2 months ago

Laravel Table Relationship Methods With Example

This Laravel tutorial helps to understand table Relationships using Elequonte ORM. We'll explore laravel table Relationships usage and best practices… Read More

2 months ago

Exploring the Power of Laravel Eloquent Join?

We'll explore different join methods of Laravel eloquent with examples. The join helps to fetch the data from multiple database… Read More

2 months ago

Quick and Easy Installation of Laravel Valet

in this Laravel tutorial, We'll explore valet, which is a development environment for macOS minimalists. It's a lightweight Laravel development… Read More

3 months ago

What is Laravel Soft Delete and How Does it Work?

I'll go through how to use soft delete in Laravel 10 in this post. The soft deletes are a method… Read More

3 months ago

Common Practices for Laravel Blade Template

in this Laravel tutorial, I will explore common practices for using the Laravel Blade template with examples. Blade is a… Read More

3 months ago

Categories