0

I have an array like this:

[
 {
  name: "simon",
  surname: "Bi"
 },
 {
  name: "Frank",
  surname: "Bour"
 }
]

I print all data in a table and I want to edit this values:

name1 - surname1 - edit1 - remove1
name2 - surname2 - edit2 - remove2

When I press "edit", I want to copy the user's data to input fields (so just 2 fields, name and surname), so I can change data and update the array but I don't know how to that in AngularJS.

JS:

angular.
   module('peopleList').
   component('peopleList', {
      templateUrl: "list.template.html",
      controller:
            function peopleListController(){
                var self = this;

                self.people = [
                    {
                        name: "Simon",
                        surname: "Bo",
                    }
                ];

                //add
                self.addPerson = function(itemToAdd) {
                    this.people.push(angular.copy(itemToAdd))
                }

                //remove
                self.delete = function(item) {
                    var index = this.people.indexOf(item);
                    this.people.splice(index, 1);
                }

                //edit
                self.edit = function(item){
                code
                }
            }
    });

HTML:

<form name="myForm" ng-submit="$ctrl.addText(form)">
            <div class="form-group">
                <label for="name">Name: </label>
                <input id="name" type="text" class="form-control" ng-model="itemToAdd.name" placeholder="name" required>
            </div>
            <div class="form-group">
                <label for="surname">Surname:</label>
                <input id="surname" type="text" class="form-control" ng-model="itemToAdd.surname" placeholder="surname" required>
               <button ng-click="$ctrl.addPerson(itemToAdd)" class="btn btn-primary" id="add">Add</button>
            </div>
</form>


<table>
            <thead>
                <tr>
                    <td>Name</td>
                    <td>Surname</td>
                    <td colspan="2">Actions</td>
                </tr>
            </thead>
            <tbody>
                <tr ng-repeat="person in $ctrl.people | filter:$ctrl.query | orderBy: $ctrl.orderProp">
                    <td>{{person.name}}</td>
                    <td>{{person.surname}}</td>
                    <td class="edit" ng-click="$ctrl.edit(person)" style="cursor: pointer; text-decoration: underline;">Edit</td>
                    <td class="remove" ng-click="$ctrl.delete(person)" style="cursor: pointer; font-weight: bold; color: red;">X</td>
                </tr>
            </tbody>
        </table>
3
  • 1
    What have you done so far? Provide some code please. Commented Nov 2, 2016 at 10:43
  • ok I added the code Commented Nov 2, 2016 at 10:57
  • @razer90 did you sort out ? Commented Nov 4, 2016 at 13:28

1 Answer 1

1

Try below code it's almost self explanatory,just used ng-model and ng-hide/ng-show to achieve this.

var app = angular.module('app',[]);
app.controller('Ctrl',function($scope,$filter){


$scope.editField={};
  
  $scope.edit = function(index){
    $scope.editField[index] = !$scope.editField[index] ;
    };

$scope.data = [{name: "simon",
surname: "Bi"
},
{name: "Frank",
surname: "Bour"
}];

});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>


<div ng-app="app" class="widget-content" ng-controller="Ctrl">

                <table>
          <tr ng-repeat="emp in data">
                      <td>{{$index+1}} </td>
                      <td ng-hide="editField[$index]"> {{emp.surname}}</td>
                      <td ng-hide="editField[$index]">{{emp.name}} </td>
            <td ng-show="editField[$index]"><input type="text" ng-model="emp.surname"/></td>
            <td ng-show="editField[$index]"><input type="text" ng-model="emp.name"/></td>
            <td ng-click="edit($index)"><button>{{editField[$index]? 'Save': 'Edit'}}</button></td>
  </tr>
                </table>

            </div>

Sign up to request clarification or add additional context in comments.

6 Comments

I updated my question with the code...can you take a look?
Yes I have seen but the example I've provided works the same way you just need to hold the values of ng-models to get the latest updated values.And may be if you can add save function to copy those to a new array like $scope.newData = angular.copy($scope.data);
maybe it's easier than I think, but I can't apply your example to my code...I tried to change ng-model to my input fields but with any result..can u edit my code?
<td ng-hide="editField[$index]"> {{emp.surname}}</td> <td ng-hide="editField[$index]">{{emp.name}} </td> <td ng-show="editField[$index]"><input type="text" ng-model="emp.surname"/></td> <td ng-show="editField[$index]"><input type="text" ng-model="emp.name"/></td> try implementing this part
ok I understood your code, but I don't want use this trick (show/hide..). I just want copy some values to input field (the SAME input field that I use to add people)..there is a way to do that?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.