0

I need help with angularjs code. The thing is, in my table rows I have "Edit" button, so if I click on edit a row I want the row data shown in a form below for edit. Please how do I complete the Angularjs code to make this work. I included some codes.

HTML

<div ng-controller="nerDev as div1">
    <table>
        <tr>
            <th>S/N</th>
            <th>FName</th>
            <th>LName</th>
        </tr>
        <tr ng-repeat="mywrk in employees | filter:search">
            <td>{{ $index + 1 }}</td>
            <td>{{mywrk.fname}}</td>
            <td>{{mywrk.lname}}</td>
            <td><input type="button" class="btn btn-secondary"
                ng-model="row.clk" ng-click="EditRow()" value="Edit"/></td>
        </tr>
    </table>
</div>
<div class='col-sm-12'>
    <form method="post" novalidate autocomplete="off">
        <div class="form-group">
            <label>FName:</label> <input type="text" class="form-control"
                ng-model="edit.fname" ng-required="true">
        </div>
        <div class="form-group">
            <label>LName</label> <input type="text" class="form-control"
                ng-model="edit.lname" ng-required="true">
        </div>
        <input class="btn btn-secondary" type="submit" value="Update">
        <input class="btn btn-secondary" type="submit" value="Cancel">
    </form>
</div>

Javascript

var app = angular.module("myApp", ['ngRoute', 'ngResource' ]);
app.controller('MainCtrl', ['$scope','$filter', function ($scope, $filter){

  $scope.employees = [
    { 'fname': 'Kelly', 'lname': 'Bob'},
    { 'fname': 'Jay', 'lname': 'White'}
  ];

}]);

2 Answers 2

4

In your edit button, just pass your object to your function like this

ng-click="EditRow(mywrk)"

then in you controller, just add that function and assign the object you've passed to your $scope.edit object like this

$scope.edit = {}; //you need to initialize this first to prevent error
$scope.EditRow = function(item) {
  $scope.edit = item;
}

And you're done.

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

2 Comments

@BillP Woa my bad I didn't notice that. Thanks for the head's up.
@JkAlombro thanks, the code works but i'm still having a problem. The data is not showing on my the form, but when i logged it on the console it worked. Please could you help me on why the data not showing on the form for edit? Cheers!
0

@JKAlombro answer is one way. Another way, you can get it by $index.

  • In button click event, you need pass row index

    ng-click="EditRow($index)"

  • in controller side

    $scope.edit = {}; $scope.EditRow = function(index) { $scope.edit = $scope.employees[index]; }

1 Comment

Thanks, I used the above method.

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.