Faizah Pratiwi
Faizah Pratiwi

Reputation: 83

How to make update in Angularjs

I want to make CRUD with angularjs. when i make a update, it can't work. the data is read only the first index. when i click the second row, the data is read the first index. Anyone can help me please? i'm new in study angularjs. data list view :

<table class="table">
        <tr>
            <th>ID</th>
            <th>Name</th>
            <th>Address</th>
            <th>Action</th>
        </tr>
        <tr ng-repeat="x in listData">
            <td>{{x.id}}</td>
            <td>{{x.nama}}</td>
            <td>{{x.alamat}}</td>
            <td>
                <a href="#" ng-click="update(x.id)">Edit</a>
            </td>
        </tr>
    </table>

form update :

<h2>Add Data</h2>
    <table>
        <tr>
            <td>Id</td>
            <td><input type="text" ng-model="id"></td>
        </tr>
        <tr>
            <td>Nama</td>
            <td><input type="text" ng-model="nama"></td>
        </tr>
        <tr>
            <td>Alamat</td>
            <td><input type="text" ng-model="alamat"></td>
        </tr>
        <tr>
            <td>&nbsp;</td>
            <td><input type="button" value="Update" ng-click="edit()"></td>
        </tr>
    </table>

controller :

$scope.update = function(id){
        var index = getSelectedIndex(id);
        var x = $scope.listData[index];
        $scope.id = x.id;
        $scope.nama = x.nama;
        $scope.alamat = x.alamat;
    }

    $scope.edit = function(id){
        var index = getSelectedIndex(id);
        $scope.listData[index].nama = $scope.nama;
        $scope.listData[index].alamat = $scope.alamat;
    }

Upvotes: 0

Views: 36

Answers (1)

mladen.plavsic
mladen.plavsic

Reputation: 189

You are expecting "id" inside "edit()" function, but you are not providing it, and "getSelectedIndex(id)" might be returning first item

Upvotes: 2

Related Questions